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

Bugfix: Retention period is being sent to the API as 0 #5706

Merged
merged 6 commits into from
May 25, 2022

Conversation

greggroth
Copy link
Contributor

The initialized value of time.Duration is 0. There is support for nil retention period at the API interface, but we missed support for this at the parameters. Thus, the API request is being made with 0 as the retention period.

This PR switches the argument type to a string and treats an empty string as a no-retention-period scenario and attempts to parse the string as a duration if the string is non-empty.

It was failing with 0 context about which particular scenario failed and
it wasn't logging the errors returned by the scenarios
@greggroth greggroth requested a review from a team as a code owner May 24, 2022 19:03
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label May 24, 2022
@cliAutomation
Copy link
Collaborator

Hi! Thanks for the pull request. Please ensure that this change is linked to an issue by mentioning an issue number in the description of the pull request. If this pull request would close the issue, please put the word 'Fixes' before the issue number somewhere in the pull request body. If this is a tiny change like fixing a typo, feel free to ignore this message.

@@ -32,7 +32,7 @@ type createOptions struct {
permissionsOptOut bool
devContainerPath string
idleTimeout time.Duration
retentionPeriod time.Duration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you use a *time.Duration instead? using pointers is a common pattern for optional types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I can because the default argument of DurationVar from the pflag library is of type time.Duration so I can't pass in nil. I'm open to suggestions if you have an idea of how this could be refactored.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true but the library provides what you need to define flag funcs for your own types: https://pkg.go.dev/github.com/spf13/pflag#Var

https://pkg.go.dev/github.com/spf13/pflag#Value

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's how we approach *string and *bool flag value receivers in the codebase already:

cli/pkg/cmdutil/flags.go

Lines 12 to 16 in ec12d27

// NilStringFlag defines a new flag with a string pointer receiver. This is useful for differentiating
// between the flag being set to a blank value and the flag not being passed at all.
func NilStringFlag(cmd *cobra.Command, p **string, name string, shorthand string, usage string) *pflag.Flag {
return cmd.Flags().VarPF(newStringValue(p), name, shorthand, usage)
}

While a pointer receiver is a valid way of differentiating between explicit empty value vs. flag not being set by the user, @reybard makes a good point that a custom flag receiver can be defined. If dealing with pointers proves to be convoluted, do not hesitate to make a custom value that wraps an underlying time.Duration value in an object that also keeps track of whether the flag was explicitly set or not. 👍

@greggroth greggroth force-pushed the gregg/zero-retention branch 2 times, most recently from a3124a7 to ac42656 Compare May 24, 2022 19:25
I think this makes parsing the duration from it and supporting a
nil-duration easier.
Copy link
Contributor

@reybard reybard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very gopher to use the interface :shipit: 👍

pkg/cmd/codespace/create.go Outdated Show resolved Hide resolved
return nil
}

func (d *NullableDuration) Type() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's part of the Value interface

pkg/cmd/codespace/create.go Outdated Show resolved Hide resolved
@mislav mislav merged commit 77507cd into cli:trunk May 25, 2022
@greggroth greggroth deleted the gregg/zero-retention branch May 25, 2022 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external pull request originating outside of the CLI core team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants