Improve installer updates and dependency checks and add command aliases
Release / release (push) Successful in 7m57s

This commit is contained in:
Sebastian Unterschütz committed 2026-10-27 18:10:44 +01:00
1 parent 12cc64fdd9
commit 4b28d441b0
13 files changed
+973 -148

No files matched your search

+66 -24
View File
@@ -4,12 +4,17 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/storage"
)
const (
// Marker is the unique string used to detect an existing hook line.
Marker = "netssh shell-init"
Marker = "netssh shell-init"
BlockStart = "# >>> netssh >>>"
BlockEnd = "# <<< netssh <<<"
// Line is the full line written into the shell profile.
Line = "netssh shell-init # netssh cache auto-refresh"
@@ -25,13 +30,13 @@ func ProfilePath(shell string) (string, error) {
case "bash":
return filepath.Join(os.Getenv("HOME"), ".bashrc"), nil
case "zsh":
return filepath.Join(os.Getenv("HOME"), ".zshrc"), nil
case "fish":
configDir, err := os.UserConfigDir()
if err != nil {
configDir = filepath.Join(os.Getenv("HOME"), ".config")
dir := os.Getenv("ZDOTDIR")
if dir == "" {
dir = os.Getenv("HOME")
}
return filepath.Join(configDir, "fish", "config.fish"), nil
return filepath.Join(dir, ".zshrc"), nil
case "fish":
return filepath.Join(FishConfigDir(), "config.fish"), nil
default:
return "", fmt.Errorf("unsupported shell %q — supported: bash, zsh, fish", shell)
}
@@ -49,7 +54,12 @@ func IsInstalled(profilePath string) bool {
if err != nil {
return false
}
return strings.Contains(string(data), Marker)
for _, line := range strings.Split(string(data), "\n") {
if strings.TrimSpace(line) == BlockStart || hookLinePattern.MatchString(strings.TrimSpace(line)) {
return true
}
}
return false
}
// Install appends the hook line to the profile if it is not already present.
@@ -81,23 +91,27 @@ func InstallAliasesSource(profilePath string) (installed bool, err error) {
if err != nil && !os.IsNotExist(err) {
return false, fmt.Errorf("reading profile: %w", err)
}
if strings.Contains(string(data), AliasesMarker) {
return false, nil
for _, line := range strings.Split(string(data), "\n") {
if strings.TrimSpace(line) == BlockStart || aliasesLinePattern.MatchString(strings.TrimSpace(line)) {
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 {
if _, err := fmt.Fprintf(f, "%s\n", "source "+shellQuote(AliasesPath(), "bash")+" 2>/dev/null # netssh aliases"); 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.
var hookLinePattern = regexp.MustCompile(`^(?:[^\s]*/)?netssh\s+shell-init\s*(?:#.*)?$`)
var aliasesLinePattern = regexp.MustCompile(`^(?:source|\.)\s+["']?[^;|&]*netssh/aliases\.sh["']?(?:\s+2>/dev/null)?\s*(?:#.*)?$`)
// Uninstall removes legacy hook lines and complete installer-managed blocks.
// Missing profiles are a no-op; malformed blocks leave the file unchanged.
func Uninstall(profilePath string) (removed bool, err error) {
data, err := os.ReadFile(profilePath)
if os.IsNotExist(err) {
@@ -106,22 +120,50 @@ func Uninstall(profilePath string) (removed bool, err error) {
if err != nil {
return false, fmt.Errorf("reading profile: %w", err)
}
content := string(data)
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) || strings.Contains(line, AliasesMarker) {
inside := false
for _, line := range strings.Split(string(data), "\n") {
value := strings.TrimSpace(line)
switch value {
case BlockStart:
if inside {
return false, fmt.Errorf("nested netssh block in %s", profilePath)
}
inside = true
removed = true
continue
case BlockEnd:
if !inside {
return false, fmt.Errorf("unmatched netssh block in %s", profilePath)
}
inside = false
continue
}
if inside {
continue
}
if hookLinePattern.MatchString(value) || aliasesLinePattern.MatchString(value) {
removed = true
continue
}
kept = append(kept, line)
}
// Collapse triple blank lines that the removal may leave.
if inside {
return false, fmt.Errorf("unclosed netssh block in %s", profilePath)
}
if !removed {
return false, nil
}
cleaned := strings.ReplaceAll(strings.Join(kept, "\n"), "\n\n\n", "\n\n")
if err := os.WriteFile(profilePath, []byte(cleaned), 0o644); err != nil {
target, err := filepath.EvalSymlinks(profilePath)
if err != nil {
return false, err
}
info, err := os.Stat(target)
if err != nil {
return false, err
}
if err := storage.WriteFile(target, []byte(cleaned), info.Mode().Perm()); err != nil {
return false, fmt.Errorf("writing profile: %w", err)
}
return true, nil