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
@@ -10,8 +10,11 @@ import (
|
||||
|
||||
"github.com/charmbracelet/huh"
|
||||
|
||||
"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/hook"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/storage"
|
||||
)
|
||||
|
||||
// RunWizard runs the interactive setup form, pre-filled with any existing cfg values.
|
||||
@@ -30,6 +33,7 @@ func RunWizard(cfg *config.Config) error {
|
||||
cacheTTL := strconv.Itoa(cfg.Cache.TTL)
|
||||
shortcutDomains := strings.Join(cfg.Shortcuts.Domains, ", ")
|
||||
stripHyphens := cfg.Shortcuts.StripHyphens
|
||||
commandAlias := cfg.Shell.Alias
|
||||
|
||||
if strategiesRaw == "" {
|
||||
strategiesRaw = "primary_ip"
|
||||
@@ -122,6 +126,12 @@ func RunWizard(cfg *config.Config) error {
|
||||
Title("Strip hyphens").
|
||||
Description("When enabled, fsn1-web01.example.com can be accessed as fsn1web01.\nOnly works for hosts already in the cache.").
|
||||
Value(&stripHyphens),
|
||||
huh.NewInput().
|
||||
Title("Command alias").
|
||||
Description("Optional alias for netssh itself, e.g. j. Leave empty to disable.").
|
||||
Placeholder("j").
|
||||
Value(&commandAlias).
|
||||
Validate(func(s string) error { return config.ValidateCommandAlias(strings.TrimSpace(s)) }),
|
||||
).Title("Shortcuts"),
|
||||
)
|
||||
|
||||
@@ -165,6 +175,7 @@ func RunWizard(cfg *config.Config) error {
|
||||
}
|
||||
|
||||
out := config.Config{
|
||||
Shell: config.ShellConfig{Alias: strings.TrimSpace(commandAlias)},
|
||||
NetBox: config.NetBoxConfig{
|
||||
URL: strings.TrimRight(strings.TrimSpace(url), "/"),
|
||||
Token: strings.TrimSpace(token),
|
||||
@@ -180,7 +191,8 @@ func RunWizard(cfg *config.Config) error {
|
||||
InterfaceName: strings.TrimSpace(interfaceName),
|
||||
},
|
||||
Cache: config.CacheConfig{
|
||||
TTL: ttl,
|
||||
TTL: ttl,
|
||||
Path: cfg.Cache.Path,
|
||||
},
|
||||
Shortcuts: config.ShortcutsConfig{
|
||||
Domains: domainList,
|
||||
@@ -188,7 +200,38 @@ func RunWizard(cfg *config.Config) error {
|
||||
},
|
||||
}
|
||||
|
||||
return save(out)
|
||||
if err := save(out); err != nil {
|
||||
return err
|
||||
}
|
||||
loaded, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c := cache.New(loaded.Cache.Path, loaded.Cache.TTL)
|
||||
if err := c.Load(); err != nil {
|
||||
return fmt.Errorf("updating aliases: %w", err)
|
||||
}
|
||||
if err := hook.WriteAliasFiles(hook.BuildAliasEntries(c.All(), *loaded)); err != nil {
|
||||
return err
|
||||
}
|
||||
if loaded.Shell.Alias != "" {
|
||||
shell := filepath.Base(os.Getenv("SHELL"))
|
||||
profile, err := hook.ProfilePath(shell)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Run 'netssh hook install --shell bash|zsh|fish' to activate your alias.\n")
|
||||
return nil
|
||||
}
|
||||
if _, err := hook.Install(profile); err != nil {
|
||||
return err
|
||||
}
|
||||
if shell != "fish" {
|
||||
if _, err := hook.InstallAliasesSource(profile); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fmt.Fprintln(os.Stderr, hook.ReloadNote(profile))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var knownStrategies = map[string]bool{
|
||||
@@ -220,6 +263,9 @@ func parseStrategies(s string) []string {
|
||||
}
|
||||
|
||||
func save(cfg config.Config) error {
|
||||
if err := config.ValidateCommandAlias(cfg.Shell.Alias); err != nil {
|
||||
return err
|
||||
}
|
||||
path := config.Path()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
return fmt.Errorf("creating config dir: %w", err)
|
||||
@@ -248,6 +294,9 @@ func save(cfg config.Config) error {
|
||||
|
||||
b.WriteString("\ncache:\n")
|
||||
fmt.Fprintf(&b, " ttl: %d\n", cfg.Cache.TTL)
|
||||
if cfg.Cache.Path != "" {
|
||||
fmt.Fprintf(&b, " path: %q\n", cfg.Cache.Path)
|
||||
}
|
||||
|
||||
b.WriteString("\nssh:\n")
|
||||
if cfg.SSH.DefaultUser != "" {
|
||||
@@ -270,7 +319,11 @@ func save(cfg config.Config) error {
|
||||
}
|
||||
}
|
||||
|
||||
if err := os.WriteFile(path, []byte(b.String()), 0o600); err != nil {
|
||||
if cfg.Shell.Alias != "" {
|
||||
fmt.Fprintf(&b, "\nshell:\n alias: %q\n", cfg.Shell.Alias)
|
||||
}
|
||||
|
||||
if err := storage.WriteFile(path, []byte(b.String()), 0o600); err != nil {
|
||||
return fmt.Errorf("writing config: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -366,3 +366,23 @@ func TestSave_RoundtripViaLoad(t *testing.T) {
|
||||
t.Errorf("TTL: got %d, want %d", loaded.Cache.TTL, original.Cache.TTL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveCommandAliasRoundtripAndDisable(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
cfg := config.Config{Shell: config.ShellConfig{Alias: "j"}, Cache: config.CacheConfig{Path: filepath.Join(t.TempDir(), "hosts.json")}}
|
||||
if err := save(cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err := config.Load()
|
||||
if err != nil || loaded.Shell.Alias != "j" || loaded.Cache.Path != cfg.Cache.Path {
|
||||
t.Fatalf("roundtrip: %+v, %v", loaded, err)
|
||||
}
|
||||
cfg.Shell.Alias = ""
|
||||
if err := save(cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err = config.Load()
|
||||
if err != nil || loaded.Shell.Alias != "" {
|
||||
t.Fatalf("disable: %+v, %v", loaded, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user