Test / test (pull_request) Successful in 4m15s
- 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>
102 lines
3.3 KiB
Go
102 lines
3.3 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type TUIConfig struct {
|
|
HistoryLimit int `mapstructure:"history_limit"`
|
|
// Columns selects what the TUI shows next to each hostname, in order;
|
|
// nil means DefaultTUIColumns. "reachability" enables the TCP check.
|
|
Columns []string `mapstructure:"columns"`
|
|
// ReachabilityTimeout is the probe timeout in milliseconds; 0 = 300.
|
|
ReachabilityTimeout int `mapstructure:"reachability_timeout"`
|
|
}
|
|
|
|
// TUIColumns are the values tui.columns accepts.
|
|
var TUIColumns = []string{"alias", "ip", "kind", "source", "status", "site", "location", "role", "platform", "tenant", "cluster", "tags", "reachability"}
|
|
|
|
// DefaultTUIColumns is the layout without tui.columns. The TCP reachability
|
|
// check is opt-in, since it opens connections to every listed host.
|
|
var DefaultTUIColumns = []string{"alias", "ip", "source", "status"}
|
|
|
|
// EffectiveColumns returns tui.columns, or the default layout.
|
|
func (t TUIConfig) EffectiveColumns() []string {
|
|
if t.Columns == nil {
|
|
return DefaultTUIColumns
|
|
}
|
|
return t.Columns
|
|
}
|
|
|
|
// ReachabilityEnabled reports whether the TUI probes listed hosts.
|
|
func (t TUIConfig) ReachabilityEnabled() bool {
|
|
for _, column := range t.EffectiveColumns() {
|
|
if column == "reachability" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ReachabilityTimeoutDuration returns tui.reachability_timeout, 300ms by default.
|
|
func (t TUIConfig) ReachabilityTimeoutDuration() time.Duration {
|
|
if t.ReachabilityTimeout <= 0 {
|
|
return 300 * time.Millisecond
|
|
}
|
|
return time.Duration(t.ReachabilityTimeout) * time.Millisecond
|
|
}
|
|
|
|
type ActivityConfig struct {
|
|
Enabled bool `mapstructure:"enabled"`
|
|
RetentionDays int `mapstructure:"retention_days"`
|
|
MaxEntries int `mapstructure:"max_entries"`
|
|
}
|
|
|
|
func (c *Config) ValidateOperations() error {
|
|
if c.TUI.HistoryLimit < 0 {
|
|
return fmt.Errorf("tui.history_limit must be >= 0 (0 = all)")
|
|
}
|
|
if c.Activity.RetentionDays < 0 || c.Activity.MaxEntries < 0 {
|
|
return fmt.Errorf("activity retention_days and max_entries must be >= 0")
|
|
}
|
|
seen := map[string]bool{}
|
|
for i, column := range c.TUI.Columns {
|
|
column = strings.ToLower(strings.TrimSpace(column))
|
|
c.TUI.Columns[i] = column
|
|
known := false
|
|
for _, valid := range TUIColumns {
|
|
known = known || column == valid
|
|
}
|
|
if !known {
|
|
return fmt.Errorf("tui.columns: unknown column %q; use %s", column, strings.Join(TUIColumns, ", "))
|
|
}
|
|
if seen[column] {
|
|
return fmt.Errorf("tui.columns: %q is listed twice", column)
|
|
}
|
|
seen[column] = true
|
|
}
|
|
if c.TUI.ReachabilityTimeout < 0 || c.TUI.ReachabilityTimeout > 10000 {
|
|
return fmt.Errorf("tui.reachability_timeout must be between 0 and 10000 milliseconds")
|
|
}
|
|
if c.Resolver.FallbackTimeout < 0 || c.Resolver.FallbackTimeout > 30000 {
|
|
return fmt.Errorf("resolver.fallback_timeout must be between 0 and 30000 milliseconds")
|
|
}
|
|
for _, status := range c.SSH.WarnStatus {
|
|
if strings.TrimSpace(status) == "" || strings.ContainsAny(status, " \t\n") {
|
|
return fmt.Errorf("ssh.warn_status: invalid status %q", status)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (m SSHMatch) Matches(host SSHHost) bool { return m == (SSHMatch{}) || m.matches(host) }
|
|
func (c HostCommand) Allowed(host SSHHost) bool { return c.Match.Matches(host) }
|
|
func HookCommand(s string) string {
|
|
s = strings.TrimSpace(s)
|
|
if strings.EqualFold(s, "none") {
|
|
return ""
|
|
}
|
|
return s
|
|
}
|