-
Notifications
You must be signed in to change notification settings - Fork 11
/
flag.go
144 lines (113 loc) · 2.55 KB
/
flag.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package glacier
import (
"time"
"github.com/mylxsw/glacier/infra"
)
func (impl *framework) buildFlagContext(cliCtx infra.FlagContext) func() (infra.FlagContext, error) {
return func() (infra.FlagContext, error) {
res, err := impl.cc.CallWithProvider(impl.flagContextInit, impl.cc.Provider(func() infra.FlagContext {
return cliCtx
}))
if err != nil {
return nil, err
}
return res[0].(infra.FlagContext), nil
}
}
type FlagContext struct {
data map[string]interface{}
}
func (f *FlagContext) setData(name string, value interface{}) {
f.data[name] = value
}
func (f *FlagContext) SetString(name string, value string) { f.setData(name, value) }
func (f *FlagContext) SetStringSlice(name string, value []string) { f.setData(name, value) }
func (f *FlagContext) SetBool(name string, value bool) { f.setData(name, value) }
func (f *FlagContext) SetInt(name string, value int) { f.setData(name, value) }
func (f *FlagContext) SetIntSlice(name string, value []int) { f.setData(name, value) }
func (f *FlagContext) SetDuration(name string, value time.Duration) { f.setData(name, value) }
func (f *FlagContext) SetFloat64(name string, value float64) { f.setData(name, value) }
func (f *FlagContext) String(name string) string {
raw, ok := f.data[name]
if !ok {
return ""
}
val, ok := raw.(string)
if !ok {
return ""
}
return val
}
func (f *FlagContext) StringSlice(name string) []string {
raw, ok := f.data[name]
if !ok {
return []string{}
}
val, ok := raw.([]string)
if !ok {
return []string{}
}
return val
}
func (f *FlagContext) Bool(name string) bool {
raw, ok := f.data[name]
if !ok {
return false
}
val, ok := raw.(bool)
if !ok {
return false
}
return val
}
func (f *FlagContext) Int(name string) int {
raw, ok := f.data[name]
if !ok {
return 0
}
val, ok := raw.(int)
if !ok {
return 0
}
return val
}
func (f *FlagContext) IntSlice(name string) []int {
raw, ok := f.data[name]
if !ok {
return []int{}
}
val, ok := raw.([]int)
if !ok {
return []int{}
}
return val
}
func (f *FlagContext) Duration(name string) time.Duration {
raw, ok := f.data[name]
if !ok {
return 0
}
val, ok := raw.(time.Duration)
if !ok {
return 0
}
return val
}
func (f *FlagContext) Float64(name string) float64 {
raw, ok := f.data[name]
if !ok {
return 0
}
val, ok := raw.(float64)
if !ok {
return 0
}
return val
}
func (f *FlagContext) FlagNames() []string {
names := make([]string, 0)
for k := range f.data {
names = append(names, k)
}
return names
}