Debian Codegraph @

ui/validators.go

raw symbols SCIP
1 package ui
2
3 import (
4 "fmt"
5 "net"
6 "strings"
7
8 "github.com/manifoldco/promptui"
9 )
10
11 // NotEmpty is a validation function that checks that the prompted string is not
12 // empty.
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 // Address is a validation function that checks that the prompted string is a
23 // valid TCP address.
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 // IPAddress is validation function that checks that the prompted string is a
34 // valid IP address.
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 // DNS is a validation function that changes that the prompted string is a valid
45 // DNS name.
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 // YesNo is a validation function that checks for a Yes/No answer.
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 }