From 6a98b6bd3ca7a3f525151f17424cfc4e50530763 Mon Sep 17 00:00:00 2001 From: Jens Feodor Nielsen Date: Tue, 11 Jun 2024 09:50:00 +0200 Subject: [PATCH] fix(cli): `login` and `logout` output improvements * Display errors using functions from `output`. * Add `output.PrintlnOK` for printing affirmative messages. * Use non-emoji symbols with ansi coloring. --- cli/cmd/login/login.go | 21 ++++++++++----------- cli/cmd/logout/logout.go | 3 +-- cli/cmd/output/ok.go | 9 +++++++++ 3 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 cli/cmd/output/ok.go diff --git a/cli/cmd/login/login.go b/cli/cmd/login/login.go index bf6bc0c..b3d893d 100644 --- a/cli/cmd/login/login.go +++ b/cli/cmd/login/login.go @@ -3,7 +3,6 @@ package login import ( "context" "fmt" - "log" "net/http" "os" @@ -22,7 +21,7 @@ var LoginCmd = &cobra.Command{ if user == nil { Login(auth.NumerousTenantAuthenticator, cmd.Context()) } else { - fmt.Println("✅ Great, you are already logged in!") + output.PrintlnOK("Great, you are already logged in!") } }, } @@ -30,7 +29,8 @@ var LoginCmd = &cobra.Command{ func Login(a auth.Authenticator, ctx context.Context) *auth.User { state, err := a.GetDeviceCode(ctx, http.DefaultClient) if err != nil { - log.Fatal(err) + output.PrintErrorDetails("Error getting device code", err) + os.Exit(1) } fmt.Printf(`You are logging into Numerous. @@ -46,7 +46,8 @@ Press Enter to continue... `, state.UserCode) if _, err = fmt.Scanln(); err != nil { - log.Fatal(err) + output.PrintErrorDetails("Error getting keystroke", err) + os.Exit(1) } if err := a.OpenURL(state.VerificationURI); err != nil { @@ -58,17 +59,15 @@ Press Enter to continue... result, err := a.WaitUntilUserLogsIn(ctx, http.DefaultClient, state) if err != nil { - log.Fatal(err) + output.PrintErrorDetails("Error waiting for login", err) + os.Exit(1) } if err := a.StoreAccessToken(result.AccessToken); err != nil { - output.PrintError( - "Login failed.", - "Error occurred storing access token in your keyring.\nError details: %s", - err.Error(), - ) + output.PrintErrorDetails("Login failed. Could not store credentials in keyring.", err) os.Exit(1) } + if err := a.StoreRefreshToken(result.RefreshToken); err != nil { output.PrintError( "Error occurred storing refresh token in your keyring.", @@ -77,7 +76,7 @@ Press Enter to continue... ) } - fmt.Println("🎉 You are now logged in to Numerous!") + output.PrintlnOK("You are now logged in to Numerous!") return a.GetLoggedInUserFromKeyring() } diff --git a/cli/cmd/logout/logout.go b/cli/cmd/logout/logout.go index 2ee7d2e..4d9523d 100644 --- a/cli/cmd/logout/logout.go +++ b/cli/cmd/logout/logout.go @@ -1,7 +1,6 @@ package logout import ( - "fmt" "net/http" "os" @@ -34,7 +33,7 @@ func logout(a auth.Authenticator) error { output.PrintUnknownError(err) return err } - fmt.Println("✅ Successfully logged out!") + output.PrintlnOK("You are now logged out.") return nil } diff --git a/cli/cmd/output/ok.go b/cli/cmd/output/ok.go new file mode 100644 index 0000000..e3eedbd --- /dev/null +++ b/cli/cmd/output/ok.go @@ -0,0 +1,9 @@ +package output + +import "fmt" + +// Prints message as a line prefixed with a green checkmark. Additional +// arguments are used for formatting. +func PrintlnOK(message string, args ...any) { + fmt.Printf(ansiGreen+checkmark+ansiReset+" "+message+"\n", args...) +}