Skip to content

Commit

Permalink
Fix:(issue_1755) Ensure that timestamp flag destination is set correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Jun 13, 2023
1 parent d988ccc commit 7f4dd25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
22 changes: 22 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3459,6 +3459,28 @@ func TestTimestampFlagApply_WithDestination(t *testing.T) {
expect(t, *fl.Destination.timestamp, expectedResult)
}

func TestTimestampFlagApply_EnvWithDestination(t *testing.T) {
var tsValue Timestamp

app := NewApp()
app.Flags = []Flag{
&TimestampFlag{
Name: "some-timestamp-flag",
EnvVars: []string{"SOME_TIMESTAMP_FLAG"},
Layout: time.RFC3339,
Destination: &tsValue,
Required: true,
},
}

t.Setenv("SOME_TIMESTAMP_FLAG", "2021-03-02T06:20:00Z")

err := app.Run([]string{})

expect(t, err, nil)
expect(t, tsValue.Value().Format(time.RFC3339), "2021-03-02T06:20:00Z")
}

// Test issue #1254
// StringSlice() with UseShortOptionHandling causes duplicated entries, depending on the ordering of the flags
func TestSliceShortOptionHandle(t *testing.T) {
Expand Down
9 changes: 4 additions & 5 deletions flag_timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,17 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error {

f.defaultValue = f.Value.clone()

if f.Destination != nil {
f.Destination.SetLayout(f.Layout)
f.Destination.SetLocation(f.Timezone)
}

if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
if err := f.Value.Set(val); err != nil {
return fmt.Errorf("could not parse %q as timestamp value from %s for flag %s: %s", val, source, f.Name, err)
}
f.HasBeenSet = true
}

if f.Destination != nil {
*f.Destination = *f.Value
}

for _, name := range f.Names() {
if f.Destination != nil {
set.Var(f.Destination, name, f.Usage)
Expand Down

0 comments on commit 7f4dd25

Please sign in to comment.