Skip to content

Commit f664246

Browse files
james-prysmDokiysDokiy
authored
Add support for alias in YAMLs (#1448)
* adding in boolean alias support * fixing formatting * adding string alias and test * adding int alias and test * add support for duration flag * adding float flag alias support * adding alias support to remaining flags and fixing tests * fixing test * Modify nesting flag apply Co-authored-by: Dokiy <[email protected]> Co-authored-by: Dokiy <[email protected]>
1 parent b98c059 commit f664246

File tree

2 files changed

+245
-60
lines changed

2 files changed

+245
-60
lines changed

altsrc/flag.go

Lines changed: 115 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,22 @@ func InitInputSourceWithContext(flags []cli.Flag, createInputSource func(cCtx *c
6464

6565
// ApplyInputSourceValue applies a generic value to the flagSet if required
6666
func (f *GenericFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
67-
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.GenericFlag.Name) {
68-
value, err := isc.Generic(f.GenericFlag.Name)
67+
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
68+
return nil
69+
}
70+
for _, name := range f.GenericFlag.Names() {
71+
if !isc.isSet(name) {
72+
continue
73+
}
74+
value, err := isc.Generic(name)
6975
if err != nil {
7076
return err
7177
}
72-
if value != nil {
73-
for _, name := range f.Names() {
74-
_ = f.set.Set(name, value.String())
75-
}
78+
if value == nil {
79+
continue
80+
}
81+
for _, n := range f.Names() {
82+
_ = f.set.Set(n, value.String())
7683
}
7784
}
7885

@@ -81,138 +88,186 @@ func (f *GenericFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceCo
8188

8289
// ApplyInputSourceValue applies a StringSlice value to the flagSet if required
8390
func (f *StringSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
84-
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.StringSliceFlag.Name) {
85-
value, err := isc.StringSlice(f.StringSliceFlag.Name)
91+
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
92+
return nil
93+
}
94+
for _, name := range f.StringSliceFlag.Names() {
95+
if !isc.isSet(name) {
96+
continue
97+
}
98+
value, err := isc.StringSlice(name)
8699
if err != nil {
87100
return err
88101
}
89-
if value != nil {
90-
var sliceValue cli.StringSlice = *(cli.NewStringSlice(value...))
91-
for _, name := range f.Names() {
92-
underlyingFlag := f.set.Lookup(name)
93-
if underlyingFlag != nil {
94-
underlyingFlag.Value = &sliceValue
95-
}
102+
if value == nil {
103+
continue
104+
}
105+
var sliceValue = *(cli.NewStringSlice(value...))
106+
for _, n := range f.Names() {
107+
underlyingFlag := f.set.Lookup(n)
108+
if underlyingFlag == nil {
109+
continue
96110
}
111+
underlyingFlag.Value = &sliceValue
97112
}
98113
}
99114
return nil
100115
}
101116

102117
// ApplyInputSourceValue applies a IntSlice value if required
103118
func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
104-
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.IntSliceFlag.Name) {
105-
value, err := isc.IntSlice(f.IntSliceFlag.Name)
119+
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
120+
return nil
121+
}
122+
for _, name := range f.IntSliceFlag.Names() {
123+
if !isc.isSet(name) {
124+
continue
125+
}
126+
value, err := isc.IntSlice(name)
106127
if err != nil {
107128
return err
108129
}
109-
if value != nil {
110-
var sliceValue cli.IntSlice = *(cli.NewIntSlice(value...))
111-
for _, name := range f.Names() {
112-
underlyingFlag := f.set.Lookup(name)
113-
if underlyingFlag != nil {
114-
underlyingFlag.Value = &sliceValue
115-
}
130+
if value == nil {
131+
continue
132+
}
133+
var sliceValue = *(cli.NewIntSlice(value...))
134+
for _, n := range f.Names() {
135+
underlyingFlag := f.set.Lookup(n)
136+
if underlyingFlag == nil {
137+
continue
116138
}
139+
underlyingFlag.Value = &sliceValue
117140
}
118141
}
119142
return nil
120143
}
121144

122145
// ApplyInputSourceValue applies a Bool value to the flagSet if required
123146
func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
124-
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.BoolFlag.Name) {
125-
value, err := isc.Bool(f.BoolFlag.Name)
147+
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
148+
return nil
149+
}
150+
for _, name := range f.BoolFlag.Names() {
151+
if !isc.isSet(name) {
152+
continue
153+
}
154+
value, err := isc.Bool(name)
126155
if err != nil {
127156
return err
128157
}
129-
for _, name := range f.Names() {
130-
_ = f.set.Set(name, strconv.FormatBool(value))
158+
for _, n := range f.Names() {
159+
_ = f.set.Set(n, strconv.FormatBool(value))
131160
}
132161
}
133162
return nil
134163
}
135164

136165
// ApplyInputSourceValue applies a String value to the flagSet if required
137166
func (f *StringFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
138-
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.StringFlag.Name) {
139-
value, err := isc.String(f.StringFlag.Name)
167+
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
168+
return nil
169+
}
170+
for _, name := range f.StringFlag.Names() {
171+
if !isc.isSet(name) {
172+
continue
173+
}
174+
value, err := isc.String(name)
140175
if err != nil {
141176
return err
142177
}
143-
if value != "" {
144-
for _, name := range f.Names() {
145-
_ = f.set.Set(name, value)
146-
}
178+
for _, n := range f.Names() {
179+
_ = f.set.Set(n, value)
147180
}
148181
}
149182
return nil
150183
}
151184

152185
// ApplyInputSourceValue applies a Path value to the flagSet if required
153186
func (f *PathFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
154-
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.PathFlag.Name) {
155-
value, err := isc.String(f.PathFlag.Name)
187+
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
188+
return nil
189+
}
190+
for _, name := range f.PathFlag.Names() {
191+
if !isc.isSet(name) {
192+
continue
193+
}
194+
value, err := isc.String(name)
156195
if err != nil {
157196
return err
158197
}
159-
if value != "" {
160-
for _, name := range f.Names() {
161-
162-
if !filepath.IsAbs(value) && isc.Source() != "" {
163-
basePathAbs, err := filepath.Abs(isc.Source())
164-
if err != nil {
165-
return err
166-
}
167-
168-
value = filepath.Join(filepath.Dir(basePathAbs), value)
198+
if value == "" {
199+
continue
200+
}
201+
for _, n := range f.Names() {
202+
if !filepath.IsAbs(value) && isc.Source() != "" {
203+
basePathAbs, err := filepath.Abs(isc.Source())
204+
if err != nil {
205+
return err
169206
}
170-
171-
_ = f.set.Set(name, value)
207+
value = filepath.Join(filepath.Dir(basePathAbs), value)
172208
}
209+
_ = f.set.Set(n, value)
173210
}
174211
}
175212
return nil
176213
}
177214

178215
// ApplyInputSourceValue applies a int value to the flagSet if required
179216
func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
180-
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.IntFlag.Name) {
181-
value, err := isc.Int(f.IntFlag.Name)
217+
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
218+
return nil
219+
}
220+
for _, name := range f.IntFlag.Names() {
221+
if !isc.isSet(name) {
222+
continue
223+
}
224+
value, err := isc.Int(name)
182225
if err != nil {
183226
return err
184227
}
185-
for _, name := range f.Names() {
186-
_ = f.set.Set(name, strconv.FormatInt(int64(value), 10))
228+
for _, n := range f.Names() {
229+
_ = f.set.Set(n, strconv.FormatInt(int64(value), 10))
187230
}
188231
}
189232
return nil
190233
}
191234

192235
// ApplyInputSourceValue applies a Duration value to the flagSet if required
193236
func (f *DurationFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
194-
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.DurationFlag.Name) {
195-
value, err := isc.Duration(f.DurationFlag.Name)
237+
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
238+
return nil
239+
}
240+
for _, name := range f.DurationFlag.Names() {
241+
if !isc.isSet(name) {
242+
continue
243+
}
244+
value, err := isc.Duration(name)
196245
if err != nil {
197246
return err
198247
}
199-
for _, name := range f.Names() {
200-
_ = f.set.Set(name, value.String())
248+
for _, n := range f.Names() {
249+
_ = f.set.Set(n, value.String())
201250
}
202251
}
203252
return nil
204253
}
205254

206255
// ApplyInputSourceValue applies a Float64 value to the flagSet if required
207256
func (f *Float64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
208-
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.Float64Flag.Name) {
209-
value, err := isc.Float64(f.Float64Flag.Name)
257+
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
258+
return nil
259+
}
260+
for _, name := range f.Float64Flag.Names() {
261+
if !isc.isSet(name) {
262+
continue
263+
}
264+
value, err := isc.Float64(name)
210265
if err != nil {
211266
return err
212267
}
213268
floatStr := float64ToString(value)
214-
for _, name := range f.Names() {
215-
_ = f.set.Set(name, floatStr)
269+
for _, n := range f.Names() {
270+
_ = f.set.Set(n, floatStr)
216271
}
217272
}
218273
return nil

0 commit comments

Comments
 (0)