From 91d2adc13442177cec7556a57dc0d4def2c0d550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 20 Nov 2020 19:36:26 +0100 Subject: [PATCH] Avoid re-requesting username if we already have it --- pkg/cmd/auth/login/login.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/auth/login/login.go b/pkg/cmd/auth/login/login.go index e6f9db9b5..4246d5d8b 100644 --- a/pkg/cmd/auth/login/login.go +++ b/pkg/cmd/auth/login/login.go @@ -229,11 +229,13 @@ func loginRun(opts *LoginOptions) error { } } + userValidated := false if authMode == 0 { _, err := authflow.AuthFlowWithConfig(cfg, opts.IO, hostname, "", opts.Scopes) if err != nil { return fmt.Errorf("failed to authenticate via web browser: %w", err) } + userValidated = true } else { fmt.Fprintln(opts.IO.ErrOut) fmt.Fprintln(opts.IO.ErrOut, heredoc.Doc(getAccessTokenTip(hostname))) @@ -286,19 +288,24 @@ func loginRun(opts *LoginOptions) error { fmt.Fprintf(opts.IO.ErrOut, "%s Configured git protocol\n", cs.SuccessIcon()) } - apiClient, err := client.ClientFromCfg(hostname, cfg) - if err != nil { - return err - } + var username string + if userValidated { + username, _ = cfg.Get(hostname, "user") + } else { + apiClient, err := client.ClientFromCfg(hostname, cfg) + if err != nil { + return err + } - username, err := api.CurrentLoginName(apiClient, hostname) - if err != nil { - return fmt.Errorf("error using api: %w", err) - } + username, err = api.CurrentLoginName(apiClient, hostname) + if err != nil { + return fmt.Errorf("error using api: %w", err) + } - err = cfg.Set(hostname, "user", username) - if err != nil { - return err + err = cfg.Set(hostname, "user", username) + if err != nil { + return err + } } err = cfg.Write()