forked from xgfone/gconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_cli.go
179 lines (164 loc) · 5.31 KB
/
source_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
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
// 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"
"fmt"
"strings"
"time"
"github.com/urfave/cli/v2"
)
// ConvertOptsToCliFlags converts the options from the group to flags of
// github.com/urfave/cli/v2.
//
// If prefix is not empty, it will add the prefix to the flag name,
// and join them with the character "-".
//
// Notice: the character "_" in the flag name will be converted to "-".
func ConvertOptsToCliFlags(group *OptGroup, prefix ...string) []cli.Flag {
var _prefix string
if len(prefix) > 0 && prefix[0] != "" {
_prefix = prefix[0]
}
opts := group.AllOpts()
flags := make([]cli.Flag, len(opts))
for i, opt := range opts {
if !opt.Cli {
continue
}
name := opt.Name
if _prefix != "" {
name = fmt.Sprintf("%s-%s", _prefix, name)
}
name = strings.Replace(name, "_", "-", -1)
if opt.Short != "" {
name = fmt.Sprintf("%s, %s", name, opt.Short)
}
var flag cli.Flag
switch v := opt.Default.(type) {
case bool:
flag = &cli.BoolFlag{Name: name, Value: v, Aliases: opt.Aliases, Usage: opt.Help}
case int:
flag = &cli.IntFlag{Name: name, Value: v, Aliases: opt.Aliases, Usage: opt.Help}
case int32:
flag = &cli.IntFlag{Name: name, Value: int(v), Aliases: opt.Aliases, Usage: opt.Help}
case int64:
flag = &cli.Int64Flag{Name: name, Value: v, Aliases: opt.Aliases, Usage: opt.Help}
case uint:
flag = &cli.UintFlag{Name: name, Value: v, Aliases: opt.Aliases, Usage: opt.Help}
case uint32:
flag = &cli.UintFlag{Name: name, Value: uint(v), Aliases: opt.Aliases, Usage: opt.Help}
case uint64:
flag = &cli.Uint64Flag{Name: name, Value: v, Aliases: opt.Aliases, Usage: opt.Help}
case float64:
flag = &cli.Float64Flag{Name: name, Value: v, Aliases: opt.Aliases, Usage: opt.Help}
case string:
flag = &cli.StringFlag{Name: name, Value: v, Aliases: opt.Aliases, Usage: opt.Help}
case time.Duration:
flag = &cli.DurationFlag{Name: name, Value: v, Aliases: opt.Aliases, Usage: opt.Help}
case time.Time:
flag = &cli.StringFlag{Name: name, Value: v.Format(time.RFC3339), Aliases: opt.Aliases, Usage: opt.Help}
case []int:
var s string
if len(v) > 0 {
s = fmt.Sprintf("%v", v)
}
flag = &cli.StringFlag{Name: name, Value: s, Aliases: opt.Aliases, Usage: opt.Help}
case []uint:
var s string
if len(v) > 0 {
s = fmt.Sprintf("%v", v)
}
flag = &cli.StringFlag{Name: name, Value: s, Aliases: opt.Aliases, Usage: opt.Help}
case []float64:
var s string
if len(v) > 0 {
s = fmt.Sprintf("%v", v)
}
flag = &cli.StringFlag{Name: name, Value: s, Aliases: opt.Aliases, Usage: opt.Help}
case []string:
var s string
if len(v) > 0 {
s = fmt.Sprintf("%v", v)
}
flag = &cli.StringFlag{Name: name, Value: s, Aliases: opt.Aliases, Usage: opt.Help}
case []time.Duration:
var s string
if len(v) > 0 {
s = fmt.Sprintf("%v", v)
}
flag = &cli.StringFlag{Name: name, Value: s, Aliases: opt.Aliases, Usage: opt.Help}
default:
flag = &cli.StringFlag{Name: name, Value: fmt.Sprintf("%v", v), Aliases: opt.Aliases, Usage: opt.Help}
}
flags[i] = flag
}
return flags
}
// NewCliSource returns a new source based on "github.com/urfave/cli",
// which will reads the configuration data from the flags of cli.
//
// groups stands for the group that the context belongs on. The command name
// may be considered as the group name. The following ways are valid.
//
// NewCliSource(ctx) // With the default global group
// NewCliSource(ctx, "group1") // With group "group1"
// NewCliSource(ctx, "group1", "group2") // With group "group1.group2"
// NewCliSource(ctx, "group1.group2") // With group "group1.group2"
//
func NewCliSource(ctx *cli.Context, groups ...string) Source {
var group string
if len(groups) > 0 {
group = strings.Trim(strings.Join(groups, "."), ".")
}
return cliSource{ctx: ctx, group: group}
}
type cliSource struct {
ctx *cli.Context
group string
}
func (c cliSource) Watch(load func(DataSet, error) bool, exit <-chan struct{}) {}
func (c cliSource) Read() (DataSet, error) {
opts := make(map[string]string, 16)
c.getFlags(c.group, c.ctx, c.ctx.FlagNames(), opts)
if len(opts) == 0 {
return DataSet{Source: "cli", Format: "json"}, nil
}
data, _ := json.Marshal(opts)
ds := DataSet{
Data: data,
Format: "json",
Source: "cli",
Timestamp: time.Now(),
}
ds.Checksum = "md5:" + ds.Md5()
return ds, nil
}
func (c cliSource) getFlags(group string, ctx *cli.Context, names []string, opts map[string]string) {
for _, name := range names {
key := name
if group != "" {
key = fmt.Sprintf("%s.%s", group, key)
}
if _, ok := opts[key]; ok {
continue
}
switch v := ctx.Generic(name).(type) {
case nil:
continue
case fmt.Stringer:
opts[key] = v.String()
default:
panic(fmt.Errorf("unknown type '%T'", v))
}
}
}