feat(instances): allow disabling prefixes in aliases and TUI
Release / release (push) Canceled after 0s
Release / release (push) Canceled after 0s
This commit is contained in:
1 parent
d9060a6eb7
commit
3804c33669
12 files changed
+298
-19
No files matched your search
@@ -124,9 +124,34 @@ func BuildAliasEntries(entries []cache.Entry, cfg config.Config) []AliasEntry {
|
||||
seen[cfg.Shell.Alias] = true
|
||||
out = append(out, AliasEntry{Name: cfg.Shell.Alias})
|
||||
}
|
||||
for _, e := range entries {
|
||||
|
||||
withoutPrefix := make(map[string]bool, len(cfg.NetBoxes))
|
||||
for _, source := range cfg.NetBoxes {
|
||||
withoutPrefix[strings.ToLower(source.Name)] = source.DisablePrefix
|
||||
}
|
||||
// Reserve canonical aliases as well as requested bare aliases before
|
||||
// generating any output, so cache ordering cannot redirect a collision.
|
||||
canonical := make(map[string]bool, len(entries))
|
||||
counts := make(map[string]int, len(entries))
|
||||
candidates := make([]string, len(entries))
|
||||
for i, e := range entries {
|
||||
name := shortcuts.AliasName(e.Name, cfg.Shortcuts)
|
||||
if seen[name] || name == "" || !regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`).MatchString(name) {
|
||||
canonical[name] = true
|
||||
source, host, qualified := strings.Cut(e.Name, "/")
|
||||
if qualified && withoutPrefix[strings.ToLower(source)] {
|
||||
name = shortcuts.AliasName(host, cfg.Shortcuts)
|
||||
}
|
||||
candidates[i] = name
|
||||
counts[name]++
|
||||
}
|
||||
validAlias := regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
|
||||
for i, e := range entries {
|
||||
name := candidates[i]
|
||||
full := shortcuts.AliasName(e.Name, cfg.Shortcuts)
|
||||
if name != full && (counts[name] > 1 || canonical[name] || seen[name] || !validAlias.MatchString(name)) {
|
||||
name = full
|
||||
}
|
||||
if seen[name] || name == "" || !validAlias.MatchString(name) {
|
||||
continue
|
||||
}
|
||||
seen[name] = true
|
||||
|
||||
@@ -253,3 +253,78 @@ func TestAliasesUseRewriteRules(t *testing.T) {
|
||||
t.Fatalf("entries: %+v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliasesOptionalInstancePrefix(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
NetBoxes: []config.NetBoxConfig{
|
||||
{Name: "OFFICE", DisablePrefix: true},
|
||||
{Name: "lab"},
|
||||
},
|
||||
Shortcuts: config.ShortcutsConfig{
|
||||
Domains: []string{".example.com"},
|
||||
Rewrites: []config.ShortcutRewrite{{Pattern: "^web-", Replace: "app"}},
|
||||
},
|
||||
}
|
||||
entries := hook.BuildAliasEntries([]cache.Entry{
|
||||
{Name: "office/web-01.example.com"},
|
||||
{Name: "lab/web-01.example.com"},
|
||||
}, cfg)
|
||||
got := map[string]string{}
|
||||
for _, e := range entries {
|
||||
got[e.Name] = e.Host
|
||||
// Both output formats must keep the fully qualified destination.
|
||||
for _, shell := range []string{"bash", "zsh", "fish"} {
|
||||
if !strings.Contains(e.Definition(shell), e.Host) {
|
||||
t.Fatalf("%s alias lost its target: %s", shell, e.Definition(shell))
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(got) != 2 || got["app01"] != "office/web-01.example.com" || got["lab_app01"] != "lab/web-01.example.com" {
|
||||
t.Fatalf("unexpected aliases: %+v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliasesWithoutPrefixCollisions(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
NetBoxes: []config.NetBoxConfig{
|
||||
{Name: "office", DisablePrefix: true},
|
||||
{Name: "lab", DisablePrefix: true},
|
||||
},
|
||||
Shell: config.ShellConfig{Alias: "j"},
|
||||
Shortcuts: config.ShortcutsConfig{Domains: []string{".example.com"}},
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
hosts []string
|
||||
want map[string]string
|
||||
}{
|
||||
{"two instances", []string{"office/web.example.com", "lab/web.example.com"},
|
||||
map[string]string{"j": "", "office_web": "office/web.example.com", "lab_web": "lab/web.example.com"}},
|
||||
{"static host", []string{"office/web.example.com", "web"},
|
||||
map[string]string{"j": "", "office_web": "office/web.example.com", "web": "web"}},
|
||||
{"command aliases", []string{"office/j", "office/netssh"},
|
||||
map[string]string{"j": "", "office_j": "office/j", "office_netssh": "office/netssh"}},
|
||||
{"reserved qualified alias", []string{"office/lab_web", "lab/web"},
|
||||
map[string]string{"j": "", "office_lab_web": "office/lab_web", "web": "lab/web"}},
|
||||
{"invalid bare alias", []string{"office/123.example.com"},
|
||||
map[string]string{"j": "", "office_123": "office/123.example.com"}},
|
||||
{"normalized names", []string{"office/web.example.com", "office/web"},
|
||||
map[string]string{"j": "", "office_web": "office/web"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var input []cache.Entry
|
||||
for _, host := range tc.hosts {
|
||||
input = append(input, cache.Entry{Name: host})
|
||||
}
|
||||
entries := hook.BuildAliasEntries(input, cfg)
|
||||
if len(entries) != len(tc.want) {
|
||||
t.Fatalf("aliases: %+v, want %+v", entries, tc.want)
|
||||
}
|
||||
for _, e := range entries {
|
||||
if host, ok := tc.want[e.Name]; !ok || host != e.Host {
|
||||
t.Fatalf("unexpected alias: %+v", e)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user