cmdAdd adds a new SSH host configuration
(ctx *ShellContext)
| 55 | |
| 56 | // cmdAdd adds a new SSH host configuration |
| 57 | func cmdAdd(ctx *ShellContext) error { |
| 58 | fmt.Println("\n=== Add New SSH Host ===") |
| 59 | |
| 60 | cfg := &sshconfig.HostConfig{} |
| 61 | |
| 62 | // 1. Host Alias (required) |
| 63 | for { |
| 64 | line, err := ctx.liner.Prompt("Host alias (e.g., myserver): ") |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | alias := strings.TrimSpace(line) |
| 69 | if alias == "" { |
| 70 | fmt.Println("Error: Host alias is required") |
| 71 | continue |
| 72 | } |
| 73 | // Check if already exists |
| 74 | if _, exists := ctx.byName[alias]; exists { |
| 75 | fmt.Printf("Warning: Host '%s' already exists. Use 'edit %s' to modify.\n", alias, alias) |
| 76 | return nil |
| 77 | } |
| 78 | cfg.Name = alias |
| 79 | break |
| 80 | } |
| 81 | |
| 82 | // 2. Hostname/IP (required) |
| 83 | for { |
| 84 | line, err := ctx.liner.Prompt("Hostname or IP: ") |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | hostname := strings.TrimSpace(line) |
| 89 | if hostname == "" { |
| 90 | fmt.Println("Error: Hostname is required") |
| 91 | continue |
| 92 | } |
| 93 | cfg.Hostname = hostname |
| 94 | break |
| 95 | } |
| 96 | |
| 97 | // 3. Username (optional) |
| 98 | line, _ := ctx.liner.Prompt("Username [press Enter to skip]: ") |
| 99 | cfg.User = strings.TrimSpace(line) |
| 100 | |
| 101 | // 4. Port (optional) |
| 102 | line, _ = ctx.liner.Prompt("Port [22]: ") |
| 103 | port := strings.TrimSpace(line) |
| 104 | if port != "" && port != "22" { |
| 105 | cfg.Port = port |
| 106 | } |
| 107 | |
| 108 | // 5. Private Key Selection |
| 109 | fmt.Println("\nPrivate Key Configuration:") |
| 110 | fmt.Println(" 1) Use fssh agent (recommended)") |
| 111 | fmt.Println(" 2) Direct IdentityFile path") |
| 112 | fmt.Println(" 3) None (password authentication)") |
| 113 | line, _ = ctx.liner.Prompt("Choice [1]: ") |
| 114 | choice := strings.TrimSpace(line) |
no test coverage detected