Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1755) Ensure that timestamp flag destination is set correctly #1756

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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