Files
ssh-netbox-wrapper/internal/setup/commands.go
Sebastian UnterschützandClaude Opus 5.5 1660bb3098
Test / test (pull_request) Successful in 4m15s
feat: NetBox insights, host tools and configurable TUI
- info/open/whois: NetBox details (site, nested locations, rack units,
  role, platform, tenant, IPs per interface), web page, reverse IP lookup
- cache status, site, role, platform, tenant, location chain and rack;
  ask before connecting to hosts in ssh.warn_status (--force skips)
- ssh rules and command restrictions match site/role/platform/tenant/status
- tui.columns selects what the TUI shows; optional TCP reachability column;
  detail view (Ctrl+L), NetBox page (Ctrl+G), status/location filters
- resolver.fallback tries other strategies' addresses when unreachable
- copy-id, known-hosts reset and address-change notices
- export ansible (YAML/INI) and export ssh-config
- token: keyring stores NetBox tokens in the system keyring
- fix stale menu number in testenv configure test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 12:33:59 +01:00

100 lines
4.0 KiB
Go

package setup
import (
"fmt"
"sort"
"strings"
"charm.land/huh/v2"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
)
func editCommands(cfg *config.Config) error {
for {
var names []string
for name := range cfg.Commands {
names = append(names, name)
}
sort.Strings(names)
var options []huh.Option[string]
for _, name := range names {
options = append(options, huh.NewOption("Edit "+name, "edit:"+name), huh.NewOption("Remove "+name, "remove:"+name))
}
options = append(options, huh.NewOption("Add command", "add"), huh.NewOption("Done", "done"))
action := "done"
if err := huh.NewSelect[string]().Title("Custom host commands").Options(options...).Value(&action).Run(); err != nil {
return err
}
if action == "done" {
return nil
}
if name, ok := strings.CutPrefix(action, "remove:"); ok {
remove := false
if err := huh.NewConfirm().Title("Remove " + name + "?").Value(&remove).Run(); err != nil {
return err
}
if remove {
delete(cfg.Commands, name)
}
continue
}
original, _ := strings.CutPrefix(action, "edit:")
if action == "add" {
original = ""
}
name := original
command := cfg.Commands[original]
binary, arguments := "", ""
if len(command.Args) > 0 {
binary = command.Args[0]
arguments = strings.Join(command.Args[1:], "\n")
}
err := huh.NewForm(huh.NewGroup(
huh.NewInput().Title("Command name").Placeholder("mtr").Value(&name).Validate(func(s string) error {
if s == "" {
return fmt.Errorf("required")
}
if s != original {
if _, exists := cfg.Commands[s]; exists {
return fmt.Errorf("name already exists")
}
}
return config.ValidateCommandAlias(s)
}),
huh.NewInput().Title("Alias prefix").Description("m + host shortcut xg1 = mxg1. Empty disables automatic aliases.").Value(&command.AliasPrefix),
huh.NewInput().Title("Program or executable script").Description("Use an absolute script path, or bash/python3 with the script path as the first argument.").Placeholder("mtr or /home/user/bin/check-host").Value(&binary),
huh.NewText().Title("Arguments, one per line").Description("Use {address} for the resolved address, {host} for the hostname, {target} for instance/hostname. No shell quoting needed.").Lines(5).Value(&arguments),
huh.NewConfirm().Title("Confirm before running?").Value(&command.Confirm),
huh.NewInput().Title("Timeout").Description("Optional positive duration, e.g. 30s or 5m.").Value(&command.Timeout),
huh.NewInput().Title("Post-run script").Description("Local command after completion; receives NETSSH_EXIT_CODE.").Value(&command.PostRun),
huh.NewInput().Title("Only for tag").Value(&command.Match.Tag),
huh.NewInput().Title("Only for instance").Value(&command.Match.Instance),
huh.NewInput().Title("Only for kind").Description("device, vm or static; empty means all.").Value(&command.Match.Kind),
huh.NewInput().Title("Only for hostname pattern").Description("For example router*; all specified filters must match.").Value(&command.Match.Name),
huh.NewInput().Title("Only for site").Description("NetBox site name or slug.").Value(&command.Match.Site),
huh.NewInput().Title("Only for role").Description("NetBox device/VM role name or slug.").Value(&command.Match.Role),
huh.NewInput().Title("Only for platform").Description("NetBox platform name or slug, e.g. junos.").Value(&command.Match.Platform),
huh.NewInput().Title("Only for status").Description("NetBox status value, e.g. active.").Value(&command.Match.Status),
).Title("Local command")).Run()
if err != nil {
return err
}
command.Match = trimMatch(command.Match)
command.Args = []string{binary}
if arguments != "" {
command.Args = append(command.Args, strings.Split(arguments, "\n")...)
}
candidate := config.Config{Commands: make(map[string]config.HostCommand)}
for k, v := range cfg.Commands {
if k != original {
candidate.Commands[k] = v
}
}
candidate.Commands[name] = command
if err := candidate.ValidateCommands(); err != nil {
return err
}
cfg.Commands = candidate.Commands
}
}