Skip to content
Merged
Prev Previous commit
Next Next commit
feat(cli): call codersk to get default token lifetime
  • Loading branch information
defelmnq committed Oct 10, 2024
commit 617fda1cbb833593b1ea251d3c174967ef9d6100
19 changes: 15 additions & 4 deletions cli/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,21 @@ func (r *RootCmd) createToken() *serpent.Command {
userID = user
}

parsedLifetime, err := extendedParseDuration(tokenLifetime)
if err != nil {
return xerrors.Errorf("parse lifetime: %w", err)
var parsedLifetime time.Duration
var err error

if tokenLifetime == "" {
tokenConfig, err := client.GetTokenConfig(inv.Context(), userID)
if err != nil {
return xerrors.Errorf("get token config: %w", err)
}

parsedLifetime = tokenConfig.MaxTokenLifetime
} else {
parsedLifetime, err = extendedParseDuration(tokenLifetime)
if err != nil {
return xerrors.Errorf("parse lifetime: %w", err)
}
Comment on lines +78 to +81
Copy link
Member

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 than MaxTokenLifetime client-side. This may not be completely necessary though, as the API will disallow creating tokens with lifetime greater than MaximumTokenDuration. Dealer's choice!

Copy link
Contributor Author

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 ! 👌

}

res, err := client.CreateToken(inv.Context(), userID, codersdk.CreateTokenRequest{
Expand All @@ -88,7 +100,6 @@ 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.StringOf(&tokenLifetime),
},
{
Expand Down
Loading