-
Notifications
You must be signed in to change notification settings - Fork 38
/
cli.go
43 lines (36 loc) · 1.04 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package cli
import (
"fmt"
"net/url"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
type errInvalidProtocol struct {
Protocol string
}
func (e *errInvalidProtocol) Error() string {
return fmt.Sprintf("invalid protocol %s, must be http or https", e.Protocol)
}
func stringSliceContains(stringSlice []string, value string) bool {
for _, x := range stringSlice {
if value == x {
return true
}
}
return false
}
// ValidateProtocol validates a Protocol passed in from the command line
func ValidateProtocol(v *viper.Viper, flagname string) error {
if p := v.GetString(flagname); p != "http" && p != "https" {
return errors.Wrap(&errInvalidProtocol{Protocol: p}, fmt.Sprintf("%s is invalid", flagname))
}
return nil
}
// ValidateURL validates a URL passed in from the command line
func ValidateURL(v *viper.Viper, flagname string) error {
endpoint := v.GetString(flagname)
if _, err := url.Parse(endpoint); endpoint != "" && err != nil {
return errors.Wrapf(err, "Unable to parse %s endpoint %s", flagname, endpoint)
}
return nil
}