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 }