move SSH key errors out of upload function

This commit is contained in:
vaindil 2023-03-31 13:47:25 -04:00
parent 0e5e7de9af
commit 7006d3f0a5
4 changed files with 35 additions and 31 deletions

View file

@ -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) {

View file

@ -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
}

View file

@ -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: "-"},
},
}

View file

@ -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
}