Skip to content

Commit c023d9b

Browse files
authored
Merge pull request #1756 from dearchap/issue_1755
Fix:(issue_1755) Ensure that timestamp flag destination is set correctly
2 parents d988ccc + 7f4dd25 commit c023d9b

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

flag_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,6 +3459,28 @@ func TestTimestampFlagApply_WithDestination(t *testing.T) {
34593459
expect(t, *fl.Destination.timestamp, expectedResult)
34603460
}
34613461

3462+
func TestTimestampFlagApply_EnvWithDestination(t *testing.T) {
3463+
var tsValue Timestamp
3464+
3465+
app := NewApp()
3466+
app.Flags = []Flag{
3467+
&TimestampFlag{
3468+
Name: "some-timestamp-flag",
3469+
EnvVars: []string{"SOME_TIMESTAMP_FLAG"},
3470+
Layout: time.RFC3339,
3471+
Destination: &tsValue,
3472+
Required: true,
3473+
},
3474+
}
3475+
3476+
t.Setenv("SOME_TIMESTAMP_FLAG", "2021-03-02T06:20:00Z")
3477+
3478+
err := app.Run([]string{})
3479+
3480+
expect(t, err, nil)
3481+
expect(t, tsValue.Value().Format(time.RFC3339), "2021-03-02T06:20:00Z")
3482+
}
3483+
34623484
// Test issue #1254
34633485
// StringSlice() with UseShortOptionHandling causes duplicated entries, depending on the ordering of the flags
34643486
func TestSliceShortOptionHandle(t *testing.T) {

flag_timestamp.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,17 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error {
145145

146146
f.defaultValue = f.Value.clone()
147147

148-
if f.Destination != nil {
149-
f.Destination.SetLayout(f.Layout)
150-
f.Destination.SetLocation(f.Timezone)
151-
}
152-
153148
if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
154149
if err := f.Value.Set(val); err != nil {
155150
return fmt.Errorf("could not parse %q as timestamp value from %s for flag %s: %s", val, source, f.Name, err)
156151
}
157152
f.HasBeenSet = true
158153
}
159154

155+
if f.Destination != nil {
156+
*f.Destination = *f.Value
157+
}
158+
160159
for _, name := range f.Names() {
161160
if f.Destination != nil {
162161
set.Var(f.Destination, name, f.Usage)

0 commit comments

Comments
 (0)