From 7006d3f0a59abe23546757d3dd571be181ac045e Mon Sep 17 00:00:00 2001 From: vaindil Date: Fri, 31 Mar 2023 13:47:25 -0400 Subject: [PATCH] move SSH key errors out of upload function --- pkg/cmd/auth/shared/login_flow.go | 15 ++++++++----- pkg/cmd/ssh-key/add/add.go | 12 +++++++++-- pkg/cmd/ssh-key/add/add_test.go | 4 ++-- pkg/cmd/ssh-key/add/http.go | 35 ++++++++++++------------------- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/pkg/cmd/auth/shared/login_flow.go b/pkg/cmd/auth/shared/login_flow.go index 4f2cb9e0c..7c2ff1639 100644 --- a/pkg/cmd/auth/shared/login_flow.go +++ b/pkg/cmd/auth/shared/login_flow.go @@ -200,11 +200,16 @@ func Login(opts *LoginOptions) error { } if keyToUpload != "" { - err := sshKeyUpload(httpClient, hostname, keyToUpload, keyTitle, opts.IO) + uploaded, err := sshKeyUpload(httpClient, hostname, keyToUpload, keyTitle) if err != nil { return err } - fmt.Fprintf(opts.IO.ErrOut, "%s Uploaded the SSH key to your GitHub account: %s\n", cs.SuccessIcon(), cs.Bold(keyToUpload)) + + if uploaded { + fmt.Fprintf(opts.IO.ErrOut, "%s Uploaded the SSH key to your GitHub account: %s\n", cs.SuccessIcon(), cs.Bold(keyToUpload)) + } else { + fmt.Fprintf(opts.IO.ErrOut, "%s SSH key already existed on your GitHub account: %s\n", cs.SuccessIcon(), cs.Bold(keyToUpload)) + } } fmt.Fprintf(opts.IO.ErrOut, "%s Logged in as %s\n", cs.SuccessIcon(), cs.Bold(username)) @@ -223,14 +228,14 @@ func scopesSentence(scopes []string, isEnterprise bool) string { return strings.Join(quoted, ", ") } -func sshKeyUpload(httpClient *http.Client, hostname, keyFile string, title string, ios *iostreams.IOStreams) error { +func sshKeyUpload(httpClient *http.Client, hostname, keyFile string, title string) (bool, error) { f, err := os.Open(keyFile) if err != nil { - return err + return false, err } defer f.Close() - return add.SSHKeyUpload(httpClient, hostname, f, title, false, ios) + return add.SSHKeyUpload(httpClient, hostname, f, title) } func getCurrentLogin(httpClient httpClient, hostname, authToken string) (string, error) { diff --git a/pkg/cmd/ssh-key/add/add.go b/pkg/cmd/ssh-key/add/add.go index df977b291..84d39d1e7 100644 --- a/pkg/cmd/ssh-key/add/add.go +++ b/pkg/cmd/ssh-key/add/add.go @@ -1,6 +1,7 @@ package add import ( + "fmt" "io" "net/http" "os" @@ -78,11 +79,18 @@ func runAdd(opts *AddOptions) error { hostname, _ := cfg.Authentication().DefaultHost() - err = SSHKeyUpload(httpClient, hostname, keyReader, opts.Title, true, opts.IO) + uploaded, err := SSHKeyUpload(httpClient, hostname, keyReader, opts.Title) if err != nil { return err } - // SSHKeyUpload prints the success message. + cs := opts.IO.ColorScheme() + + if uploaded { + fmt.Fprintf(opts.IO.ErrOut, "%s Public key added to your account\n", cs.SuccessIcon()) + } else { + fmt.Fprintf(opts.IO.ErrOut, "%s Public key already exists on your account\n", cs.SuccessIcon()) + } + return nil } diff --git a/pkg/cmd/ssh-key/add/add_test.go b/pkg/cmd/ssh-key/add/add_test.go index 8497dc991..d49aaea90 100644 --- a/pkg/cmd/ssh-key/add/add_test.go +++ b/pkg/cmd/ssh-key/add/add_test.go @@ -62,8 +62,8 @@ func Test_runAdd(t *testing.T) { name: "invalid key format", stdin: "ssh-ed25519", wantStdout: "", - wantStderr: "X Error: provided key is not in a valid format\n", - wantErrMsg: "SilentError", + wantStderr: "", + wantErrMsg: "provided key is not in a valid format", opts: AddOptions{KeyFile: "-"}, }, } diff --git a/pkg/cmd/ssh-key/add/http.go b/pkg/cmd/ssh-key/add/http.go index 85e4a4dc0..e716425e0 100644 --- a/pkg/cmd/ssh-key/add/http.go +++ b/pkg/cmd/ssh-key/add/http.go @@ -3,7 +3,7 @@ package add import ( "bytes" "encoding/json" - "fmt" + "errors" "io" "net/http" "strings" @@ -11,39 +11,33 @@ import ( "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/ghinstance" "github.com/cli/cli/v2/pkg/cmd/ssh-key/shared" - "github.com/cli/cli/v2/pkg/cmdutil" - "github.com/cli/cli/v2/pkg/iostreams" ) -func SSHKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader, title string, printSuccessMsgs bool, ios *iostreams.IOStreams) error { +// Uploads the provided SSH key. Returns true if the key was uploaded, false if it was not. +func SSHKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader, title string) (bool, error) { url := ghinstance.RESTPrefix(hostname) + "user/keys" - cs := ios.ColorScheme() keyBytes, err := io.ReadAll(keyFile) if err != nil { - return err + return false, err } fullUserKey := string(keyBytes) splitKey := strings.Fields(fullUserKey) if len(splitKey) < 2 { - fmt.Fprintf(ios.ErrOut, "%s Error: provided key is not in a valid format\n", cs.FailureIcon()) - return cmdutil.SilentError + return false, errors.New("provided key is not in a valid format") } keyToCompare := splitKey[0] + " " + splitKey[1] keys, err := shared.UserKeys(httpClient, hostname, "") if err != nil { - return err + return false, err } for _, k := range keys { if k.Key == keyToCompare { - if printSuccessMsgs && ios.IsStderrTTY() { - fmt.Fprintf(ios.ErrOut, "%s Public key already exists on your account\n", cs.SuccessIcon()) - } - return nil + return false, nil } } @@ -54,31 +48,28 @@ func SSHKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader, t payloadBytes, err := json.Marshal(payload) if err != nil { - return err + return false, err } req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes)) if err != nil { - return err + return false, err } resp, err := httpClient.Do(req) if err != nil { - return err + return false, err } defer resp.Body.Close() if resp.StatusCode > 299 { - return api.HandleHTTPError(resp) + return false, api.HandleHTTPError(resp) } _, err = io.Copy(io.Discard, resp.Body) if err != nil { - return err + return false, err } - if printSuccessMsgs && ios.IsStderrTTY() { - fmt.Fprintf(ios.ErrOut, "%s Public key added to your account\n", cs.SuccessIcon()) - } - return nil + return true, nil }