package main import ( "context" "fmt" "time" "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox" internalssh "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/ssh" "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/tui" ) // configureTUIExtras wires reachability, the detail view and opening NetBox // pages into the TUI. known enriches a listed host from the cache. func configureTUIExtras(m *tui.Model, cfg *config.Config, client *netbox.Client, known func(name, kind string, tags []string) resolvedHost) { m.SetColumns(cfg.TUI.EffectiveColumns()) if cfg.TUI.ReachabilityEnabled() { m.SetReachability(cfg.TUI.ReachabilityTimeoutDuration(), func(name, kind string, tags []string) (int, bool) { h := known(name, kind, tags) if usesJumpHost(hostOptions(cfg, h), internalssh.WrapperFlags{}) { return 0, false } _, port := hostDefaults(cfg, h) if port <= 0 { port = 22 } return port, true }) } lookup := func(ctx context.Context, name, kind string, id int) (netbox.HostEntry, resolvedHost, error) { h := known(name, kind, nil) if kind == config.StaticKind { return netbox.HostEntry{}, h, fmt.Errorf("%s is a static host and has no NetBox record", name) } if client == nil { return netbox.HostEntry{}, h, fmt.Errorf("no NetBox is configured") } if id <= 0 { id = h.ID } entry, _, h, err := netboxEntry(ctx, client, name, kind, id, h) return entry, h, err } m.SetOpener(func(name, kind string, id int) error { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() entry, _, err := lookup(ctx, name, kind, id) if err != nil { return err } url, err := client.WebURL(entry) if err != nil { return err } return openBrowser(url) }) m.SetDetails(func(name, kind string, id int) (string, error) { if kind == config.StaticKind { h := known(name, kind, nil) user, port := hostDefaults(cfg, h) text := fmt.Sprintf("%s (static host from hosts:)\n Address %s\n", name, h.IP) if user != "" { text += " User " + user + "\n" } if port > 0 { text += fmt.Sprintf(" Port %d\n", port) } return text, nil } ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() entry, h, err := lookup(ctx, name, kind, id) if err != nil { return "", err } details, err := client.GetDetails(ctx, entry) if err != nil && details.Name == "" { return "", err } text := formatDetails(details, h) if err != nil { text += "\nwarning: " + err.Error() + "\n" } return text, nil }) }