feat: add host actions, lifecycle hooks and operational tools
This commit is contained in:
1 parent
8d4915543e
commit
e5bde9c61f
32 files changed
+2083
-64
No files matched your search
+188
-14
@@ -22,6 +22,7 @@ import (
|
||||
|
||||
// SelectedHost is returned when the user confirms a host in the TUI.
|
||||
type SelectedHost struct {
|
||||
Command string
|
||||
Kind string
|
||||
Name string
|
||||
IP string
|
||||
@@ -63,13 +64,15 @@ type searchResultMsg struct {
|
||||
// --- list item ---
|
||||
|
||||
type hostItem struct {
|
||||
label string // display only; name remains the canonical target
|
||||
alias string // shortcut alias shown next to the name (shortcuts.show)
|
||||
name string
|
||||
id int
|
||||
ip string
|
||||
kind string
|
||||
tags []string
|
||||
provenance string
|
||||
favorite, pinned bool
|
||||
label string // display only; name remains the canonical target
|
||||
alias string // shortcut alias shown next to the name (shortcuts.show)
|
||||
name string
|
||||
id int
|
||||
ip string
|
||||
kind string
|
||||
tags []string
|
||||
}
|
||||
|
||||
func (h hostItem) Title() string {
|
||||
@@ -102,7 +105,16 @@ func (d compactDelegate) Render(w io.Writer, m list.Model, index int, item list.
|
||||
if h.alias != "" {
|
||||
title += " " + lipgloss.NewStyle().Foreground(lipgloss.Color("109")).Render("→ "+h.alias)
|
||||
}
|
||||
line := fmt.Sprintf(" %s%s %s", mark, title, lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render(h.ip))
|
||||
badge := ""
|
||||
if h.pinned {
|
||||
badge = "◆ "
|
||||
} else if h.favorite {
|
||||
badge = "★ "
|
||||
}
|
||||
line := fmt.Sprintf(" %s%s %s", mark+badge, title, lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render(h.ip))
|
||||
if h.provenance != "" {
|
||||
line += " [" + h.provenance + "]"
|
||||
}
|
||||
if index == m.Index() {
|
||||
line = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("86")).Render("> " + strings.TrimPrefix(line, " "))
|
||||
}
|
||||
@@ -161,6 +173,14 @@ func applyFilter(entries []netbox.HostEntry, f filterOpts) []netbox.HostEntry {
|
||||
// --- Model ---
|
||||
|
||||
type Model struct {
|
||||
historyLimit int
|
||||
favorites, pinned map[string]bool
|
||||
savePreferences func(map[string]bool, map[string]bool) error
|
||||
commandsFor func(string, string, []string) []string
|
||||
actionHost *hostItem
|
||||
actionNames []string
|
||||
actionIndex int
|
||||
|
||||
noteErr error
|
||||
editHost *hostItem
|
||||
noteInput textarea.Model
|
||||
@@ -229,6 +249,7 @@ func New(client *netbox.Client, c *cache.Cache, defaultUser string, defaultPorts
|
||||
ni.CharLimit = 0
|
||||
ni.SetHeight(10)
|
||||
return &Model{
|
||||
historyLimit: 10, favorites: map[string]bool{}, pinned: map[string]bool{},
|
||||
noteInput: ni,
|
||||
defaultPort: port,
|
||||
input: ti,
|
||||
@@ -250,14 +271,40 @@ func (m *Model) Init() tea.Cmd {
|
||||
|
||||
// loadRecent returns a Cmd that immediately resolves to the recently-used host list.
|
||||
func (m *Model) loadRecent() tea.Cmd {
|
||||
favoriteNames, pinned, limit := copyBools(m.favorites), copyBools(m.pinned), m.historyLimit
|
||||
return func() tea.Msg {
|
||||
if m.cache == nil {
|
||||
return searchResultMsg{query: "", entries: nil, recent: true}
|
||||
}
|
||||
recent := m.cache.RecentlyUsed(10)
|
||||
recent := m.cache.RecentlyUsed(limit)
|
||||
seen := map[string]bool{}
|
||||
var favorites []cache.Entry
|
||||
for name, enabled := range favoriteNames {
|
||||
if !enabled {
|
||||
continue
|
||||
}
|
||||
e, _ := m.cache.Get(name)
|
||||
if e.Name == "" {
|
||||
e.Name = name
|
||||
}
|
||||
favorites = append(favorites, e)
|
||||
seen[name] = true
|
||||
}
|
||||
sort.Slice(favorites, func(i, j int) bool {
|
||||
if pinned[favorites[i].Name] != pinned[favorites[j].Name] {
|
||||
return pinned[favorites[i].Name]
|
||||
}
|
||||
return favorites[i].Name < favorites[j].Name
|
||||
})
|
||||
for _, e := range recent {
|
||||
if !seen[e.Name] {
|
||||
favorites = append(favorites, e)
|
||||
}
|
||||
}
|
||||
recent = favorites
|
||||
entries := make([]netbox.HostEntry, len(recent))
|
||||
for i, e := range recent {
|
||||
entries[i] = netbox.HostEntry{Name: e.Name, PrimaryIP4: e.IP, Kind: e.Kind, Tags: e.Tags}
|
||||
entries[i] = netbox.HostEntry{Name: e.Name, PrimaryIP4: e.IP, Kind: e.Kind, Tags: e.Tags, Cached: true, CachedAt: e.CachedAt, Static: e.Static}
|
||||
}
|
||||
return searchResultMsg{query: "", entries: entries, recent: true}
|
||||
}
|
||||
@@ -294,6 +341,34 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
if msg.String() == "ctrl+c" {
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
||||
if m.actionHost != nil {
|
||||
switch msg.String() {
|
||||
case "esc":
|
||||
m.actionHost = nil
|
||||
case "up":
|
||||
if m.actionIndex > 0 {
|
||||
m.actionIndex--
|
||||
}
|
||||
case "down":
|
||||
if m.actionIndex+1 < len(m.actionNames) {
|
||||
m.actionIndex++
|
||||
}
|
||||
case "enter":
|
||||
h := m.actionHost
|
||||
if m.actionIndex == 0 {
|
||||
if len(m.markOrder) > 0 {
|
||||
m.selected = &SelectedHost{Action: ActionMulti, Targets: append([]string(nil), m.markOrder...)}
|
||||
} else {
|
||||
m.selected = &SelectedHost{Name: h.name, Kind: h.kind, IP: h.ip}
|
||||
}
|
||||
} else {
|
||||
m.selected = &SelectedHost{Name: h.name, Kind: h.kind, IP: h.ip, Command: m.actionNames[m.actionIndex], Targets: append([]string(nil), m.markOrder...)}
|
||||
}
|
||||
return m, tea.Quit
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
if m.noteMode {
|
||||
if m.noteBusy {
|
||||
return m, nil
|
||||
@@ -403,6 +478,43 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
case "ctrl+c", "esc":
|
||||
return m, tea.Quit
|
||||
|
||||
case "ctrl+r":
|
||||
if h, ok := m.list.SelectedItem().(hostItem); ok {
|
||||
m.actionHost = &h
|
||||
m.actionIndex = 0
|
||||
m.actionNames = []string{"SSH"}
|
||||
if m.commandsFor != nil {
|
||||
m.actionNames = append(m.actionNames, m.commandsFor(h.name, h.kind, h.tags)...)
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
case "ctrl+d", "ctrl+p":
|
||||
if h, ok := m.list.SelectedItem().(hostItem); ok {
|
||||
favorites, pinned := copyBools(m.favorites), copyBools(m.pinned)
|
||||
if msg.String() == "ctrl+p" {
|
||||
pinned[h.name] = !pinned[h.name]
|
||||
if pinned[h.name] {
|
||||
favorites[h.name] = true
|
||||
}
|
||||
} else {
|
||||
favorites[h.name] = !favorites[h.name]
|
||||
if !favorites[h.name] {
|
||||
delete(pinned, h.name)
|
||||
}
|
||||
}
|
||||
if m.savePreferences != nil {
|
||||
if err := m.savePreferences(favorites, pinned); err != nil {
|
||||
m.err = err
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
m.favorites, m.pinned = favorites, pinned
|
||||
m.updateListItems(m.lastResults)
|
||||
if m.recentMode && m.input.Value() == "" {
|
||||
return m, m.loadRecent()
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
case "ctrl+f":
|
||||
m.filterOpen = true
|
||||
m.input.Blur()
|
||||
@@ -563,6 +675,24 @@ func (m *Model) View() tea.View {
|
||||
}
|
||||
|
||||
func (m *Model) viewContent() string {
|
||||
if m.actionHost != nil {
|
||||
var out strings.Builder
|
||||
fmt.Fprintf(&out, "Action for %s", m.actionHost.Title())
|
||||
if len(m.markOrder) > 0 {
|
||||
fmt.Fprintf(&out, " (%d marked hosts)", len(m.markOrder))
|
||||
}
|
||||
out.WriteString("\n\n")
|
||||
for i, n := range m.actionNames {
|
||||
marker := " "
|
||||
if i == m.actionIndex {
|
||||
marker = "> "
|
||||
}
|
||||
fmt.Fprintf(&out, "%s%s\n", marker, n)
|
||||
}
|
||||
out.WriteString("\n↑/↓ select enter run esc cancel\n")
|
||||
return out.String()
|
||||
}
|
||||
|
||||
if m.noteMode {
|
||||
target := "local"
|
||||
if m.noteRemote {
|
||||
@@ -623,7 +753,7 @@ func (m *Model) viewContent() string {
|
||||
sb.WriteString(m.editInput.View() + "\n")
|
||||
sb.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render(" enter connect esc cancel") + "\n")
|
||||
} else {
|
||||
hint := "ctrl+c quit enter connect space mark ctrl+o sftp ctrl+t forward ctrl+e edit ctrl+f filter ctrl+n notes ctrl+b NetBox"
|
||||
hint := "ctrl+r actions ctrl+d favorite ctrl+p pin ctrl+c quit enter connect space mark ctrl+o sftp ctrl+t forward ctrl+e edit ctrl+f filter ctrl+n notes ctrl+b NetBox"
|
||||
if n := len(m.markOrder); n > 0 {
|
||||
hint = fmt.Sprintf("%d marked — enter open side by side space unmark ctrl+a all esc quit", n)
|
||||
}
|
||||
@@ -652,7 +782,7 @@ func (m *Model) doSearch(query string, seq int) tea.Cmd {
|
||||
if m.cache != nil {
|
||||
for _, entry := range m.cache.Search(query) {
|
||||
source, name := netbox.SplitTarget(entry.Name)
|
||||
cached = append(cached, netbox.HostEntry{Source: source, Name: name, PrimaryIP4: entry.IP, Kind: entry.Kind, Tags: entry.Tags})
|
||||
cached = append(cached, netbox.HostEntry{Source: source, Name: name, PrimaryIP4: entry.IP, Kind: entry.Kind, Tags: entry.Tags, Cached: true, CachedAt: entry.CachedAt, Static: entry.Static})
|
||||
}
|
||||
}
|
||||
if m.client == nil || (len(cached) > 0 && !m.client.MultipleSources()) {
|
||||
@@ -689,7 +819,19 @@ func (m *Model) netboxSearchOpts() netbox.SearchOptions {
|
||||
// updateListItems applies the active filter and sets the list items.
|
||||
func (m *Model) updateListItems(entries []netbox.HostEntry) {
|
||||
f := parseFilter(m.filterInput.Value())
|
||||
filtered := applyFilter(entries, f)
|
||||
filtered := append([]netbox.HostEntry(nil), applyFilter(entries, f)...)
|
||||
sort.SliceStable(filtered, func(i, j int) bool {
|
||||
rank := func(e netbox.HostEntry) int {
|
||||
if m.pinned[e.Target()] {
|
||||
return 2
|
||||
}
|
||||
if m.favorites[e.Target()] {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return rank(filtered[i]) > rank(filtered[j])
|
||||
})
|
||||
items := make([]list.Item, len(filtered))
|
||||
labels := make([]string, len(filtered))
|
||||
counts := make(map[string]int, len(filtered))
|
||||
@@ -718,7 +860,23 @@ func (m *Model) updateListItems(entries []netbox.HostEntry) {
|
||||
alias = ""
|
||||
}
|
||||
}
|
||||
items[i] = hostItem{label: label, alias: alias, id: e.ID, name: e.Target(), ip: ip, kind: e.Kind, tags: e.Tags}
|
||||
provenance := "live"
|
||||
if e.Static {
|
||||
provenance = "static"
|
||||
} else if e.Cached {
|
||||
if e.CachedAt.IsZero() {
|
||||
provenance = "saved host; resolve on connect"
|
||||
} else {
|
||||
age := max(time.Duration(0), time.Since(e.CachedAt)).Round(time.Second)
|
||||
provenance = "cache " + age.String()
|
||||
if m.cache != nil {
|
||||
if _, fresh := m.cache.Get(e.Target()); !fresh {
|
||||
provenance += ", stale"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
items[i] = hostItem{provenance: provenance, favorite: m.favorites[e.Target()], pinned: m.pinned[e.Target()], label: label, alias: alias, id: e.ID, name: e.Target(), ip: ip, kind: e.Kind, tags: e.Tags}
|
||||
}
|
||||
m.list.SetItems(items)
|
||||
m.resizeList()
|
||||
@@ -866,3 +1024,19 @@ func (m *Model) SetHostLabel(format func(string) string) {
|
||||
func (m *Model) SetAliases(aliasFor func(target string) string) {
|
||||
m.aliasFor = aliasFor
|
||||
}
|
||||
|
||||
func copyBools(in map[string]bool) map[string]bool {
|
||||
out := map[string]bool{}
|
||||
for k, v := range in {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
func (m *Model) SetHistoryLimit(limit int) { m.historyLimit = limit }
|
||||
func (m *Model) SetPreferences(favorites, pinned map[string]bool, save func(map[string]bool, map[string]bool) error) {
|
||||
m.favorites, m.pinned = copyBools(favorites), copyBools(pinned)
|
||||
m.savePreferences = save
|
||||
}
|
||||
func (m *Model) SetCommands(commands func(string, string, []string) []string) {
|
||||
m.commandsFor = commands
|
||||
}
|
||||
Reference in new issue
Block a user