Skip to content

Commit

Permalink
fix(cli): login and logout output improvements
Browse files Browse the repository at this point in the history
* Display errors using functions from `output`.
* Add `output.PrintlnOK` for printing affirmative messages.
* Use non-emoji symbols with ansi coloring.
  • Loading branch information
jfeodor committed Jun 11, 2024
1 parent 3102a30 commit 6a98b6b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
21 changes: 10 additions & 11 deletions cli/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package login
import (
"context"
"fmt"
"log"
"net/http"
"os"

Expand All @@ -22,15 +21,16 @@ 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!")
}
},
}

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.
Expand All @@ -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 {
Expand All @@ -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.",
Expand All @@ -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()
}
Expand Down
3 changes: 1 addition & 2 deletions cli/cmd/logout/logout.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package logout

import (
"fmt"
"net/http"
"os"

Expand Down Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions cli/cmd/output/ok.go
Original file line number Diff line number Diff line change
@@ -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...)
}

0 comments on commit 6a98b6b

Please sign in to comment.