- Notifications
You must be signed in to change notification settings - Fork927
feat(cli): extend duration to longer units#15040
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
cli/util_internal_test.go Outdated
{"10s", 10 * time.Second, true}, | ||
{"1m", 1 * time.Minute, true}, | ||
{"20h", 20 * time.Hour, true}, | ||
{"10y10d10s", 0, false}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I think users will expect durations like 1y1d to work here. It shouldn't be a huge lift to enable this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Hey@johnstcn I tried to play with time.ParseDuration golang function to mimic the behavior as much as possible - and changed to indeed modify what you've raised.
matifali commentedOct 14, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
This is excellent@defelmnq. I suggest we stay uniform and check and update all durations of input throughput the product to support the string format across CLI/API/frontend. It doesn't have to be in this PR and can be tracked as a separate issue. |
cli/util.go Outdated
// - d (days) | ||
// - y (years) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
It may make sense to explicitly note that this will not take into account things like DST or leap years or all that other assorted wibbly wobbley timey wimey stuff.
// - d (days) | |
// - y (years) | |
// - d (days, interpreted as 24 hours) | |
// - y (years, interpreted as 8_760 hours) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
My first though was about leap years too :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Yep - indeed we can document it. About the behavior are we good to keep it this way ? I felt like it can quickly become complicated with time saving, leap years etc.. otherwise - maybe we can do it in the follow-up PR in serpent if we want to use it everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Yeah, definitely not a must-have
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I'm happy with a naive definition of days and years here. Adding DST / leap year awareness adds a lot of complications, as you need both a timezone and a relative time. We can add a follow-up issue if we think it's necessary though.
{"1d", 24 * time.Hour, true}, | ||
{"1y", 365 * 24 * time.Hour, true}, | ||
{"10s", 10 * time.Second, true}, | ||
{"1m", 1 * time.Minute, true}, | ||
{"20h", 20 * time.Hour, true}, | ||
{"10y10d10s", 10*365*24*time.Hour + 10*24*time.Hour + 10*time.Second, true}, | ||
{"10ms", 10 * time.Millisecond, true}, | ||
{"5y10d10s5y2ms8ms", 10*365*24*time.Hour + 10*24*time.Hour + 10*time.Second + 10*time.Millisecond, true}, | ||
{"10yz10d10s", 0, false}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
question: What happens if we try to parse a duration greater than2540400h10m10.000000000s
(e.g.1000y
)?
(see:https://github.com/golang/go/blob/master/src/time/time.go#L954C21-L954C45)
parsedLifetime, err = extendedParseDuration(tokenLifetime) | ||
if err != nil { | ||
return xerrors.Errorf("parse lifetime: %w", err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
suggestion: given that we have the token config available, we can check if the requested lifetime is greater thanMaxTokenLifetime
client-side. This may not be completely necessary though, as the API will disallow creating tokens with lifetime greater thanMaximumTokenDuration
. Dealer's choice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Yeah you're completely right - makes sense I just changed it a bit to handle the config fetched ! 👌
Uh oh!
There was an error while loading.Please reload this page.
@@ -46,7 +46,7 @@ func (r *RootCmd) tokens() *serpent.Command { | |||
func (r *RootCmd) createToken() *serpent.Command { | |||
var ( | |||
tokenLifetimetime.Duration | |||
tokenLifetimestring |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@defelmnq@johnstcn WDYT about defining a new type incoder/serpent,time.LongDuration
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Could be done as a follow-up!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I created afollow-up issue here - would definitely be a cool one to do indeed.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
cli/util.go Outdated
// - d (days) | ||
// - y (years) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
My first though was about leap years too :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Hey@mtojek@johnstcn thanks a lot for the input here - really appreciate. Since the last review, here are globally the points I tried to change and improve :
What I would love to do as a follow-up :
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Strong tests 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Nice work! 👍
ccbb687
intomainUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
This PR is a proposal to improve the situation described in#14750
For some precise commands - we would like to be able to use durations bigger than hours, minutes..
This PR extends the Duration proposed by Go with :
d
- a day or 24hours.y
- a year or 365 days.I also removed the default value for lifetime and instead fetch the maxLifetime value from codersdk - so by default if no value set we use the value defined in the config.