fix: host lookup by hostname, interactive input with spaces
This commit is contained in:
+18
-31
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@@ -174,52 +175,47 @@ func runAddHost(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addHostInteractive(cfg *config.Config) error {
|
func addHostInteractive(cfg *config.Config) error {
|
||||||
reader := strings.NewReader("")
|
input := bufio.NewReader(os.Stdin)
|
||||||
|
|
||||||
fmt.Println("╔══════════════════════════════════════╗")
|
fmt.Println("╔══════════════════════════════════════╗")
|
||||||
fmt.Println("║ Add New SSH Host ║")
|
fmt.Println("║ Add New SSH Host ║")
|
||||||
fmt.Println("╚══════════════════════════════════════╝")
|
fmt.Println("╚══════════════════════════════════════╝")
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
|
||||||
|
readLine := func(prompt string) string {
|
||||||
|
fmt.Print(prompt)
|
||||||
|
line, _ := input.ReadString('\n')
|
||||||
|
return strings.TrimRight(line, "\n\r")
|
||||||
|
}
|
||||||
|
|
||||||
// Get host name
|
// Get host name
|
||||||
fmt.Print("Host Name (e.g., myserver): ")
|
name := readLine("Host Name (e.g., myserver): ")
|
||||||
var name string
|
|
||||||
fmt.Fscanln(reader)
|
|
||||||
fmt.Scanln(&name)
|
|
||||||
if name == "" {
|
if name == "" {
|
||||||
return fmt.Errorf("host name is required")
|
return fmt.Errorf("host name is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get hostname
|
// Get hostname
|
||||||
fmt.Print("Hostname or IP (e.g., 192.168.1.10): ")
|
hostname := readLine("Hostname or IP (e.g., 192.168.1.10): ")
|
||||||
var hostname string
|
|
||||||
fmt.Scanln(&hostname)
|
|
||||||
if hostname == "" {
|
if hostname == "" {
|
||||||
return fmt.Errorf("hostname is required")
|
return fmt.Errorf("hostname is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get port
|
// Get port
|
||||||
defaultPort := cfg.GetAppConfig().DefaultPort
|
defaultPort := cfg.GetAppConfig().DefaultPort
|
||||||
fmt.Printf("Port [%d]: ", defaultPort)
|
portInput := readLine(fmt.Sprintf("Port [%d]: ", defaultPort))
|
||||||
var portInput string
|
|
||||||
fmt.Scanln(&portInput)
|
|
||||||
port := defaultPort
|
port := defaultPort
|
||||||
if portInput != "" {
|
if portInput != "" {
|
||||||
fmt.Sscanf(portInput, "%d", &port)
|
fmt.Sscanf(portInput, "%d", &port)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get username
|
// Get username
|
||||||
fmt.Print("Username: ")
|
username := readLine("Username: ")
|
||||||
var username string
|
|
||||||
fmt.Scanln(&username)
|
|
||||||
if username == "" {
|
if username == "" {
|
||||||
return fmt.Errorf("username is required")
|
return fmt.Errorf("username is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get auth type
|
// Get auth type
|
||||||
fmt.Print("Auth Type (password/key/both) [password]: ")
|
authType := readLine("Auth Type (password/key/both) [password]: ")
|
||||||
var authType string
|
|
||||||
fmt.Scanln(&authType)
|
|
||||||
if authType == "" {
|
if authType == "" {
|
||||||
authType = "password"
|
authType = "password"
|
||||||
}
|
}
|
||||||
@@ -227,16 +223,13 @@ func addHostInteractive(cfg *config.Config) error {
|
|||||||
// Get password
|
// Get password
|
||||||
var password string
|
var password string
|
||||||
if authType == "password" || authType == "both" {
|
if authType == "password" || authType == "both" {
|
||||||
fmt.Print("Password: ")
|
password = readLine("Password: ")
|
||||||
fmt.Scanln(&password)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get key path
|
// Get key path
|
||||||
var keyContent string
|
var keyContent string
|
||||||
if authType == "key" || authType == "both" {
|
if authType == "key" || authType == "both" {
|
||||||
fmt.Print("Path to private key (~/.ssh/id_rsa): ")
|
keyPath := readLine("Path to private key (~/.ssh/id_rsa): ")
|
||||||
var keyPath string
|
|
||||||
fmt.Scanln(&keyPath)
|
|
||||||
if keyPath != "" {
|
if keyPath != "" {
|
||||||
data, err := os.ReadFile(keyPath)
|
data, err := os.ReadFile(keyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -247,14 +240,10 @@ func addHostInteractive(cfg *config.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get group
|
// Get group
|
||||||
fmt.Print("Group (optional): ")
|
group := readLine("Group (optional): ")
|
||||||
var group string
|
|
||||||
fmt.Scanln(&group)
|
|
||||||
|
|
||||||
// Get tags
|
// Get tags
|
||||||
fmt.Print("Tags (comma-separated, optional): ")
|
tagsInput := readLine("Tags (comma-separated, optional): ")
|
||||||
var tagsInput string
|
|
||||||
fmt.Scanln(&tagsInput)
|
|
||||||
var tags []string
|
var tags []string
|
||||||
if tagsInput != "" {
|
if tagsInput != "" {
|
||||||
tags = strings.Split(tagsInput, ",")
|
tags = strings.Split(tagsInput, ",")
|
||||||
@@ -264,9 +253,7 @@ func addHostInteractive(cfg *config.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get notes
|
// Get notes
|
||||||
fmt.Print("Notes (optional): ")
|
notes := readLine("Notes (optional): ")
|
||||||
var notes string
|
|
||||||
fmt.Scanln(¬es)
|
|
||||||
|
|
||||||
// Create host
|
// Create host
|
||||||
host := &models.Host{
|
host := &models.Host{
|
||||||
|
|||||||
@@ -103,6 +103,20 @@ func findHost(ctx context.Context, store storage.Storage, identifier string) (*m
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to find by hostname
|
||||||
|
for _, h := range hosts {
|
||||||
|
if h.Hostname == identifier {
|
||||||
|
return h, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to find by ID prefix (short ID match)
|
||||||
|
for _, h := range hosts {
|
||||||
|
if len(h.ID) >= 8 && h.ID[:8] == identifier {
|
||||||
|
return h, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Host not found, provide helpful error
|
// Host not found, provide helpful error
|
||||||
return nil, fmt.Errorf("host '%s' not found. Use 'hostkeeper list' to see available hosts", identifier)
|
return nil, fmt.Errorf("host '%s' not found. Use 'hostkeeper list' to see available hosts", identifier)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user