netssh link add|list|remove manages links; netssh NAME follows them, links complete with Tab and get a shell alias. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
141 lines
4.0 KiB
Go
141 lines
4.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"text/tabwriter"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
|
)
|
|
|
|
func linkCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "link",
|
|
Short: "Short names for hosts: netssh NAME and shell alias NAME",
|
|
Long: `Links give a host a name of your own, for example mx123 for the server that
|
|
mx123.webseite.de points to. netssh NAME then connects to the host, and new
|
|
shells get the alias NAME. netssh offers to create a link after a DNS
|
|
suggestion; links are stored under links: in the config.`,
|
|
}
|
|
cmd.AddCommand(linkListCmd(), linkAddCmd(), linkRemoveCmd())
|
|
return cmd
|
|
}
|
|
|
|
func linkListCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "list",
|
|
Short: "List links",
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(cfg.Links) == 0 {
|
|
fmt.Fprintln(cmd.OutOrStdout(), "No links. Add one with: netssh link add NAME HOST")
|
|
return nil
|
|
}
|
|
names := make([]string, 0, len(cfg.Links))
|
|
for name := range cfg.Links {
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names)
|
|
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
|
|
fmt.Fprintln(w, "NAME\tHOST")
|
|
for _, name := range names {
|
|
fmt.Fprintf(w, "%s\t%s\n", name, cfg.Links[name])
|
|
}
|
|
return w.Flush()
|
|
},
|
|
}
|
|
}
|
|
|
|
func linkAddCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "add NAME HOST",
|
|
Short: "Create or replace a link",
|
|
Example: " netssh link add mx123 prod/web07\n netssh link add mx123 mx123.webseite.de # asks which host owns the address",
|
|
Args: cobra.ExactArgs(2),
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
|
if len(args) == 1 {
|
|
return hostCompletion(cmd, args, toComplete)
|
|
}
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
name := strings.ToLower(args[0])
|
|
if _, isLink := cfg.Link(args[1]); isLink {
|
|
return fmt.Errorf("%q is a link itself; link to a host", args[1])
|
|
}
|
|
// Resolve now, so a typo fails here and the link stores the canonical target.
|
|
h, err := resolveOrSuggestQuiet(cfg, args[1])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if h.Kind == "dns" {
|
|
return fmt.Errorf("%s is not a NetBox or static host; links point to hosts netssh knows", args[1])
|
|
}
|
|
if err := saveLink(name, h.Name); err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(cmd.OutOrStdout(), "netssh %s → %s (open a new shell for the alias %s)\n", name, h.Name, name)
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
// resolveOrSuggestQuiet resolves for link add without offering another link.
|
|
func resolveOrSuggestQuiet(cfg *config.Config, host string) (resolvedHost, error) {
|
|
old := offerLinkFunc
|
|
offerLinkFunc = func(string, string, string) {}
|
|
defer func() { offerLinkFunc = old }()
|
|
return resolveOrSuggest(cfg, host, false)
|
|
}
|
|
|
|
func linkRemoveCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "remove NAME",
|
|
Short: "Remove a link",
|
|
Args: cobra.ExactArgs(1),
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return nil, cobra.ShellCompDirectiveError
|
|
}
|
|
var names []string
|
|
for name := range cfg.Links {
|
|
names = append(names, name)
|
|
}
|
|
return filterPrefix(names, toComplete, nil), cobra.ShellCompDirectiveNoFileComp
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
removed := false
|
|
for key := range cfg.Links {
|
|
if strings.EqualFold(key, args[0]) {
|
|
delete(cfg.Links, key)
|
|
removed = true
|
|
}
|
|
}
|
|
if !removed {
|
|
return fmt.Errorf("no link %q", args[0])
|
|
}
|
|
if err := saveHosts(cfg); err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(cmd.OutOrStdout(), "Link %s removed.\n", args[0])
|
|
return nil
|
|
},
|
|
}
|
|
}
|