feat: add pre_connect hook and optional shortcut alias display
- ssh.pre_connect: local shell command run before every connection, layered like proxy_jump (global, instance/static host, rules; "none" disables). Host details are passed as NETSSH_* env vars, a non-zero exit aborts the connection, --dry-run only prints it. - shortcuts.show: show shortcut aliases in the TUI host list, an ALIAS column in `cache list` / `hosts list` (also via --aliases) and in Tab completion. - Both editable in `netssh configure`; README and CHANGELOG updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAuVpPncumMViLLc8N4
This commit is contained in:
1 parent
3804c33669
commit
419c682a71
15 files changed
+507
-23
No files matched your search
@@ -166,3 +166,30 @@ func TestHiddenPrefixDuplicateLabels(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliasesShownNextToHostName(t *testing.T) {
|
||||
m := New(nil, cache.New("", 3600), "")
|
||||
m.SetAliases(func(target string) string {
|
||||
return map[string]string{"fsn1-bx1.hoster.de": "fsnbx1", "web01": "web01"}[target]
|
||||
})
|
||||
m.updateListItems([]netbox.HostEntry{{Name: "fsn1-bx1.hoster.de", PrimaryIP4: "10.0.0.1"}, {Name: "web01", PrimaryIP4: "10.0.0.2"}})
|
||||
items := m.list.Items()
|
||||
if got := items[0].(hostItem).alias; got != "fsnbx1" {
|
||||
t.Fatalf("alias = %q", got)
|
||||
}
|
||||
// An alias equal to the shown name is not repeated.
|
||||
if got := items[1].(hostItem).alias; got != "" {
|
||||
t.Fatalf("redundant alias %q", got)
|
||||
}
|
||||
var b strings.Builder
|
||||
compactDelegate{marked: map[string]bool{}}.Render(&b, m.list, 1, items[0])
|
||||
if !strings.Contains(b.String(), "fsn1-bx1.hoster.de") || !strings.Contains(b.String(), "→ fsnbx1") {
|
||||
t.Fatalf("row %q", b.String())
|
||||
}
|
||||
// Without SetAliases nothing changes.
|
||||
plain := New(nil, cache.New("", 3600), "")
|
||||
plain.updateListItems([]netbox.HostEntry{{Name: "fsn1-bx1.hoster.de"}})
|
||||
if got := plain.list.Items()[0].(hostItem).alias; got != "" {
|
||||
t.Fatalf("alias without SetAliases: %q", got)
|
||||
}
|
||||
}
|
||||
+22
-3
@@ -64,6 +64,7 @@ type searchResultMsg struct {
|
||||
|
||||
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
|
||||
@@ -97,7 +98,11 @@ 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.Title(), lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render(h.ip))
|
||||
title := h.Title()
|
||||
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))
|
||||
if index == m.Index() {
|
||||
line = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("86")).Render("> " + strings.TrimPrefix(line, " "))
|
||||
}
|
||||
@@ -188,7 +193,8 @@ type Model struct {
|
||||
forwardMode bool // Ctrl+T on the selected item
|
||||
marked map[string]bool // hostItem.key() of hosts marked with space
|
||||
hostLabel func(string) string
|
||||
markOrder []string // targets in the order they were marked
|
||||
aliasFor func(string) string // shortcut alias per target, "" for none
|
||||
markOrder []string // targets in the order they were marked
|
||||
}
|
||||
|
||||
func New(client *netbox.Client, c *cache.Cache, defaultUser string, defaultPorts ...int) *Model {
|
||||
@@ -705,7 +711,14 @@ func (m *Model) updateListItems(entries []netbox.HostEntry) {
|
||||
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}
|
||||
alias := ""
|
||||
if m.aliasFor != nil {
|
||||
alias = m.aliasFor(e.Target())
|
||||
if strings.EqualFold(alias, label) {
|
||||
alias = ""
|
||||
}
|
||||
}
|
||||
items[i] = hostItem{label: label, alias: alias, id: e.ID, name: e.Target(), ip: ip, kind: e.Kind, tags: e.Tags}
|
||||
}
|
||||
m.list.SetItems(items)
|
||||
m.resizeList()
|
||||
@@ -847,3 +860,9 @@ func parseForwards(input string, available []string) ([]string, error) {
|
||||
func (m *Model) SetHostLabel(format func(string) string) {
|
||||
m.hostLabel = format
|
||||
}
|
||||
|
||||
// SetAliases shows the shortcut alias returned for each target next to its
|
||||
// name; return "" for hosts without one. Configure it before the program starts.
|
||||
func (m *Model) SetAliases(aliasFor func(target string) string) {
|
||||
m.aliasFor = aliasFor
|
||||
}
|
||||
Reference in new issue
Block a user