forked from xgfone/gconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_flag.go
214 lines (191 loc) · 5.69 KB
/
source_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2019 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gconf
import (
"encoding/json"
"flag"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"time"
)
// isZeroValue determines whether the string represents the zero
// value for a flag.
func isZeroValue(f *flag.Flag, value string) bool {
// Build a zero value of the flag's Value type, and see if the
// result of calling its String method equals the value passed in.
// This works unless the Value type is itself an interface type.
typ := reflect.TypeOf(f.Value)
var z reflect.Value
if typ.Kind() == reflect.Ptr {
z = reflect.New(typ.Elem())
} else {
z = reflect.Zero(typ)
}
return value == z.Interface().(flag.Value).String()
}
// PrintFlagUsage prints the flag usage instead of the default.
func PrintFlagUsage(flagSet *flag.FlagSet) {
flagSet.VisitAll(func(f *flag.Flag) {
// Two spaces before -; see next two comments.
prefix := " -"
if len(f.Name) > 1 {
prefix += "-"
}
s := fmt.Sprintf(prefix+"%s", f.Name)
name, usage := flag.UnquoteUsage(f)
if len(name) > 0 {
s += " " + name
} else {
vf := reflect.ValueOf(f.Value)
if vf.Kind() == reflect.Ptr {
vf = vf.Elem()
}
if vf.Kind() == reflect.Bool {
s += " bool"
}
}
// Boolean flags of one ASCII letter are so common we
// treat them specially, putting their usage on the same line.
if len(s) <= 4 { // space, space, '-', 'x'.
s += "\t"
} else {
// Four spaces before the tab triggers good alignment
// for both 4- and 8-space tab stops.
s += "\n \t"
}
s += strings.Replace(usage, "\n", "\n \t", -1)
if !isZeroValue(f, f.DefValue) {
vf := reflect.ValueOf(f.Value)
if vf.Kind() == reflect.Ptr {
vf = vf.Elem()
}
switch vf.Kind() {
case reflect.String:
// put quotes on the value
s += fmt.Sprintf(" (default %q)", f.DefValue)
case reflect.Int64:
if _, err := time.ParseDuration(f.DefValue); err != nil {
s += fmt.Sprintf(" (default %s)", f.DefValue)
} else {
s += fmt.Sprintf(" (default %q)", f.DefValue)
}
default:
s += fmt.Sprintf(" (default %s)", f.DefValue)
}
}
fmt.Fprint(flagSet.Output(), s, "\n")
})
}
// AddOptFlag adds the option to the flagSet, which is flag.CommandLine
// by default.
func AddOptFlag(c *Config, flagSet ...*flag.FlagSet) {
addAndParseOptFlag(false, c, flagSet...)
}
// AddAndParseOptFlag adds the option to the flagSet, which is flag.CommandLine
// by default, then parses the flagSet.
//
// Notice: if there is the version flag and it is true, it will print the version
// and exit.
func AddAndParseOptFlag(c *Config, flagSet ...*flag.FlagSet) error {
return addAndParseOptFlag(true, c, flagSet...)
}
func addAndParseOptFlag(parse bool, c *Config, flagSet ...*flag.FlagSet) error {
flagset := flag.CommandLine
if len(flagSet) > 0 && flagSet[0] != nil {
flagset = flagSet[0]
}
var vName, value string
if opt := c.GetVersion(); opt.Name != "" && opt.Default != nil {
flagset.Bool(opt.Name, false, opt.Help)
vName = opt.Name
value = opt.Default.(string)
}
flagset.Usage = func() { PrintFlagUsage(flagset) }
for _, group := range c.AllGroups() {
for _, opt := range group.AllOpts() {
if !opt.Cli {
continue
}
name := opt.Name
if gname := group.Name(); gname != "" {
name = fmt.Sprintf("%s.%s", gname, opt.Name)
}
name = strings.Replace(name, "_", "-", -1)
switch v := opt.Default.(type) {
case nil:
flagset.String(name, "", opt.Help)
case bool:
flagset.Bool(name, v, opt.Help)
case int, int8, int16, int32, int64:
flagset.Int64(name, reflect.ValueOf(v).Int(), opt.Help)
case uint, uint8, uint16, uint32, uint64:
flagset.Uint64(name, reflect.ValueOf(v).Uint(), opt.Help)
case float32, float64:
flagset.Float64(name, reflect.ValueOf(v).Float(), opt.Help)
case time.Duration:
flagset.Duration(name, v, opt.Help)
default:
flagset.String(name, fmt.Sprintf("%v", v), opt.Help)
}
}
}
if parse {
if err := flagset.Parse(os.Args[1:]); err != nil {
return err
}
if vName != "" {
if flag := flagset.Lookup(vName); flag != nil {
if yes, _ := strconv.ParseBool(flag.Value.String()); yes {
fmt.Println(value)
os.Exit(0)
}
}
}
}
return nil
}
// NewFlagSource returns a new source based on flag.FlagSet,
// which is flag.CommandLine by default.
func NewFlagSource(flagSet ...*flag.FlagSet) Source {
flagset := flag.CommandLine
if len(flagSet) > 0 && flagSet[0] != nil {
flagset = flagSet[0]
}
return flagSource{flagSet: flagset}
}
type flagSource struct {
flagSet *flag.FlagSet
}
func (f flagSource) Watch(load func(DataSet, error) bool, exit <-chan struct{}) {}
func (f flagSource) Read() (DataSet, error) {
if !f.flagSet.Parsed() {
if err := f.flagSet.Parse(os.Args[1:]); err != nil {
return DataSet{Source: "flag", Format: "json"}, err
}
}
vs := make(map[string]string, 32)
f.flagSet.Visit(func(f *flag.Flag) {
vs[strings.Replace(f.Name, "-", "_", -1)] = f.Value.String()
})
data, err := json.Marshal(vs)
if err != nil {
return DataSet{Source: "flag", Format: "json"}, err
}
ds := DataSet{Data: data, Format: "json", Source: "flag", Timestamp: time.Now()}
ds.Checksum = fmt.Sprintf("md5:%s", ds.Md5())
return ds, nil
}