feat: add short ID column to list, improve connect error with hint
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"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
|
// 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
|
// connectWithNativeSSH uses the system SSH client
|
||||||
|
|||||||
+13
-5
@@ -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 {
|
func outputTable(hosts []*models.Host) error {
|
||||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||||
|
|
||||||
fmt.Fprintln(w, "NAME\tHOSTNAME\tPORT\tUSER\tGROUP\tAUTH\tTAGS")
|
fmt.Fprintln(w, "ID\tNAME\tHOSTNAME\tPORT\tUSER\tGROUP\tAUTH\tTAGS")
|
||||||
fmt.Fprintln(w, "────\t────────\t────\t────\t─────\t────\t────")
|
fmt.Fprintln(w, "--\t────\t────────\t────\t────\t─────\t────\t────")
|
||||||
|
|
||||||
for _, h := range hosts {
|
for _, h := range hosts {
|
||||||
tags := strings.Join(h.Tags, ", ")
|
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.Name,
|
||||||
h.Hostname,
|
h.Hostname,
|
||||||
h.Port,
|
h.Port,
|
||||||
@@ -178,8 +186,8 @@ func outputJSON(hosts []*models.Host) error {
|
|||||||
if i > 0 {
|
if i > 0 {
|
||||||
fmt.Print(",")
|
fmt.Print(",")
|
||||||
}
|
}
|
||||||
fmt.Printf(`{"id":"%s","name":"%s","hostname":"%s","port":%d,"username":"%s","group":"%s","auth_type":"%s"}`,
|
fmt.Printf(`{"id":"%s","short_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)
|
h.ID, shortID(h.ID), h.Name, h.Hostname, h.Port, h.Username, h.Group, h.Auth.Type)
|
||||||
}
|
}
|
||||||
fmt.Println("]")
|
fmt.Println("]")
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user