From 2ae03fa69ce7b6030174af976b8d51d24386394c Mon Sep 17 00:00:00 2001 From: Alex Couture-Beil Date: Mon, 24 Aug 2020 13:59:26 -0700 Subject: [PATCH] bugfix: don't overwrite existing stringslice refence fixes an issue where values set by environment var are not saved to existing stringslice reference. --- flag_string_slice.go | 4 +++- flag_test.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/flag_string_slice.go b/flag_string_slice.go index 74cf0a51f2..35497032cb 100644 --- a/flag_string_slice.go +++ b/flag_string_slice.go @@ -124,7 +124,9 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error { } if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { - f.Value = &StringSlice{} + if f.Value == nil { + f.Value = &StringSlice{} + } destination := f.Value if f.Destination != nil { destination = f.Destination diff --git a/flag_test.go b/flag_test.go index ced5514714..b1fe70a4fd 100644 --- a/flag_test.go +++ b/flag_test.go @@ -386,6 +386,20 @@ func TestStringSliceFlagApply_SetsAllNames(t *testing.T) { expect(t, err, nil) } +func TestStringSliceFlagApply_UsesEnvValues(t *testing.T) { + defer resetEnv(os.Environ()) + os.Clearenv() + _ = os.Setenv("MY_GOAT", "vincent van goat,scape goat") + var val StringSlice + fl := StringSliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: &val} + set := flag.NewFlagSet("test", 0) + _ = fl.Apply(set) + + err := set.Parse(nil) + expect(t, err, nil) + expect(t, val.Value(), NewStringSlice("vincent van goat", "scape goat").Value()) +} + func TestStringSliceFlagApply_DefaultValueWithDestination(t *testing.T) { defValue := []string{"UA", "US"}