diff --git a/cmd/hostkeeper/connect.go b/cmd/hostkeeper/connect.go index a15a8eb..03e92da 100644 --- a/cmd/hostkeeper/connect.go +++ b/cmd/hostkeeper/connect.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "os/exec" + "strings" "time" "github.com/spf13/cobra" @@ -118,7 +119,11 @@ func findHost(ctx context.Context, store storage.Storage, identifier string) (*m } // Host not found, provide helpful error - return nil, fmt.Errorf("host '%s' not found. Use 'hostkeeper list' to see available hosts", identifier) + msg := fmt.Sprintf("host '%s' not found. Use 'hostkeeper list' to see available hosts", identifier) + if strings.Contains(identifier, " ") { + msg += fmt.Sprintf("\nHint: if the host name contains spaces, quote it: connect \"%s\"", identifier) + } + return nil, fmt.Errorf("%s", msg) } // connectWithNativeSSH uses the system SSH client diff --git a/cmd/hostkeeper/list.go b/cmd/hostkeeper/list.go index 5e881f5..cd377b7 100644 --- a/cmd/hostkeeper/list.go +++ b/cmd/hostkeeper/list.go @@ -150,15 +150,23 @@ func sortHosts(hosts []*models.Host, sortBy string) { } } +func shortID(id string) string { + if len(id) >= 8 { + return id[:8] + } + return id +} + func outputTable(hosts []*models.Host) error { w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) - fmt.Fprintln(w, "NAME\tHOSTNAME\tPORT\tUSER\tGROUP\tAUTH\tTAGS") - fmt.Fprintln(w, "────\t────────\t────\t────\t─────\t────\t────") + fmt.Fprintln(w, "ID\tNAME\tHOSTNAME\tPORT\tUSER\tGROUP\tAUTH\tTAGS") + fmt.Fprintln(w, "--\t────\t────────\t────\t────\t─────\t────\t────") for _, h := range hosts { tags := strings.Join(h.Tags, ", ") - fmt.Fprintf(w, "%s\t%s\t%d\t%s\t%s\t%s\t%s\n", + fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%s\t%s\t%s\t%s\n", + shortID(h.ID), h.Name, h.Hostname, h.Port, @@ -178,8 +186,8 @@ func outputJSON(hosts []*models.Host) error { if i > 0 { fmt.Print(",") } - fmt.Printf(`{"id":"%s","name":"%s","hostname":"%s","port":%d,"username":"%s","group":"%s","auth_type":"%s"}`, - h.ID, h.Name, h.Hostname, h.Port, h.Username, h.Group, h.Auth.Type) + fmt.Printf(`{"id":"%s","short_id":"%s","name":"%s","hostname":"%s","port":%d,"username":"%s","group":"%s","auth_type":"%s"}`, + h.ID, shortID(h.ID), h.Name, h.Hostname, h.Port, h.Username, h.Group, h.Auth.Type) } fmt.Println("]") return nil