diff --git a/pkg/cmd/auth/shared/login_flow.go b/pkg/cmd/auth/shared/login_flow.go index 5888995cb..48b0c773c 100644 --- a/pkg/cmd/auth/shared/login_flow.go +++ b/pkg/cmd/auth/shared/login_flow.go @@ -106,7 +106,7 @@ func Login(opts *LoginOptions) error { if sshChoice { passphrase, err := promptForSshKeyPassphrase() if err != nil { - return err + return fmt.Errorf("could not prompt for key passphrase: %w", err) } keyPair, err := opts.sshContext.GenerateSSHKey("id_ed25519", passphrase) @@ -235,8 +235,9 @@ func promptForSshKeyPassphrase() (string, error) { err := prompt.SurveyAskOne(&survey.Password{ Message: "Enter a passphrase for your new SSH key (Optional)", }, &sshPassphrase) + if err != nil { - return "", fmt.Errorf("could not prompt: %w", err) + return "", err } return sshPassphrase, nil diff --git a/pkg/cmd/codespace/ssh.go b/pkg/cmd/codespace/ssh.go index e8b5d039c..2c3f1c93f 100644 --- a/pkg/cmd/codespace/ssh.go +++ b/pkg/cmd/codespace/ssh.go @@ -126,10 +126,8 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e if shouldGenerateSSHKeys(args, opts) && sshContext.HasKeygen() { keyPair, err := sshContext.GenerateSSHKey("codespaces", "") - if err != nil { - if _, ok := err.(*ssh.KeyAlreadyExistsError); !ok { - return fmt.Errorf("failed to generate ssh keys: %s", err) - } + if err != nil && !errors.Is(err, ssh.ErrKeyAlreadyExists) { + return fmt.Errorf("failed to generate ssh keys: %w", err) } startSSHOptions.UserPublicKeyFile = keyPair.PublicKeyPath diff --git a/pkg/liveshare/session.go b/pkg/liveshare/session.go index cd6c2e078..f912fa5ea 100644 --- a/pkg/liveshare/session.go +++ b/pkg/liveshare/session.go @@ -75,7 +75,7 @@ func (s *Session) StartSSHServerWithOptions(ctx context.Context, options StartSS if options.UserPublicKeyFile != "" { publicKeyBytes, err := os.ReadFile(options.UserPublicKeyFile) if err != nil { - return 0, "", fmt.Errorf("failed to read public key file: %s", err) + return 0, "", fmt.Errorf("failed to read public key file: %w", err) } params.UserPublicKey = strings.TrimSpace(string(publicKeyBytes)) diff --git a/pkg/ssh/ssh_keys.go b/pkg/ssh/ssh_keys.go index 7f4a1e9d9..4f7b4ef1d 100644 --- a/pkg/ssh/ssh_keys.go +++ b/pkg/ssh/ssh_keys.go @@ -1,7 +1,7 @@ package ssh import ( - "fmt" + "errors" "os" "os/exec" "path/filepath" @@ -22,11 +22,7 @@ type KeyPair struct { PrivateKeyPath string } -type KeyAlreadyExistsError struct { - keyPath string -} - -func (err *KeyAlreadyExistsError) Error() string { return "SSH key already exists: " + err.keyPath } +var ErrKeyAlreadyExists = errors.New("SSH key already exists") func (c *Context) LocalPublicKeys() ([]string, error) { sshDir, err := c.sshDir() @@ -45,7 +41,7 @@ func (c *Context) HasKeygen() bool { func (c *Context) GenerateSSHKey(keyName string, passphrase string) (*KeyPair, error) { keygenExe, err := c.findKeygen() if err != nil { - return nil, fmt.Errorf("could not find keygen executable") + return nil, err } sshDir, err := c.sshDir() @@ -59,8 +55,8 @@ func (c *Context) GenerateSSHKey(keyName string, passphrase string) (*KeyPair, e } if _, err := os.Stat(keyFile); err == nil { - // Still return keyPair because the caller might be OK with they - they can check the error with - return &keyPair, &KeyAlreadyExistsError{keyFile} + // Still return keyPair because the caller might be OK with this - they can check the error with errors.Is(err, ErrKeyAlreadyExists) + return &keyPair, ErrKeyAlreadyExists } if err := os.MkdirAll(filepath.Dir(keyFile), 0711); err != nil {