From 7829bdfe10bcf4f518a2a6ac212a3dee7178e1ca Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Fri, 6 Dec 2024 15:33:06 +1100 Subject: [PATCH] fix: force token refresh with lagoon login (#409) --- cmd/login.go | 26 ++++++++++++++++++++------ docs/commands/lagoon.md | 2 +- docs/commands/lagoon_login.md | 10 ++++++++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/cmd/login.go b/cmd/login.go index 5aa16b44..af3132b7 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -16,12 +16,22 @@ import ( ) var loginCmd = &cobra.Command{ - Use: "login", - Short: "Log into a Lagoon instance", - Aliases: []string{"l"}, - Run: func(cmd *cobra.Command, args []string) { - validateToken(lagoonCLIConfig.Current) // get a new token if the current one is invalid - fmt.Println("Token fetched and saved.") + Use: "login", + Short: "Login or refresh an authentication token", + Long: `Login or refresh an authentication token +This can be used to clear out and force your CLI to refresh a token for a given context.`, + Aliases: []string{"token-refresh", "tr", "l"}, + RunE: func(cmd *cobra.Command, args []string) error { + // clear the current token + lc := lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current] + lc.Token = "" + lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current] = lc + // refresh and save new token + if err := validateTokenE(lagoonCLIConfig.Current); err != nil { + return err + } + fmt.Printf("token for context %s fetched and saved.\n", lagoonCLIConfig.Current) + return nil }, } @@ -181,3 +191,7 @@ func retrieveTokenViaSsh() (string, error) { } return strings.TrimSpace(string(out)), err } + +func init() { + loginCmd.Flags().Bool("reset-token", false, "clear the token before attempting to log in") +} diff --git a/docs/commands/lagoon.md b/docs/commands/lagoon.md index b2541563..4b152d8b 100644 --- a/docs/commands/lagoon.md +++ b/docs/commands/lagoon.md @@ -45,7 +45,7 @@ lagoon [flags] * [lagoon import](lagoon_import.md) - Import a config from a yaml file * [lagoon kibana](lagoon_kibana.md) - Launch the kibana interface * [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications -* [lagoon login](lagoon_login.md) - Log into a Lagoon instance +* [lagoon login](lagoon_login.md) - Login or refresh an authentication token * [lagoon logs](lagoon_logs.md) - Display logs for a service of an environment and project * [lagoon raw](lagoon_raw.md) - Run a custom query or mutation * [lagoon reset-password](lagoon_reset-password.md) - Send a password reset email diff --git a/docs/commands/lagoon_login.md b/docs/commands/lagoon_login.md index 140d66ec..a5450115 100644 --- a/docs/commands/lagoon_login.md +++ b/docs/commands/lagoon_login.md @@ -1,6 +1,11 @@ ## lagoon login -Log into a Lagoon instance +Login or refresh an authentication token + +### Synopsis + +Login or refresh an authentication token +This can be used to clear out and force your CLI to refresh a token for a given context. ``` lagoon login [flags] @@ -9,7 +14,8 @@ lagoon login [flags] ### Options ``` - -h, --help help for login + -h, --help help for login + --reset-token clear the token before attempting to log in ``` ### Options inherited from parent commands