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
+73
-17
@@ -4,7 +4,14 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/cache"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/shortcuts"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/storage"
|
||||
)
|
||||
|
||||
// AliasEntry holds a single shell alias mapping.
|
||||
@@ -15,9 +22,13 @@ type AliasEntry struct {
|
||||
|
||||
// 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")
|
||||
cacheDir := os.Getenv("XDG_CACHE_HOME")
|
||||
if cacheDir == "" {
|
||||
var err error
|
||||
cacheDir, err = os.UserCacheDir()
|
||||
if err != nil {
|
||||
cacheDir = filepath.Join(os.Getenv("HOME"), ".cache")
|
||||
}
|
||||
}
|
||||
return filepath.Join(cacheDir, "netssh", "aliases.sh")
|
||||
}
|
||||
@@ -25,11 +36,7 @@ func AliasesPath() string {
|
||||
// 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")
|
||||
return filepath.Join(FishConfigDir(), "conf.d", "netssh.fish")
|
||||
}
|
||||
|
||||
// WriteAliasFiles writes alias definitions to disk for all supported shells.
|
||||
@@ -54,18 +61,14 @@ func writeShAliasFile(entries []AliasEntry) error {
|
||||
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)
|
||||
b.WriteString(e.Definition("bash") + "\n")
|
||||
}
|
||||
return os.WriteFile(p, []byte(b.String()), 0o644)
|
||||
return storage.WriteFile(p, []byte(b.String()), 0o600)
|
||||
}
|
||||
|
||||
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 {
|
||||
if _, err := os.Stat(FishConfigDir()); err != nil {
|
||||
return
|
||||
}
|
||||
p := FishAliasesPath()
|
||||
@@ -76,7 +79,60 @@ func writeFishAliasFile(entries []AliasEntry) {
|
||||
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)
|
||||
b.WriteString(e.Definition("fish") + "\n")
|
||||
}
|
||||
os.WriteFile(p, []byte(b.String()), 0o644) //nolint:errcheck
|
||||
storage.WriteFile(p, []byte(b.String()), 0o600) //nolint:errcheck
|
||||
}
|
||||
|
||||
// Empty Host represents an alias for the netssh command itself.
|
||||
func (e AliasEntry) Definition(shell string) string {
|
||||
command := "netssh"
|
||||
if e.Host != "" {
|
||||
host := e.Host
|
||||
if !regexp.MustCompile(`^[a-zA-Z0-9_.:-]+$`).MatchString(host) {
|
||||
host = shellQuote(host, shell)
|
||||
}
|
||||
command += " " + host
|
||||
}
|
||||
if shell == "fish" {
|
||||
return fmt.Sprintf("alias %s %s", e.Name, shellQuote(command, shell))
|
||||
}
|
||||
return fmt.Sprintf("alias %s=%s", e.Name, shellQuote(command, shell))
|
||||
}
|
||||
|
||||
func shellQuote(value, shell string) string {
|
||||
if shell == "fish" {
|
||||
value = strings.ReplaceAll(value, `\`, `\\`)
|
||||
return "'" + strings.ReplaceAll(value, "'", `\'`) + "'"
|
||||
}
|
||||
return "'" + strings.ReplaceAll(value, "'", `'\''`) + "'"
|
||||
}
|
||||
|
||||
// BuildAliasEntries gives the configured command alias priority over host aliases.
|
||||
func BuildAliasEntries(entries []cache.Entry, cfg config.Config) []AliasEntry {
|
||||
sort.Slice(entries, func(i, j int) bool { return entries[i].Name < entries[j].Name })
|
||||
seen := map[string]bool{"netssh": true}
|
||||
var out []AliasEntry
|
||||
if cfg.Shell.Alias != "" && config.ValidateCommandAlias(cfg.Shell.Alias) == nil {
|
||||
seen[cfg.Shell.Alias] = true
|
||||
out = append(out, AliasEntry{Name: cfg.Shell.Alias})
|
||||
}
|
||||
for _, 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) {
|
||||
continue
|
||||
}
|
||||
seen[name] = true
|
||||
out = append(out, AliasEntry{Name: name, Host: e.Name})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// FishConfigDir follows fish's XDG layout on both Linux and macOS.
|
||||
func FishConfigDir() string {
|
||||
dir := os.Getenv("XDG_CONFIG_HOME")
|
||||
if dir == "" {
|
||||
dir = filepath.Join(os.Getenv("HOME"), ".config")
|
||||
}
|
||||
return filepath.Join(dir, "fish")
|
||||
}
|
||||
Reference in new issue
Block a user