From 7ecfd216b14542e1bec159d58d5d7af992969bf8 Mon Sep 17 00:00:00 2001 From: Vinicius Schettino Date: Sat, 11 Jul 2020 15:54:46 -0300 Subject: [PATCH 1/2] Verify whether there is a Value before overriding --- flag_timestamp.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/flag_timestamp.go b/flag_timestamp.go index 9fac1d1e26..0382a6b9dc 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -118,7 +118,9 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error { if f.Layout == "" { return fmt.Errorf("timestamp Layout is required") } - f.Value = &Timestamp{} + if f.Value == nil { + f.Value = &Timestamp{} + } f.Value.SetLayout(f.Layout) if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { From 54fbe0a445de822f35335d30706105fc0af73fa0 Mon Sep 17 00:00:00 2001 From: Vinicius Schettino Date: Sat, 11 Jul 2020 15:55:19 -0300 Subject: [PATCH 2/2] Basic test case to check if Value is propagated as it should --- flag_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/flag_test.go b/flag_test.go index 7dbfbdc5bd..ced5514714 100644 --- a/flag_test.go +++ b/flag_test.go @@ -1836,6 +1836,17 @@ func TestTimestampFlagApply(t *testing.T) { expect(t, *fl.Value.timestamp, expectedResult) } +func TestTimestampFlagApplyValue(t *testing.T) { + expectedResult, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") + fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: time.RFC3339, Value: NewTimestamp(expectedResult)} + set := flag.NewFlagSet("test", 0) + _ = fl.Apply(set) + + err := set.Parse([]string{""}) + expect(t, err, nil) + expect(t, *fl.Value.timestamp, expectedResult) +} + func TestTimestampFlagApply_Fail_Parse_Wrong_Layout(t *testing.T) { fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: "randomlayout"} set := flag.NewFlagSet("test", 0)