| 1 |
package ui |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/manifoldco/promptui" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
func NotEmpty() promptui.ValidateFunc { |
| 14 |
return func(s string) error { |
| 15 |
if len(strings.TrimSpace(s)) == 0 { |
| 16 |
return fmt.Errorf("value is empty") |
| 17 |
} |
| 18 |
return nil |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
func Address() promptui.ValidateFunc { |
| 25 |
return func(s string) error { |
| 26 |
if _, _, err := net.SplitHostPort(s); err != nil { |
| 27 |
return fmt.Errorf("%s is not an TCP address", s) |
| 28 |
} |
| 29 |
return nil |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
func IPAddress() promptui.ValidateFunc { |
| 36 |
return func(s string) error { |
| 37 |
if net.ParseIP(s) == nil { |
| 38 |
return fmt.Errorf("%s is not an ip address", s) |
| 39 |
} |
| 40 |
return nil |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
func DNS() promptui.ValidateFunc { |
| 47 |
return func(s string) error { |
| 48 |
if len(strings.TrimSpace(s)) == 0 { |
| 49 |
return fmt.Errorf("value is empty") |
| 50 |
} |
| 51 |
if _, _, err := net.SplitHostPort(s + ":443"); err != nil { |
| 52 |
return fmt.Errorf("%s is not a valid DNS name", s) |
| 53 |
} |
| 54 |
return nil |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
func YesNo() promptui.ValidateFunc { |
| 60 |
return func(s string) error { |
| 61 |
s = strings.ToLower(strings.TrimSpace(s)) |
| 62 |
switch s { |
| 63 |
case "y", "yes", "n", "no": |
| 64 |
return nil |
| 65 |
default: |
| 66 |
return fmt.Errorf("%s is not a valid answer", s) |
| 67 |
} |
| 68 |
} |
| 69 |
} |