keypairForPrivateKey returns the KeyPair with the specified private key if it and the public key both exist
(privateKeyPath string)
| 522 | |
| 523 | // keypairForPrivateKey returns the KeyPair with the specified private key if it and the public key both exist |
| 524 | func keypairForPrivateKey(privateKeyPath string) (*ssh.KeyPair, error) { |
| 525 | if strings.HasPrefix(privateKeyPath, "~") { |
| 526 | userHomeDir, err := os.UserHomeDir() |
| 527 | if err != nil { |
| 528 | return nil, fmt.Errorf("getting home dir: %w", err) |
| 529 | } |
| 530 | |
| 531 | // os.Stat can't handle ~, so convert it to the real path |
| 532 | privateKeyPath = strings.Replace(privateKeyPath, "~", userHomeDir, 1) |
| 533 | } |
| 534 | |
| 535 | // The default configuration includes standard keys like id_rsa or id_ed25519, |
| 536 | // but these may not actually exist |
| 537 | if _, err := os.Stat(privateKeyPath); err != nil { |
| 538 | return nil, errKeyFileNotFound |
| 539 | } |
| 540 | |
| 541 | publicKeyPath := privateKeyPath + ".pub" |
| 542 | if _, err := os.Stat(publicKeyPath); err != nil { |
| 543 | return nil, errKeyFileNotFound |
| 544 | } |
| 545 | |
| 546 | return &ssh.KeyPair{ |
| 547 | PrivateKeyPath: privateKeyPath, |
| 548 | PublicKeyPath: publicKeyPath, |
| 549 | }, nil |
| 550 | } |
| 551 | |
| 552 | func (a *App) printOpenSSHConfig(ctx context.Context, opts sshOptions) (err error) { |
| 553 | ctx, cancel := context.WithCancel(ctx) |
no test coverage detected