feat(instances): allow disabling prefixes in aliases and TUI
This commit is contained in:
1 parent
1f46d9d9e5
commit
20d7a40543
12 files changed
+298
-19
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
tea "charm.land/bubbletea/v2"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/cache"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
|
||||
)
|
||||
|
||||
@@ -79,3 +80,89 @@ func TestPartialOutageKeepsLiveAndCachedResultsVisible(t *testing.T) {
|
||||
t.Fatalf("outage warning overflowed view: %d", rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHiddenPrefixKeepsCanonicalTargets(t *testing.T) {
|
||||
cfg := config.Config{NetBoxes: []config.NetBoxConfig{
|
||||
{Name: "OFFICE", DisablePrefix: true}, {Name: "lab"},
|
||||
}}
|
||||
for _, cached := range []bool{false, true} {
|
||||
for _, action := range []string{"ssh", "sftp", "multi", "autocomplete", "edit", "tunnel"} {
|
||||
t.Run(fmt.Sprintf("cached=%v/%s", cached, action), func(t *testing.T) {
|
||||
m := New(nil, nil, "")
|
||||
m.SetHostLabel(cfg.HostLabel)
|
||||
m.Update(tea.WindowSizeMsg{Width: 100, Height: 24})
|
||||
entries := []netbox.HostEntry{
|
||||
{Source: "office", Name: "web01", PrimaryIP4: "10.0.0.1"},
|
||||
{Source: "lab", Name: "web01", PrimaryIP4: "10.0.0.2"},
|
||||
}
|
||||
if cached {
|
||||
for i := range entries {
|
||||
entries[i].Name = entries[i].Target()
|
||||
entries[i].Source = ""
|
||||
}
|
||||
}
|
||||
m.updateListItems(entries)
|
||||
item := m.list.Items()[0].(hostItem)
|
||||
if item.Title() != "web01" || item.name != "office/web01" {
|
||||
t.Fatalf("display changed identity: %+v", item)
|
||||
}
|
||||
view := m.View().Content
|
||||
if strings.Contains(view, "office/web01") || !strings.Contains(view, "lab/web01") {
|
||||
t.Fatalf("wrong rendered prefixes: %s", view)
|
||||
}
|
||||
switch action {
|
||||
case "ssh", "sftp":
|
||||
key := tea.KeyPressMsg{Code: tea.KeyEnter}
|
||||
if action == "sftp" {
|
||||
key = tea.KeyPressMsg{Code: 'o', Mod: tea.ModCtrl}
|
||||
}
|
||||
m.Update(key)
|
||||
if m.selected == nil || m.selected.Name != "office/web01" {
|
||||
t.Fatalf("lost instance: %+v", m.selected)
|
||||
}
|
||||
case "multi":
|
||||
m.Update(tea.KeyPressMsg{Code: tea.KeySpace, Text: " "})
|
||||
m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
if m.selected == nil || len(m.selected.Targets) != 1 || m.selected.Targets[0] != "office/web01" {
|
||||
t.Fatalf("multi lost instance: %+v", m.selected)
|
||||
}
|
||||
case "autocomplete":
|
||||
m.Update(tea.KeyPressMsg{Code: tea.KeyTab})
|
||||
if m.input.Value() != "office/web01" {
|
||||
t.Fatal("autocomplete lost instance")
|
||||
}
|
||||
case "edit":
|
||||
m.Update(tea.KeyPressMsg{Code: 'e', Mod: tea.ModCtrl})
|
||||
if m.editHost == nil || m.editHost.name != "office/web01" {
|
||||
t.Fatal("editor lost instance")
|
||||
}
|
||||
case "tunnel":
|
||||
m.Update(tea.KeyPressMsg{Code: 't', Mod: tea.ModCtrl})
|
||||
if m.forwardHost == nil || m.forwardHost.name != "office/web01" {
|
||||
t.Fatal("tunnel lost instance")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHiddenPrefixDuplicateLabels(t *testing.T) {
|
||||
cfg := config.Config{NetBoxes: []config.NetBoxConfig{
|
||||
{Name: "office", DisablePrefix: true}, {Name: "lab", DisablePrefix: true},
|
||||
}}
|
||||
for _, other := range []netbox.HostEntry{
|
||||
{Source: "lab", Name: "WEB"},
|
||||
{Name: "web", Kind: "static"},
|
||||
} {
|
||||
m := New(nil, nil, "")
|
||||
m.SetHostLabel(cfg.HostLabel)
|
||||
m.updateListItems([]netbox.HostEntry{{Source: "office", Name: "web"}, other})
|
||||
for _, entry := range m.list.Items() {
|
||||
item := entry.(hostItem)
|
||||
if item.Title() != item.name {
|
||||
t.Fatalf("ambiguous label: %+v", item)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
-9
@@ -63,14 +63,20 @@ type searchResultMsg struct {
|
||||
// --- list item ---
|
||||
|
||||
type hostItem struct {
|
||||
name string
|
||||
id int
|
||||
ip string
|
||||
kind string
|
||||
tags []string
|
||||
label string // display only; name remains the canonical target
|
||||
name string
|
||||
id int
|
||||
ip string
|
||||
kind string
|
||||
tags []string
|
||||
}
|
||||
|
||||
func (h hostItem) Title() string { return h.name }
|
||||
func (h hostItem) Title() string {
|
||||
if h.label != "" {
|
||||
return h.label
|
||||
}
|
||||
return h.name
|
||||
}
|
||||
func (h hostItem) Description() string { return fmt.Sprintf("%s [%s]", h.ip, h.kind) }
|
||||
func (h hostItem) FilterValue() string { return h.name }
|
||||
|
||||
@@ -91,7 +97,7 @@ func (d compactDelegate) Render(w io.Writer, m list.Model, index int, item list.
|
||||
if d.marked[h.key()] {
|
||||
mark = "● "
|
||||
}
|
||||
line := fmt.Sprintf(" %s%s %s", mark, h.name, lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render(h.ip))
|
||||
line := fmt.Sprintf(" %s%s %s", mark, h.Title(), lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render(h.ip))
|
||||
if index == m.Index() {
|
||||
line = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("86")).Render("> " + strings.TrimPrefix(line, " "))
|
||||
}
|
||||
@@ -181,7 +187,8 @@ type Model struct {
|
||||
editMode bool // Ctrl+E on the selected item
|
||||
forwardMode bool // Ctrl+T on the selected item
|
||||
marked map[string]bool // hostItem.key() of hosts marked with space
|
||||
markOrder []string // targets in the order they were marked
|
||||
hostLabel func(string) string
|
||||
markOrder []string // targets in the order they were marked
|
||||
}
|
||||
|
||||
func New(client *netbox.Client, c *cache.Cache, defaultUser string, defaultPorts ...int) *Model {
|
||||
@@ -678,12 +685,27 @@ func (m *Model) updateListItems(entries []netbox.HostEntry) {
|
||||
f := parseFilter(m.filterInput.Value())
|
||||
filtered := applyFilter(entries, f)
|
||||
items := make([]list.Item, len(filtered))
|
||||
labels := make([]string, len(filtered))
|
||||
counts := make(map[string]int, len(filtered))
|
||||
canonical := make(map[string]bool, len(filtered))
|
||||
for i, e := range filtered {
|
||||
labels[i] = e.Target()
|
||||
canonical[strings.ToLower(e.Target())] = true
|
||||
if m.hostLabel != nil {
|
||||
labels[i] = m.hostLabel(e.Target())
|
||||
}
|
||||
counts[strings.ToLower(labels[i])]++
|
||||
}
|
||||
for i, e := range filtered {
|
||||
ip := e.PrimaryIP4
|
||||
if ip == "" {
|
||||
ip = e.PrimaryIP6
|
||||
}
|
||||
items[i] = hostItem{id: e.ID, name: e.Target(), ip: ip, kind: e.Kind, tags: e.Tags}
|
||||
label := labels[i]
|
||||
if label != e.Target() && (counts[strings.ToLower(label)] > 1 || canonical[strings.ToLower(label)]) {
|
||||
label = e.Target()
|
||||
}
|
||||
items[i] = hostItem{label: label, id: e.ID, name: e.Target(), ip: ip, kind: e.Kind, tags: e.Tags}
|
||||
}
|
||||
m.list.SetItems(items)
|
||||
m.resizeList()
|
||||
@@ -819,3 +841,9 @@ func parseForwards(input string, available []string) ([]string, error) {
|
||||
}
|
||||
return fields, nil
|
||||
}
|
||||
|
||||
// SetHostLabel sets the display name formatter without changing host identity.
|
||||
// Configure it before the program starts, just like SetNotes and SetForwards.
|
||||
func (m *Model) SetHostLabel(format func(string) string) {
|
||||
m.hostLabel = format
|
||||
}
|
||||
Reference in new issue
Block a user