Files
ssh-netbox-wrapper/internal/setup/operations.go
T
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

85 lines
3.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package setup
import (
"fmt"
"strconv"
"charm.land/huh/v2"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
)
func editOperations(cfg *config.Config) error {
history, days, entries := strconv.Itoa(cfg.TUI.HistoryLimit), strconv.Itoa(cfg.Activity.RetentionDays), strconv.Itoa(cfg.Activity.MaxEntries)
enabled := cfg.Activity.Enabled
columns := append([]string(nil), cfg.TUI.EffectiveColumns()...)
timeout := strconv.Itoa(int(cfg.TUI.ReachabilityTimeoutDuration().Milliseconds()))
columnOptions := make([]huh.Option[string], 0, len(config.TUIColumns))
for _, column := range config.TUIColumns {
columnOptions = append(columnOptions, huh.NewOption(columnLabels[column], column).Selected(contains(columns, column)))
}
valid := func(s string) error {
n, err := strconv.Atoi(s)
if err != nil || n < 0 {
return fmt.Errorf("enter a non-negative number")
}
return nil
}
err := huh.NewForm(huh.NewGroup(
huh.NewInput().Title("Recent hosts shown").Description("10 by default; 0 shows all. Favorites are additional.").Value(&history).Validate(valid),
huh.NewConfirm().Title("Record local action history?").Description("Stores host, command name, duration and exit code; no arguments or output.").Value(&enabled),
huh.NewInput().Title("Retention in days").Description("0 = no age limit").Value(&days).Validate(valid),
huh.NewInput().Title("Maximum recorded actions").Description("0 = no count limit").Value(&entries).Validate(valid),
).Title("History & activity"), huh.NewGroup(
huh.NewMultiSelect[string]().Title("Shown in the TUI next to each host").
Description("Reachability opens a TCP connection to every listed host's SSH port.").
Options(columnOptions...).Value(&columns),
huh.NewInput().Title("Reachability timeout (ms)").Description("300 by default").Value(&timeout).Validate(valid),
).Title("TUI display")).Run()
if err != nil {
return err
}
cfg.TUI.Columns = orderColumns(cfg.TUI.EffectiveColumns(), columns)
if ms, _ := strconv.Atoi(timeout); ms != 300 {
cfg.TUI.ReachabilityTimeout = ms
} else {
cfg.TUI.ReachabilityTimeout = 0
}
cfg.TUI.HistoryLimit, _ = strconv.Atoi(history)
cfg.Activity.RetentionDays, _ = strconv.Atoi(days)
cfg.Activity.MaxEntries, _ = strconv.Atoi(entries)
cfg.Activity.Enabled = enabled
return nil
}
var columnLabels = map[string]string{
"alias": "Shortcut alias", "ip": "IP address", "kind": "Kind (device/vm/static)", "source": "Source (live/cache/static)",
"status": "NetBox status (if not active)", "site": "Site", "location": "Location (site › locations › rack › U)", "role": "Role", "platform": "Platform", "tenant": "Tenant",
"cluster": "Cluster", "tags": "Tags", "reachability": "Reachability (TCP check ✓/✗)",
}
func contains(list []string, value string) bool {
for _, item := range list {
if item == value {
return true
}
}
return false
}
// orderColumns keeps an existing custom order and appends new selections in
// the canonical order; the multi-select itself does not preserve order.
func orderColumns(previous, selected []string) []string {
out := []string{}
for _, column := range previous {
if contains(selected, column) {
out = append(out, column)
}
}
for _, column := range config.TUIColumns {
if contains(selected, column) && !contains(out, column) {
out = append(out, column)
}
}
return out
}