feat: add pre_connect hook and optional shortcut alias display
- ssh.pre_connect: local shell command run before every connection, layered like proxy_jump (global, instance/static host, rules; "none" disables). Host details are passed as NETSSH_* env vars, a non-zero exit aborts the connection, --dry-run only prints it. - shortcuts.show: show shortcut aliases in the TUI host list, an ALIAS column in `cache list` / `hosts list` (also via --aliases) and in Tab completion. - Both editable in `netssh configure`; README and CHANGELOG updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAuVpPncumMViLLc8N4
This commit is contained in:
1 parent
3804c33669
commit
419c682a71
15 files changed
+507
-23
No files matched your search
+26
-5
@@ -14,27 +14,32 @@ import (
|
||||
)
|
||||
|
||||
func hostsCmd() *cobra.Command {
|
||||
var showAliases bool
|
||||
cmd := &cobra.Command{
|
||||
Use: "hosts",
|
||||
Short: "Manage static hosts that are not in NetBox",
|
||||
Long: `Static hosts are defined under hosts: in the config. They are found before
|
||||
NetBox, work offline, and support shortcuts, rules (kind: static), aliases,
|
||||
completion, sftp, scp, and tunnels like NetBox hosts.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error { return listHosts() },
|
||||
RunE: func(cmd *cobra.Command, args []string) error { return listHosts(showAliases) },
|
||||
}
|
||||
cmd.AddCommand(hostsListCmd(), hostsAddCmd(), hostsRemoveCmd())
|
||||
cmd.Flags().BoolVar(&showAliases, "aliases", false, "Show each host's shortcut alias (always on with shortcuts.show)")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func hostsListCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
var showAliases bool
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List static hosts",
|
||||
RunE: func(cmd *cobra.Command, args []string) error { return listHosts() },
|
||||
RunE: func(cmd *cobra.Command, args []string) error { return listHosts(showAliases) },
|
||||
}
|
||||
cmd.Flags().BoolVar(&showAliases, "aliases", false, "Show each host's shortcut alias (always on with shortcuts.show)")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func listHosts() error {
|
||||
func listHosts(showAliases bool) error {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -43,13 +48,29 @@ func listHosts() error {
|
||||
fmt.Println("No static hosts. Add one with: netssh hosts add NAME ADDRESS")
|
||||
return nil
|
||||
}
|
||||
withAliases := showAliases || cfg.Shortcuts.Show
|
||||
var aliases map[string]string
|
||||
if withAliases {
|
||||
// Aliases are unique across all hosts, so compute them with the cache.
|
||||
c := newCache(cfg)
|
||||
_ = c.Load()
|
||||
aliases = shortcutAliases(c.All(), *cfg)
|
||||
}
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||
fmt.Fprintln(w, "NAME\tADDRESS\tUSER\tPORT\tTAGS")
|
||||
if withAliases {
|
||||
fmt.Fprintln(w, "NAME\tALIAS\tADDRESS\tUSER\tPORT\tTAGS")
|
||||
} else {
|
||||
fmt.Fprintln(w, "NAME\tADDRESS\tUSER\tPORT\tTAGS")
|
||||
}
|
||||
for _, host := range cfg.Hosts {
|
||||
port := ""
|
||||
if host.Port > 0 {
|
||||
port = fmt.Sprint(host.Port)
|
||||
}
|
||||
if withAliases {
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n", host.Name, orDash(aliases[strings.ToLower(host.Name)]), host.Address, host.User, port, strings.Join(host.Tags, ","))
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", host.Name, host.Address, host.User, port, strings.Join(host.Tags, ","))
|
||||
}
|
||||
return w.Flush()
|
||||
|
||||
Reference in new issue
Block a user