feat: add alias file generation and management for shell hooks
Release / release (push) Successful in 49s
Release / release (push) Successful in 49s
- **Aliases**: Generate shell alias files (`aliases.sh` for bash/zsh, `netssh.fish` for fish) from cached hosts. Regenerate on each shell startup and cache refresh to keep aliases updated. - **Hooks**: Extend shell hook functionality to include alias file support. Install and uninstall commands updated for bash, zsh, and fish. - **Tests**: Add unit tests to verify alias file generation, path resolution, and idempotent hook installation. - **Docs**: Update README with instructions for alias file usage, installation, and relation to hooks.
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package hook
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AliasEntry holds a single shell alias mapping.
|
||||
type AliasEntry struct {
|
||||
Name string // short alias name (e.g. "web01")
|
||||
Host string // canonical NetBox hostname (e.g. "web01.example.com")
|
||||
}
|
||||
|
||||
// AliasesPath returns the path for the bash/zsh aliases file.
|
||||
func AliasesPath() string {
|
||||
cacheDir, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
cacheDir = filepath.Join(os.Getenv("HOME"), ".cache")
|
||||
}
|
||||
return filepath.Join(cacheDir, "netssh", "aliases.sh")
|
||||
}
|
||||
|
||||
// FishAliasesPath returns the path for the fish conf.d aliases file.
|
||||
// Fish auto-sources every file in conf.d, so no explicit source line is needed.
|
||||
func FishAliasesPath() string {
|
||||
configDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
configDir = filepath.Join(os.Getenv("HOME"), ".config")
|
||||
}
|
||||
return filepath.Join(configDir, "fish", "conf.d", "netssh.fish")
|
||||
}
|
||||
|
||||
// WriteAliasFiles writes alias definitions to disk for all supported shells.
|
||||
//
|
||||
// The bash/zsh file (~/.cache/netssh/aliases.sh) is always written.
|
||||
// The fish file (~/.config/fish/conf.d/netssh.fish) is written only when
|
||||
// ~/.config/fish/ already exists (i.e. fish is configured on this system).
|
||||
func WriteAliasFiles(entries []AliasEntry) error {
|
||||
if err := writeShAliasFile(entries); err != nil {
|
||||
return err
|
||||
}
|
||||
writeFishAliasFile(entries) // best-effort — never blocks the caller on failure
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeShAliasFile(entries []AliasEntry) error {
|
||||
p := AliasesPath()
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
return fmt.Errorf("creating aliases dir: %w", err)
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("# netssh aliases — generated automatically, do not edit\n")
|
||||
b.WriteString("# Regenerated on every shell start and after 'netssh cache refresh'.\n")
|
||||
for _, e := range entries {
|
||||
fmt.Fprintf(&b, "alias %s='netssh %s'\n", e.Name, e.Host)
|
||||
}
|
||||
return os.WriteFile(p, []byte(b.String()), 0o644)
|
||||
}
|
||||
|
||||
func writeFishAliasFile(entries []AliasEntry) {
|
||||
configDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// Only write if fish is configured on this system.
|
||||
if _, err := os.Stat(filepath.Join(configDir, "fish")); err != nil {
|
||||
return
|
||||
}
|
||||
p := FishAliasesPath()
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
return
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("# netssh aliases — generated automatically, do not edit\n")
|
||||
b.WriteString("# Regenerated on every shell start and after 'netssh cache refresh'.\n")
|
||||
for _, e := range entries {
|
||||
fmt.Fprintf(&b, "alias %s 'netssh %s'\n", e.Name, e.Host)
|
||||
}
|
||||
os.WriteFile(p, []byte(b.String()), 0o644) //nolint:errcheck
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
package hook_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/hook"
|
||||
)
|
||||
|
||||
func TestAliasesPath_UnderCacheDir(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
orig := os.Getenv("XDG_CACHE_HOME")
|
||||
os.Setenv("XDG_CACHE_HOME", dir)
|
||||
defer os.Setenv("XDG_CACHE_HOME", orig)
|
||||
|
||||
p := hook.AliasesPath()
|
||||
if !strings.HasPrefix(p, dir) {
|
||||
t.Errorf("AliasesPath should be under XDG_CACHE_HOME, got %q", p)
|
||||
}
|
||||
if !strings.HasSuffix(p, "aliases.sh") {
|
||||
t.Errorf("AliasesPath should end with aliases.sh, got %q", p)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFishAliasesPath_UnderConfigDir(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
orig := os.Getenv("XDG_CONFIG_HOME")
|
||||
os.Setenv("XDG_CONFIG_HOME", dir)
|
||||
defer os.Setenv("XDG_CONFIG_HOME", orig)
|
||||
|
||||
p := hook.FishAliasesPath()
|
||||
if !strings.HasPrefix(p, dir) {
|
||||
t.Errorf("FishAliasesPath should be under XDG_CONFIG_HOME, got %q", p)
|
||||
}
|
||||
if !strings.HasSuffix(p, "netssh.fish") {
|
||||
t.Errorf("FishAliasesPath should end with netssh.fish, got %q", p)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWriteAliasFiles_ShFile verifies the bash/zsh file is always written.
|
||||
func TestWriteAliasFiles_ShFile(t *testing.T) {
|
||||
cacheDir := t.TempDir()
|
||||
orig := os.Getenv("XDG_CACHE_HOME")
|
||||
os.Setenv("XDG_CACHE_HOME", cacheDir)
|
||||
defer os.Setenv("XDG_CACHE_HOME", orig)
|
||||
|
||||
entries := []hook.AliasEntry{
|
||||
{Name: "web01", Host: "web01.example.com"},
|
||||
{Name: "db01", Host: "db01.example.com"},
|
||||
}
|
||||
if err := hook.WriteAliasFiles(entries); err != nil {
|
||||
t.Fatalf("WriteAliasFiles: %v", err)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(hook.AliasesPath())
|
||||
if err != nil {
|
||||
t.Fatalf("aliases.sh not created: %v", err)
|
||||
}
|
||||
content := string(data)
|
||||
|
||||
for _, want := range []string{
|
||||
"alias web01='netssh web01.example.com'",
|
||||
"alias db01='netssh db01.example.com'",
|
||||
} {
|
||||
if !strings.Contains(content, want) {
|
||||
t.Errorf("aliases.sh missing %q\ncontent:\n%s", want, content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestWriteAliasFiles_FishFile verifies the fish file is written when fish config dir exists.
|
||||
func TestWriteAliasFiles_FishFile(t *testing.T) {
|
||||
configDir := t.TempDir()
|
||||
orig := os.Getenv("XDG_CONFIG_HOME")
|
||||
os.Setenv("XDG_CONFIG_HOME", configDir)
|
||||
defer os.Setenv("XDG_CONFIG_HOME", orig)
|
||||
|
||||
// Create the fish config directory so WriteAliasFiles knows fish is set up.
|
||||
fishDir := filepath.Join(configDir, "fish")
|
||||
os.MkdirAll(fishDir, 0o755)
|
||||
|
||||
cacheDir := t.TempDir()
|
||||
origCache := os.Getenv("XDG_CACHE_HOME")
|
||||
os.Setenv("XDG_CACHE_HOME", cacheDir)
|
||||
defer os.Setenv("XDG_CACHE_HOME", origCache)
|
||||
|
||||
entries := []hook.AliasEntry{{Name: "web01", Host: "web01.example.com"}}
|
||||
if err := hook.WriteAliasFiles(entries); err != nil {
|
||||
t.Fatalf("WriteAliasFiles: %v", err)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(hook.FishAliasesPath())
|
||||
if err != nil {
|
||||
t.Fatalf("netssh.fish not created: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(data), "alias web01 'netssh web01.example.com'") {
|
||||
t.Errorf("fish alias file missing expected line\ncontent:\n%s", string(data))
|
||||
}
|
||||
}
|
||||
|
||||
// TestWriteAliasFiles_SkipsFishWhenNotConfigured verifies that fish file is NOT written
|
||||
// when fish is not set up (no ~/.config/fish directory).
|
||||
func TestWriteAliasFiles_SkipsFishWhenNotConfigured(t *testing.T) {
|
||||
configDir := t.TempDir() // no fish subdir
|
||||
orig := os.Getenv("XDG_CONFIG_HOME")
|
||||
os.Setenv("XDG_CONFIG_HOME", configDir)
|
||||
defer os.Setenv("XDG_CONFIG_HOME", orig)
|
||||
|
||||
cacheDir := t.TempDir()
|
||||
origCache := os.Getenv("XDG_CACHE_HOME")
|
||||
os.Setenv("XDG_CACHE_HOME", cacheDir)
|
||||
defer os.Setenv("XDG_CACHE_HOME", origCache)
|
||||
|
||||
entries := []hook.AliasEntry{{Name: "web01", Host: "web01.example.com"}}
|
||||
if err := hook.WriteAliasFiles(entries); err != nil {
|
||||
t.Fatalf("WriteAliasFiles: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(hook.FishAliasesPath()); err == nil {
|
||||
t.Error("fish alias file should not be created when fish is not configured")
|
||||
}
|
||||
}
|
||||
|
||||
// TestWriteAliasFiles_EmptyEntries verifies an empty (but valid) file is written.
|
||||
func TestWriteAliasFiles_EmptyEntries(t *testing.T) {
|
||||
cacheDir := t.TempDir()
|
||||
orig := os.Getenv("XDG_CACHE_HOME")
|
||||
os.Setenv("XDG_CACHE_HOME", cacheDir)
|
||||
defer os.Setenv("XDG_CACHE_HOME", orig)
|
||||
|
||||
if err := hook.WriteAliasFiles(nil); err != nil {
|
||||
t.Fatalf("WriteAliasFiles with nil entries: %v", err)
|
||||
}
|
||||
data, err := os.ReadFile(hook.AliasesPath())
|
||||
if err != nil {
|
||||
t.Fatalf("aliases.sh not created: %v", err)
|
||||
}
|
||||
// Should contain only the header comment, no alias lines.
|
||||
if strings.Contains(string(data), "alias ") {
|
||||
t.Error("aliases.sh should have no alias lines for empty entries")
|
||||
}
|
||||
}
|
||||
|
||||
// --- InstallAliasesSource ---
|
||||
|
||||
func TestInstallAliasesSource_Fresh(t *testing.T) {
|
||||
profile := writeProfile(t, "existing line\n")
|
||||
|
||||
installed, err := hook.InstallAliasesSource(profile)
|
||||
if err != nil {
|
||||
t.Fatalf("InstallAliasesSource: %v", err)
|
||||
}
|
||||
if !installed {
|
||||
t.Error("should report installed=true on first install")
|
||||
}
|
||||
|
||||
data, _ := os.ReadFile(profile)
|
||||
if !strings.Contains(string(data), hook.AliasesMarker) {
|
||||
t.Error("profile should contain aliases source line")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallAliasesSource_Idempotent(t *testing.T) {
|
||||
profile := writeProfile(t, "")
|
||||
|
||||
hook.InstallAliasesSource(profile)
|
||||
installed, err := hook.InstallAliasesSource(profile)
|
||||
if err != nil {
|
||||
t.Fatalf("second InstallAliasesSource: %v", err)
|
||||
}
|
||||
if installed {
|
||||
t.Error("second call should report installed=false")
|
||||
}
|
||||
|
||||
data, _ := os.ReadFile(profile)
|
||||
if count := strings.Count(string(data), hook.AliasesMarker); count != 1 {
|
||||
t.Errorf("aliases source line should appear once, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUninstall_AlsoRemovesAliasesSourceLine verifies that Uninstall removes both lines.
|
||||
func TestUninstall_AlsoRemovesAliasesSourceLine(t *testing.T) {
|
||||
content := "export PATH=$PATH\n" +
|
||||
hook.Line + "\n" +
|
||||
hook.AliasesSourceLine + "\n" +
|
||||
"export FOO=bar\n"
|
||||
profile := writeProfile(t, content)
|
||||
|
||||
removed, err := hook.Uninstall(profile)
|
||||
if err != nil {
|
||||
t.Fatalf("Uninstall: %v", err)
|
||||
}
|
||||
if !removed {
|
||||
t.Error("should report removed=true")
|
||||
}
|
||||
|
||||
data, _ := os.ReadFile(profile)
|
||||
s := string(data)
|
||||
if strings.Contains(s, hook.Marker) {
|
||||
t.Error("shell-init line should be removed")
|
||||
}
|
||||
if strings.Contains(s, hook.AliasesMarker) {
|
||||
t.Error("aliases source line should be removed")
|
||||
}
|
||||
if !strings.Contains(s, "export PATH") || !strings.Contains(s, "export FOO") {
|
||||
t.Error("other content should be preserved")
|
||||
}
|
||||
}
|
||||
+32
-5
@@ -8,10 +8,15 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// Marker is the unique string used to detect an existing hook installation.
|
||||
// Marker is the unique string used to detect an existing hook line.
|
||||
Marker = "netssh shell-init"
|
||||
// Line is the full line written into the shell profile.
|
||||
Line = "netssh shell-init # netssh cache auto-refresh"
|
||||
|
||||
// AliasesMarker is the unique string used to detect the aliases source line.
|
||||
AliasesMarker = "netssh/aliases.sh"
|
||||
// AliasesSourceLine is the bash/zsh line that sources the generated aliases file.
|
||||
AliasesSourceLine = "source ~/.cache/netssh/aliases.sh 2>/dev/null # netssh aliases"
|
||||
)
|
||||
|
||||
// ProfilePath returns the canonical shell profile path for the given shell name.
|
||||
@@ -68,8 +73,30 @@ func Install(profilePath string) (installed bool, err error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Uninstall removes the hook line from the profile.
|
||||
// Returns (true, nil) when removed, (false, nil) when the hook was not found.
|
||||
// InstallAliasesSource appends the aliases source line to a bash/zsh profile.
|
||||
// This is a no-op for fish (which uses conf.d auto-sourcing instead).
|
||||
// Returns (true, nil) when newly added, (false, nil) when already present.
|
||||
func InstallAliasesSource(profilePath string) (installed bool, err error) {
|
||||
data, err := os.ReadFile(profilePath)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return false, fmt.Errorf("reading profile: %w", err)
|
||||
}
|
||||
if strings.Contains(string(data), AliasesMarker) {
|
||||
return false, nil
|
||||
}
|
||||
f, err := os.OpenFile(profilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("opening profile: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
if _, err := fmt.Fprintf(f, "%s\n", AliasesSourceLine); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Uninstall removes the hook line and the aliases source line from the profile.
|
||||
// Returns (true, nil) when something was removed, (false, nil) when nothing was found.
|
||||
// Returns (false, nil) — not an error — when the file does not exist.
|
||||
func Uninstall(profilePath string) (removed bool, err error) {
|
||||
data, err := os.ReadFile(profilePath)
|
||||
@@ -80,13 +107,13 @@ func Uninstall(profilePath string) (removed bool, err error) {
|
||||
return false, fmt.Errorf("reading profile: %w", err)
|
||||
}
|
||||
content := string(data)
|
||||
if !strings.Contains(content, Marker) {
|
||||
if !strings.Contains(content, Marker) && !strings.Contains(content, AliasesMarker) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
var kept []string
|
||||
for _, line := range strings.Split(content, "\n") {
|
||||
if strings.Contains(line, Marker) {
|
||||
if strings.Contains(line, Marker) || strings.Contains(line, AliasesMarker) {
|
||||
continue
|
||||
}
|
||||
kept = append(kept, line)
|
||||
|
||||
Reference in New Issue
Block a user