importSSHKeys scans ~/.ssh/ and interactively imports discovered keys
()
| 23 | |
| 24 | // importSSHKeys scans ~/.ssh/ and interactively imports discovered keys |
| 25 | func importSSHKeys() error { |
| 26 | home, err := os.UserHomeDir() |
| 27 | if err != nil { |
| 28 | return fmt.Errorf("failed to get home directory: %w", err) |
| 29 | } |
| 30 | |
| 31 | sshDir := filepath.Join(home, ".ssh") |
| 32 | |
| 33 | // Scan for SSH keys |
| 34 | keys, err := scanSSHDirectory(sshDir) |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | |
| 39 | if len(keys) == 0 { |
| 40 | fmt.Println("No SSH private keys found in ~/.ssh/") |
| 41 | fmt.Println() |
| 42 | fmt.Println("You can import keys later with: fssh import --alias <name> --file <path>") |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | // Display found keys |
| 47 | fmt.Printf("Found %d SSH private key(s):\n", len(keys)) |
| 48 | fmt.Println() |
| 49 | |
| 50 | for i, key := range keys { |
| 51 | encrypted := "" |
| 52 | if key.IsEncrypted { |
| 53 | encrypted = " [encrypted]" |
| 54 | } |
| 55 | fmt.Printf(" %d) %s%s\n", i+1, key.Filename, encrypted) |
| 56 | fmt.Printf(" Path: %s\n", key.Path) |
| 57 | fmt.Println() |
| 58 | } |
| 59 | |
| 60 | // Ask which keys to import |
| 61 | fmt.Println("Enter the numbers of keys to import (e.g., '1,3' or '1-3' or 'all'):") |
| 62 | selection, err := otp.PromptInput("Keys to import [all]: ") |
| 63 | if err != nil { |
| 64 | return fmt.Errorf("failed to read selection: %w", err) |
| 65 | } |
| 66 | |
| 67 | // Trim and normalize input |
| 68 | selection = strings.TrimSpace(selection) |
| 69 | |
| 70 | // Replace any Unicode spaces with regular spaces |
| 71 | selection = strings.Map(func(r rune) rune { |
| 72 | if r == '\u3000' || r == '\u00A0' || r == '\t' { |
| 73 | return ' ' |
| 74 | } |
| 75 | return r |
| 76 | }, selection) |
| 77 | |
| 78 | // Remove all spaces for easier parsing |
| 79 | selection = strings.ReplaceAll(selection, " ", "") |
| 80 | |
| 81 | if selection == "" { |
| 82 | selection = "all" |
no test coverage detected