Skip to content

Commit 880a802

Browse files
authored
Merge pull request #1495 from dearchap/issue_1197
Fix:(issue_1197) Set destination field from altsrc for slice flags
2 parents 9120e06 + c8dc60b commit 880a802

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

altsrc/flag.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ func (f *StringSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSour
110110
}
111111
underlyingFlag.Value = &sliceValue
112112
}
113+
if f.Destination != nil {
114+
f.Destination.Set(sliceValue.Serialize())
115+
}
113116
}
114117
return nil
115118
}
@@ -138,6 +141,9 @@ func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC
138141
}
139142
underlyingFlag.Value = &sliceValue
140143
}
144+
if f.Destination != nil {
145+
f.Destination.Set(sliceValue.Serialize())
146+
}
141147
}
142148
return nil
143149
}

altsrc/flag_test.go

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,39 +100,61 @@ func TestGenericApplyInputSourceMethodEnvVarSet(t *testing.T) {
100100
}
101101

102102
func TestStringSliceApplyInputSourceValue_Alias(t *testing.T) {
103+
dest := cli.NewStringSlice()
103104
tis := testApplyInputSource{
104-
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Aliases: []string{"test_alias"}}),
105+
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Aliases: []string{"test_alias"}, Destination: dest}),
105106
FlagName: "test_alias",
106107
MapValue: []interface{}{"hello", "world"},
107108
}
108109
c := runTest(t, tis)
109110
expect(t, c.StringSlice("test_alias"), []string{"hello", "world"})
111+
expect(t, dest.Value(), []string{"hello", "world"})
110112

113+
// reset dest
114+
dest = cli.NewStringSlice()
115+
tis = testApplyInputSource{
116+
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Aliases: []string{"test_alias"}, Destination: dest}),
117+
FlagName: "test_alias",
118+
MapValue: []interface{}{"hello", "world"},
119+
}
111120
c = runRacyTest(t, tis)
112121
refute(t, c.StringSlice("test_alias"), []string{"hello", "world"})
122+
refute(t, dest.Value(), []string{"hello", "world"})
113123
}
114124

115125
func TestStringSliceApplyInputSourceValue(t *testing.T) {
126+
dest := cli.NewStringSlice()
116127
tis := testApplyInputSource{
117-
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test"}),
128+
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Destination: dest}),
118129
FlagName: "test",
119130
MapValue: []interface{}{"hello", "world"},
120131
}
121132
c := runTest(t, tis)
122133
expect(t, c.StringSlice("test"), []string{"hello", "world"})
134+
expect(t, dest.Value(), []string{"hello", "world"})
123135

136+
// reset dest
137+
dest = cli.NewStringSlice()
138+
tis = testApplyInputSource{
139+
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Destination: dest}),
140+
FlagName: "test",
141+
MapValue: []interface{}{"hello", "world"},
142+
}
124143
c = runRacyTest(t, tis)
125144
refute(t, c.StringSlice("test"), []string{"hello", "world"})
145+
refute(t, dest.Value(), []string{"hello", "world"})
126146
}
127147

128148
func TestStringSliceApplyInputSourceMethodContextSet(t *testing.T) {
149+
dest := cli.NewStringSlice()
129150
c := runTest(t, testApplyInputSource{
130-
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test"}),
151+
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Destination: dest}),
131152
FlagName: "test",
132153
MapValue: []interface{}{"hello", "world"},
133154
ContextValueString: "ohno",
134155
})
135156
expect(t, c.StringSlice("test"), []string{"ohno"})
157+
expect(t, dest.Value(), []string{"ohno"})
136158
}
137159

138160
func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
@@ -151,43 +173,73 @@ func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
151173
}
152174

153175
func TestIntSliceApplyInputSourceValue_Alias(t *testing.T) {
176+
dest := cli.NewIntSlice()
154177
tis := testApplyInputSource{
155-
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Aliases: []string{"test_alias"}}),
178+
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Aliases: []string{"test_alias"}, Destination: dest}),
156179
FlagName: "test_alias",
157180
MapValue: []interface{}{1, 2},
158181
}
159182
c := runTest(t, tis)
160183
expect(t, c.IntSlice("test_alias"), []int{1, 2})
184+
expect(t, dest.Value(), []int{1, 2})
161185

186+
dest = cli.NewIntSlice()
187+
tis = testApplyInputSource{
188+
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Aliases: []string{"test_alias"}, Destination: dest}),
189+
FlagName: "test_alias",
190+
MapValue: []interface{}{1, 2},
191+
}
162192
c = runRacyTest(t, tis)
163193
refute(t, c.IntSlice("test_alias"), []int{1, 2})
194+
refute(t, dest.Value(), []int{1, 2})
164195
}
165196

166197
func TestIntSliceApplyInputSourceValue(t *testing.T) {
198+
dest := cli.NewIntSlice()
167199
tis := testApplyInputSource{
168-
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test"}),
200+
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Destination: dest}),
169201
FlagName: "test",
170202
MapValue: []interface{}{1, 2},
171203
}
172204
c := runTest(t, tis)
173205
expect(t, c.IntSlice("test"), []int{1, 2})
206+
expect(t, dest.Value(), []int{1, 2})
174207

208+
// reset dest
209+
dest = cli.NewIntSlice()
210+
tis = testApplyInputSource{
211+
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Destination: dest}),
212+
FlagName: "test",
213+
MapValue: []interface{}{1, 2},
214+
}
175215
c = runRacyTest(t, tis)
176216
refute(t, c.IntSlice("test"), []int{1, 2})
217+
refute(t, dest.Value(), []int{1, 2})
177218
}
178219

179220
func TestIntSliceApplyInputSourceMethodContextSet(t *testing.T) {
221+
dest := cli.NewIntSlice()
180222
tis := testApplyInputSource{
181-
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test"}),
223+
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Destination: dest}),
182224
FlagName: "test",
183225
MapValue: []interface{}{1, 2},
184226
ContextValueString: "3",
185227
}
186228
c := runTest(t, tis)
187229
expect(t, c.IntSlice("test"), []int{3})
230+
expect(t, dest.Value(), []int{3})
188231

232+
// reset dest
233+
dest = cli.NewIntSlice()
234+
tis = testApplyInputSource{
235+
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Destination: dest}),
236+
FlagName: "test",
237+
MapValue: []interface{}{1, 2},
238+
ContextValueString: "3",
239+
}
189240
c = runRacyTest(t, tis)
190241
refute(t, c.IntSlice("test"), []int{3})
242+
refute(t, dest.Value(), []int{3})
191243
}
192244

193245
func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {

altsrc/yaml_file_loader.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ func NewYamlSourceFromFile(file string) (InputSourceContext, error) {
3333
// NewYamlSourceFromFlagFunc creates a new Yaml InputSourceContext from a provided flag name and source context.
3434
func NewYamlSourceFromFlagFunc(flagFileName string) func(cCtx *cli.Context) (InputSourceContext, error) {
3535
return func(cCtx *cli.Context) (InputSourceContext, error) {
36-
if cCtx.IsSet(flagFileName) {
37-
filePath := cCtx.String(flagFileName)
36+
if filePath := cCtx.String(flagFileName); filePath != "" {
3837
return NewYamlSourceFromFile(filePath)
3938
}
40-
4139
return defaultInputSource()
4240
}
4341
}

0 commit comments

Comments
 (0)