Cleanups from PR comments

This commit is contained in:
Caleb Brose 2022-06-14 14:33:41 +00:00 committed by GitHub
parent 9f6e1c774b
commit 83153fe9e5
4 changed files with 11 additions and 16 deletions

View file

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

View file

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

View file

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

View file

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