MCPcopy
hub / github.com/cli/cli / keypairForPrivateKey

Function keypairForPrivateKey

pkg/cmd/codespace/ssh.go:524–550  ·  view source on GitHub ↗

keypairForPrivateKey returns the KeyPair with the specified private key if it and the public key both exist

(privateKeyPath string)

Source from the content-addressed store, hash-verified

522
523// keypairForPrivateKey returns the KeyPair with the specified private key if it and the public key both exist
524func 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
552func (a *App) printOpenSSHConfig(ctx context.Context, opts sshOptions) (err error) {
553 ctx, cancel := context.WithCancel(ctx)

Callers 1

firstConfiguredKeyPairFunction · 0.85

Calls 2

ReplaceMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected