diff options
author | Øyvind Grønnesby <oyving@yahooinc.com> | 2022-03-29 09:58:17 +0200 |
---|---|---|
committer | Øyvind Grønnesby <oyving@yahooinc.com> | 2022-03-29 09:58:17 +0200 |
commit | 4fdab3816bccfcf43d7f8a0d253d8da50b0bbc3e (patch) | |
tree | 33c287f635a3a9d41a9534fa5b5b86347065c418 /client | |
parent | 0596a4f21e7278ebfc54095e9401dc45c252f0ab (diff) |
Ask if user wants login link in browser opened
Diffstat (limited to 'client')
-rw-r--r-- | client/go/cmd/login.go | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/client/go/cmd/login.go b/client/go/cmd/login.go index 5cf471ed8db..4067a2221c9 100644 --- a/client/go/cmd/login.go +++ b/client/go/cmd/login.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "strings" "time" "github.com/pkg/browser" @@ -43,18 +44,18 @@ func newLoginCmd(cli *CLI) *cobra.Command { return fmt.Errorf("could not start the authentication process: %w", err) } - log.Printf("Your Device Confirmation code is: %s\n\n", state.UserCode) + log.Printf("Your Device Confirmation code is: %s\n", state.UserCode) - log.Println("If you prefer, you can open the URL directly for verification") - log.Printf("Your Verification URL: %s\n\n", state.VerificationURI) + auto_open := confirm("Allow Vespa CLI to open confirmation page in your default browser?") - log.Println("Press Enter to open the browser to log in or ^C to quit...") - fmt.Scanln() - - err = browser.OpenURL(state.VerificationURI) - - if err != nil { - log.Printf("Couldn't open the URL, please do it manually: %s.", state.VerificationURI) + if auto_open { + log.Printf("Opened link in your browser: %s\n", state.VerificationURI) + err = browser.OpenURL(state.VerificationURI) + if err != nil { + log.Println("Couldn't open the URL, please do it manually") + } + } else { + log.Printf("Please open link in your browser: %s\n", state.VerificationURI) } var res auth.Result @@ -93,3 +94,22 @@ func newLoginCmd(cli *CLI) *cobra.Command { }, } } + +func confirm(question string) bool { + for { + var answer string + + log.Printf("%s [Y/n] ", question) + fmt.Scanln(&answer) + + answer = strings.TrimSpace(strings.ToLower(answer)) + + if answer == "y" || answer == "" { + return true + } else if answer == "n" { + return false + } else { + log.Printf("Please answer Y or N.\n") + } + } +} |