-
Notifications
You must be signed in to change notification settings - Fork 3
/
variable_int64.go
88 lines (75 loc) · 1.74 KB
/
variable_int64.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package unpuzzled
import (
"flag"
"strconv"
log "github.com/sirupsen/logrus"
)
type Int64Variable struct {
Name string
Description string
Default int64
Required bool
Destination *int64
flagDestination *int64
}
func (i *Int64Variable) GetName() string {
return i.Name
}
func (i *Int64Variable) GetDescription() string {
return i.Description
}
func (i *Int64Variable) GetDestination() interface{} {
return i.Destination
}
func (i *Int64Variable) IsRequired() bool {
return i.Required
}
func (i *Int64Variable) GetDefault() (interface{}, bool) {
if i.Default == 0 {
return nil, false
} else {
return i.Default, true
}
}
func (i *Int64Variable) apply(val interface{}) {
switch val.(type) {
case int:
value := val.(int)
*i.Destination = int64(value)
case int64:
value := val.(int64)
*i.Destination = value
case float64:
value := val.(float64)
*i.Destination = int64(value)
}
}
func (i *Int64Variable) setDefaults() {
if i.Default != int64(0) {
*i.Destination = i.Default
}
}
func (i *Int64Variable) setFlag(flagset *flag.FlagSet) {
var destination int64
i.flagDestination = &destination
flagset.Int64Var(i.flagDestination, i.Name, i.Default, i.Description)
}
func (i *Int64Variable) getFlagValue(set *flag.FlagSet) (interface{}, bool) {
if *i.flagDestination == int64(0) {
return nil, false
} else {
return *i.flagDestination, true
}
}
func (i *Int64Variable) setEnv(value string, envName string) (interface{}, bool) {
parsedValue, err := strconv.ParseInt(value, 0, 64)
if err != nil {
log.WithFields(log.Fields{
"envName": envName,
"err": err,
"envValue": value,
"name": i.Name,
}).Fatal("Failed to parse from environment.")
}
return parsedValue, true
}