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
@@ -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