88 lines
2.8 KiB
Go
88 lines
2.8 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),
|
|
).Title("Local command")).Run()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
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
|
|
}
|
|
}
|