Improve installer updates and dependency checks and add command aliases
Release / release (push) Successful in 7m57s
Release / release (push) Successful in 7m57s
This commit is contained in:
1 parent
12cc64fdd9
commit
4b28d441b0
13 files changed
+973
-148
No files matched your search
+6
-25
@@ -366,8 +366,7 @@ func completionInstallCmd(root *cobra.Command) *cobra.Command {
|
||||
}
|
||||
note = "Make sure ~/.zfunc is in your fpath:\n fpath=(~/.zfunc $fpath)\n autoload -Uz compinit && compinit"
|
||||
case "fish":
|
||||
configDir, _ := os.UserConfigDir()
|
||||
dir = filepath.Join(configDir, "fish", "completions")
|
||||
dir = filepath.Join(hook.FishConfigDir(), "completions")
|
||||
file = filepath.Join(dir, "netssh.fish")
|
||||
gen = func() ([]byte, error) {
|
||||
var buf strings.Builder
|
||||
@@ -570,23 +569,7 @@ func cacheRefreshCmd() *cobra.Command {
|
||||
|
||||
// buildAliasEntries computes the sorted, deduplicated alias list from the cache.
|
||||
func buildAliasEntries(c *cache.Cache, cfg config.Config) []hook.AliasEntry {
|
||||
all := c.All()
|
||||
sort.Slice(all, func(i, j int) bool { return all[i].Name < all[j].Name })
|
||||
|
||||
seen := make(map[string]string)
|
||||
var out []hook.AliasEntry
|
||||
for _, e := range all {
|
||||
name := shortcuts.AliasName(e.Name, cfg.Shortcuts)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
if _, dup := seen[name]; dup {
|
||||
continue
|
||||
}
|
||||
seen[name] = e.Name
|
||||
out = append(out, hook.AliasEntry{Name: name, Host: e.Name})
|
||||
}
|
||||
return out
|
||||
return hook.BuildAliasEntries(c.All(), cfg)
|
||||
}
|
||||
|
||||
func aliasCmd() *cobra.Command {
|
||||
@@ -627,12 +610,12 @@ Or use in a script:
|
||||
case "fish":
|
||||
fmt.Printf("# netssh aliases (%d hosts) — source with: netssh alias --shell fish | source\n", len(entries))
|
||||
for _, e := range entries {
|
||||
fmt.Printf("alias %s 'netssh %s'\n", e.Name, e.Host)
|
||||
fmt.Println(e.Definition("fish"))
|
||||
}
|
||||
default:
|
||||
fmt.Printf("# netssh aliases (%d hosts) — source with: eval \"$(netssh alias)\"\n", len(entries))
|
||||
for _, e := range entries {
|
||||
fmt.Printf("alias %s='netssh %s'\n", e.Name, e.Host)
|
||||
fmt.Println(e.Definition("bash"))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -651,7 +634,7 @@ func shellInitCmd() *cobra.Command {
|
||||
Hidden: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := config.Load()
|
||||
if err != nil || cfg.NetBox.URL == "" {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
c := cache.New(cfg.Cache.Path, cfg.Cache.TTL)
|
||||
@@ -659,9 +642,7 @@ func shellInitCmd() *cobra.Command {
|
||||
|
||||
// Regenerate alias files from the current cache every shell start
|
||||
// so aliases are always up-to-date even before a refresh completes.
|
||||
if entries := buildAliasEntries(c, *cfg); len(entries) > 0 {
|
||||
_ = hook.WriteAliasFiles(entries)
|
||||
}
|
||||
_ = hook.WriteAliasFiles(buildAliasEntries(c, *cfg))
|
||||
|
||||
maybeBackgroundRefresh(cfg, c)
|
||||
return nil
|
||||
|
||||
@@ -2,6 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/hook"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -38,3 +40,36 @@ func TestLocalNotesCLI(t *testing.T) {
|
||||
t.Fatalf("clear: %q, %v", out, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShellInitUpdatesAndRemovesCommandAliasWithoutHosts(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
t.Setenv("NETSSH_CACHE_PATH", filepath.Join(t.TempDir(), "hosts.json"))
|
||||
t.Setenv("NETSSH_NETBOX_URL", "")
|
||||
t.Setenv("NETSSH_SHELL_ALIAS", "j")
|
||||
run := func() {
|
||||
t.Helper()
|
||||
root := rootCmd()
|
||||
root.SetArgs([]string{"shell-init"})
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
run()
|
||||
data, err := os.ReadFile(hook.AliasesPath())
|
||||
if err != nil || !strings.Contains(string(data), "alias j='netssh'") {
|
||||
t.Fatalf("alias missing: %s, %v", data, err)
|
||||
}
|
||||
t.Setenv("NETSSH_SHELL_ALIAS", "k")
|
||||
run()
|
||||
data, _ = os.ReadFile(hook.AliasesPath())
|
||||
if strings.Contains(string(data), "alias j=") || !strings.Contains(string(data), "alias k='netssh'") {
|
||||
t.Fatalf("alias change: %s", data)
|
||||
}
|
||||
t.Setenv("NETSSH_SHELL_ALIAS", "")
|
||||
run()
|
||||
data, _ = os.ReadFile(hook.AliasesPath())
|
||||
if strings.Contains(string(data), "alias k=") {
|
||||
t.Fatalf("stale alias: %s", data)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user