mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat(cli): extend duration to longer units (#15040)
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.
This commit is contained in:
+25
-4
@@ -46,7 +46,7 @@ func (r *RootCmd) tokens() *serpent.Command {
|
||||
|
||||
func (r *RootCmd) createToken() *serpent.Command {
|
||||
var (
|
||||
tokenLifetime time.Duration
|
||||
tokenLifetime string
|
||||
name string
|
||||
user string
|
||||
)
|
||||
@@ -63,8 +63,30 @@ func (r *RootCmd) createToken() *serpent.Command {
|
||||
if user != "" {
|
||||
userID = user
|
||||
}
|
||||
|
||||
var parsedLifetime time.Duration
|
||||
var err error
|
||||
|
||||
tokenConfig, err := client.GetTokenConfig(inv.Context(), userID)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get token config: %w", err)
|
||||
}
|
||||
|
||||
if tokenLifetime == "" {
|
||||
parsedLifetime = tokenConfig.MaxTokenLifetime
|
||||
} else {
|
||||
parsedLifetime, err = extendedParseDuration(tokenLifetime)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("parse lifetime: %w", err)
|
||||
}
|
||||
|
||||
if parsedLifetime > tokenConfig.MaxTokenLifetime {
|
||||
return xerrors.Errorf("lifetime (%s) is greater than the maximum allowed lifetime (%s)", parsedLifetime, tokenConfig.MaxTokenLifetime)
|
||||
}
|
||||
}
|
||||
|
||||
res, err := client.CreateToken(inv.Context(), userID, codersdk.CreateTokenRequest{
|
||||
Lifetime: tokenLifetime,
|
||||
Lifetime: parsedLifetime,
|
||||
TokenName: name,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -82,8 +104,7 @@ func (r *RootCmd) createToken() *serpent.Command {
|
||||
Flag: "lifetime",
|
||||
Env: "CODER_TOKEN_LIFETIME",
|
||||
Description: "Specify a duration for the lifetime of the token.",
|
||||
Default: (time.Hour * 24 * 30).String(),
|
||||
Value: serpent.DurationOf(&tokenLifetime),
|
||||
Value: serpent.StringOf(&tokenLifetime),
|
||||
},
|
||||
{
|
||||
Flag: "name",
|
||||
|
||||
Reference in New Issue
Block a user