feat: netssh multi — several hosts side by side in tmux or zellij
netssh multi opens one pane per host in a tmux window or zellij tab. Hosts come from names, tag:TAG (NetBox and static hosts), cluster:NAME (NetBox virtualization clusters; devices by default, --kind vm|all), and groups from the config (@name, nested, with cycle detection). Every pane runs netssh HOST, so lookup, rules, and jump hosts apply. - tmux: a new window inside tmux, else a new session that is attached; tiled layout, pane borders with the host and a [SYNC] marker. - zellij: a generated grid layout; a new tab inside zellij, else a new session. - --sync broadcasts typing to all panes; netssh sync toggles it in the current tmux window or zellij tab. - TUI: Space marks hosts, Ctrl+A marks all, Enter opens them together. - Configurator: multiplexer, panes per window, groups. NetBox SearchAll gains a cluster filter (name to cluster IDs per instance), and cache entries keep the host's cluster for completion and an offline fallback. Panes skip the 24-hour background refresh (checked once by multi) and get HOME/XDG paths but no NETSSH_* values, which would expose tokens in the process list. testenv/tests/15-multi.sh checks real tmux and zellij sessions, including a broadcast command arriving on all three cluster nodes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dbb99bdded
commit
a31aab4110
24 files changed
+1754
-40
No files matched your search
+74
-9
@@ -27,8 +27,9 @@ type SelectedHost struct {
|
||||
IP string
|
||||
User string // empty = use config default
|
||||
Port string // empty = use default port
|
||||
Action string // ActionSSH, ActionSFTP, or ActionTunnel
|
||||
Action string // ActionSSH, ActionSFTP, ActionTunnel, or ActionMulti
|
||||
Forwards []string // forward names or specs for ActionTunnel
|
||||
Targets []string // marked hosts for ActionMulti
|
||||
}
|
||||
|
||||
// Actions the user can start on a host.
|
||||
@@ -36,6 +37,7 @@ const (
|
||||
ActionSSH = ""
|
||||
ActionSFTP = "sftp"
|
||||
ActionTunnel = "tunnel"
|
||||
ActionMulti = "multi"
|
||||
)
|
||||
|
||||
// --- bubbletea messages ---
|
||||
@@ -74,7 +76,8 @@ func (h hostItem) FilterValue() string { return h.name }
|
||||
|
||||
// --- compact list delegate ---
|
||||
|
||||
type compactDelegate struct{}
|
||||
// compactDelegate shares the model's marks so marked hosts show a dot.
|
||||
type compactDelegate struct{ marked map[string]bool }
|
||||
|
||||
func (d compactDelegate) Height() int { return 1 }
|
||||
func (d compactDelegate) Spacing() int { return 0 }
|
||||
@@ -84,7 +87,11 @@ func (d compactDelegate) Render(w io.Writer, m list.Model, index int, item list.
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
line := fmt.Sprintf(" %s %s", h.name, lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render(h.ip))
|
||||
mark := " "
|
||||
if d.marked[h.key()] {
|
||||
mark = "● "
|
||||
}
|
||||
line := fmt.Sprintf(" %s%s %s", mark, h.name, 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, " "))
|
||||
}
|
||||
@@ -169,10 +176,12 @@ type Model struct {
|
||||
selected *SelectedHost
|
||||
width int
|
||||
height int
|
||||
recentMode bool // true when showing recent hosts (empty search, initial state)
|
||||
filterOpen bool // Ctrl+F toggles
|
||||
editMode bool // Ctrl+E on the selected item
|
||||
forwardMode bool // Ctrl+T on the selected item
|
||||
recentMode bool // true when showing recent hosts (empty search, initial state)
|
||||
filterOpen bool // Ctrl+F toggles
|
||||
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
|
||||
}
|
||||
|
||||
func New(client *netbox.Client, c *cache.Cache, defaultUser string, defaultPorts ...int) *Model {
|
||||
@@ -191,7 +200,8 @@ func New(client *netbox.Client, c *cache.Cache, defaultUser string, defaultPorts
|
||||
fwd.Prompt = ""
|
||||
fwd.Placeholder = "web, 8080:localhost:80"
|
||||
|
||||
l := list.New(nil, compactDelegate{}, 0, 0)
|
||||
marked := make(map[string]bool)
|
||||
l := list.New(nil, compactDelegate{marked: marked}, 0, 0)
|
||||
l.SetShowHelp(false)
|
||||
l.SetShowTitle(false)
|
||||
l.SetShowStatusBar(false)
|
||||
@@ -212,6 +222,7 @@ func New(client *netbox.Client, c *cache.Cache, defaultUser string, defaultPorts
|
||||
filterInput: fi,
|
||||
editInput: ei,
|
||||
forwardInput: fwd,
|
||||
marked: marked,
|
||||
list: l,
|
||||
client: client,
|
||||
cache: c,
|
||||
@@ -386,11 +397,26 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, nil
|
||||
|
||||
case "enter":
|
||||
if len(m.markOrder) > 0 {
|
||||
m.selected = &SelectedHost{Action: ActionMulti, Targets: append([]string(nil), m.markOrder...)}
|
||||
return m, tea.Quit
|
||||
}
|
||||
if item, ok := m.list.SelectedItem().(hostItem); ok {
|
||||
m.selected = &SelectedHost{Name: item.name, IP: item.ip, Kind: item.kind}
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
||||
case "space":
|
||||
if item, ok := m.list.SelectedItem().(hostItem); ok {
|
||||
m.toggleMark(item)
|
||||
m.list.CursorDown()
|
||||
return m, nil
|
||||
}
|
||||
|
||||
case "ctrl+a":
|
||||
m.toggleAllVisible()
|
||||
return m, nil
|
||||
|
||||
case "ctrl+e":
|
||||
if item, ok := m.list.SelectedItem().(hostItem); ok {
|
||||
m.openEdit(item)
|
||||
@@ -584,7 +610,10 @@ 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 ctrl+o sftp ctrl+t forward ctrl+e edit ctrl+f filter ctrl+n notes ctrl+b NetBox"
|
||||
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"
|
||||
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)
|
||||
}
|
||||
sb.WriteString("\n" + lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render(hint) + "\n")
|
||||
}
|
||||
|
||||
@@ -724,6 +753,42 @@ func (m *Model) buildSelected(item hostItem) *SelectedHost {
|
||||
|
||||
func (m *Model) SetNotes(service *notes.Service) { m.noteService = service }
|
||||
|
||||
func (h hostItem) key() string { return h.name + "\x00" + h.kind }
|
||||
|
||||
// toggleMark marks or unmarks a host for opening several side by side.
|
||||
func (m *Model) toggleMark(item hostItem) {
|
||||
key := item.key()
|
||||
if m.marked[key] {
|
||||
delete(m.marked, key)
|
||||
for i, target := range m.markOrder {
|
||||
if target == item.name {
|
||||
m.markOrder = append(m.markOrder[:i], m.markOrder[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
m.marked[key] = true
|
||||
m.markOrder = append(m.markOrder, item.name)
|
||||
}
|
||||
|
||||
// toggleAllVisible marks every listed host, or unmarks them if all are marked.
|
||||
func (m *Model) toggleAllVisible() {
|
||||
var items []hostItem
|
||||
allMarked := true
|
||||
for _, it := range m.list.Items() {
|
||||
if item, ok := it.(hostItem); ok {
|
||||
items = append(items, item)
|
||||
allMarked = allMarked && m.marked[item.key()]
|
||||
}
|
||||
}
|
||||
for _, item := range items {
|
||||
if m.marked[item.key()] == allMarked {
|
||||
m.toggleMark(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetForwards provides the forward names configured for a host (ctrl+t).
|
||||
func (m *Model) SetForwards(forwardsFor func(name, kind string, tags []string) []string) {
|
||||
m.forwardsFor = forwardsFor
|
||||
|
||||
@@ -122,3 +122,38 @@ func TestForwardInputEscCancels(t *testing.T) {
|
||||
t.Fatal("esc did not cancel")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSpaceMarksHostsAndEnterOpensThem(t *testing.T) {
|
||||
m := New(nil, nil, "")
|
||||
m.updateListItems([]netbox.HostEntry{{Name: "a", Kind: "device"}, {Name: "b", Kind: "device"}, {Name: "c", Kind: "vm"}})
|
||||
m.Update(tea.KeyPressMsg{Code: tea.KeySpace, Text: " "})
|
||||
if len(m.markOrder) != 1 || m.markOrder[0] != "a" || m.list.Index() != 1 {
|
||||
t.Fatalf("space: marks %v, cursor %d", m.markOrder, m.list.Index())
|
||||
}
|
||||
if m.input.Value() != "" {
|
||||
t.Fatal("space went into the search field")
|
||||
}
|
||||
m.list.Select(2)
|
||||
m.Update(tea.KeyPressMsg{Code: tea.KeySpace, Text: " "})
|
||||
if !strings.Contains(m.viewContent(), "2 marked") {
|
||||
t.Fatalf("no marked count:\n%s", m.viewContent())
|
||||
}
|
||||
m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
||||
if m.selected == nil || m.selected.Action != ActionMulti || !reflect.DeepEqual(m.selected.Targets, []string{"a", "c"}) {
|
||||
t.Fatalf("got %+v", m.selected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCtrlATogglesAllVisible(t *testing.T) {
|
||||
m := New(nil, nil, "")
|
||||
m.updateListItems([]netbox.HostEntry{{Name: "a"}, {Name: "b"}})
|
||||
m.toggleMark(hostItem{name: "a"})
|
||||
m.Update(tea.KeyPressMsg{Code: 'a', Mod: tea.ModCtrl})
|
||||
if !reflect.DeepEqual(m.markOrder, []string{"a", "b"}) {
|
||||
t.Fatalf("mark all: %v", m.markOrder)
|
||||
}
|
||||
m.Update(tea.KeyPressMsg{Code: 'a', Mod: tea.ModCtrl})
|
||||
if len(m.markOrder) != 0 {
|
||||
t.Fatalf("unmark all: %v", m.markOrder)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user