refactor: simplify config into a single load/save with defaults resolved at load time

This commit is contained in:
2026-06-12 01:21:04 +02:00
parent 7b8202b50b
commit 6421c28191
7 changed files with 132 additions and 187 deletions
+35 -11
View File
@@ -4,6 +4,7 @@ import (
"axolotl/store"
"fmt"
"os"
"slices"
"github.com/spf13/cobra"
)
@@ -15,21 +16,32 @@ var aliasCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
w := cmd.OutOrStdout()
if len(args) == 0 {
if aliases, err := cfg.ListAliases(); err == nil {
PrintAliases(w, aliases, jsonFlag)
}
PrintAliases(w, cfg.Aliases, jsonFlag)
return
}
if len(args) == 1 {
a, err := cfg.GetAlias(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, "alias not found:", args[0])
os.Exit(1)
for _, a := range cfg.Aliases {
if a.Name == args[0] {
fmt.Println(a.Command)
return
}
}
fmt.Println(a.Command)
return
fmt.Fprintln(os.Stderr, "alias not found:", args[0])
os.Exit(1)
}
if err := cfg.SetAlias(&store.Alias{Name: args[0], Command: args[1], Description: aliasDesc}); err != nil {
alias := &store.Alias{Name: args[0], Command: args[1], Description: aliasDesc}
found := false
for i, a := range cfg.Aliases {
if a.Name == alias.Name {
cfg.Aliases[i] = alias
found = true
break
}
}
if !found {
cfg.Aliases = append(cfg.Aliases, alias)
}
if err := cfg.Save(); err != nil {
fmt.Fprintln(os.Stderr, "failed to set alias:", err)
} else {
PrintAction(w, "Alias set", args[0], false)
@@ -40,7 +52,19 @@ var aliasCmd = &cobra.Command{
var aliasDelCmd = &cobra.Command{
Use: "del <name>", Short: "Delete an alias", Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := cfg.DeleteAlias(args[0]); err != nil {
found := false
for i, a := range cfg.Aliases {
if a.Name == args[0] {
cfg.Aliases = slices.Delete(cfg.Aliases, i, i+1)
found = true
break
}
}
if !found {
fmt.Fprintln(os.Stderr, "alias not found")
os.Exit(1)
}
if err := cfg.Save(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}