fa646f25a6
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.
83 lines
2.6 KiB
Go
83 lines
2.6 KiB
Go
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
|
|
}
|