-
Notifications
You must be signed in to change notification settings - Fork 3
/
variable_string.go
69 lines (56 loc) · 1.29 KB
/
variable_string.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package unpuzzled
import (
"flag"
)
type StringVariable struct {
Name string
Description string
Default string
Required bool
Destination *string
flagDestination *string
}
func (s *StringVariable) GetName() string {
return s.Name
}
func (s *StringVariable) GetDescription() string {
return s.Description
}
func (s *StringVariable) GetDestination() interface{} {
return s.Destination
}
func (s *StringVariable) IsRequired() bool {
return s.Required
}
func (s *StringVariable) GetDefault() (interface{}, bool) {
if s.Default == "" {
return "", false
} else {
return s.Default, true
}
}
func (s *StringVariable) apply(val interface{}) {
if stringVal, ok := val.(string); ok {
*s.Destination = stringVal
}
}
func (s *StringVariable) setDefaults() {
if s.Default != "" {
*s.Destination = s.Default
}
}
func (s *StringVariable) setFlag(flagset *flag.FlagSet) {
var destination string
s.flagDestination = &destination
flagset.StringVar(s.flagDestination, s.Name, s.Default, s.Description)
}
func (s *StringVariable) getFlagValue(set *flag.FlagSet) (interface{}, bool) {
if *s.flagDestination == "" {
return nil, false
} else {
return *s.flagDestination, true
}
}
func (s *StringVariable) setEnv(value string, envName string) (interface{}, bool) {
return value, true
}