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
+40
-1
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"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/hook"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/shortcuts"
|
||||
)
|
||||
@@ -29,6 +30,28 @@ func newNetBoxClient(cfg *config.Config) *netbox.Client {
|
||||
return netbox.NewClient(cfg.NetBox.URL, cfg.NetBox.Token, cfg.NetBox.TokenVersion)
|
||||
}
|
||||
|
||||
// shortcutAliases maps each target (lowercased) to the shell alias generated for
|
||||
// it, but only where shortcuts actually shorten the displayed name: an alias that
|
||||
// merely replaces dots with underscores ("web01_example_com") adds nothing.
|
||||
// The aliases are the same ones netssh alias and the shell hook define.
|
||||
func shortcutAliases(entries []cache.Entry, cfg config.Config) map[string]string {
|
||||
out := make(map[string]string)
|
||||
if !cfg.Shortcuts.Enabled() {
|
||||
return out
|
||||
}
|
||||
plain := strings.NewReplacer(".", "_", "/", "_")
|
||||
for _, alias := range hook.BuildAliasEntries(append([]cache.Entry(nil), entries...), cfg) {
|
||||
if alias.Host == "" {
|
||||
continue // the command alias for netssh itself
|
||||
}
|
||||
if alias.Name == plain.Replace(strings.ToLower(cfg.HostLabel(alias.Host))) {
|
||||
continue
|
||||
}
|
||||
out[strings.ToLower(alias.Host)] = alias.Name
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func normalizeTarget(cfg config.ShortcutsConfig, qualified bool) func(string) string {
|
||||
return func(target string) string {
|
||||
source, name := netbox.SplitTarget(target)
|
||||
@@ -48,6 +71,9 @@ func normalizeTarget(cfg config.ShortcutsConfig, qualified bool) func(string) st
|
||||
// Names present in several instances stay qualified ("prod/web01").
|
||||
// Hosts whose full name does not match are offered by their configured
|
||||
// shortcut instead ("fsnb" → "fsnbx1" for fsn1-bx1.example.com).
|
||||
//
|
||||
// With shortcuts.show, shortcuts are also offered for hosts whose full name
|
||||
// matches, so "fsn" lists both fsn1-bx1.example.com and fsnbx1.
|
||||
func hostCompletions(entries []cache.Entry, toComplete string, sc config.ShortcutsConfig) []string {
|
||||
prefix := strings.ToLower(toComplete)
|
||||
qualifiedInput := strings.Contains(prefix, "/")
|
||||
@@ -75,7 +101,20 @@ func hostCompletions(entries []cache.Entry, toComplete string, sc config.Shortcu
|
||||
}
|
||||
}
|
||||
if sc.Enabled() {
|
||||
names = append(names, shortcutCompletions(entries, unmatched, prefix, sc)...)
|
||||
candidates := unmatched
|
||||
if sc.Show {
|
||||
candidates = entries
|
||||
}
|
||||
offered := make(map[string]bool, len(names))
|
||||
for _, name := range names {
|
||||
offered[strings.ToLower(name)] = true
|
||||
}
|
||||
for _, name := range shortcutCompletions(entries, candidates, prefix, sc) {
|
||||
if !offered[name] {
|
||||
offered[name] = true
|
||||
names = append(names, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(names)
|
||||
return names
|
||||
|
||||
Reference in new issue
Block a user