refactor: remove db package and move database logic to service layer

This commit is contained in:
2026-03-29 21:24:09 +02:00
parent 6ff013dd2a
commit 4ebcb88628
16 changed files with 163 additions and 217 deletions

View File

@@ -1,7 +1,6 @@
package cmd
import (
"axolotl/db"
"axolotl/models"
"axolotl/output"
"axolotl/service"
@@ -19,12 +18,6 @@ var cTags, cRels []string
var addCmd = &cobra.Command{
Use: "add <title>", Short: "Create a new node", Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
d, err := db.GetDB()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
if !slices.ContainsFunc(cTags, func(e string) bool { return strings.HasPrefix(e, "_type::") }) {
cTags = append(cTags, "_type::issue")
}
@@ -49,11 +42,13 @@ var addCmd = &cobra.Command{
rels[models.RelInNamespace] = append(rels[models.RelInNamespace], cfg.GetUser())
}
svc := service.NewSQLiteNodeService(d.DB, cfg.GetUser())
if n, err := svc.Create(args[0], cContent, cDue, cTags, rels); err != nil {
svc, err := service.GetNodeService(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to create:", err)
} else if n, err := svc.Create(args[0], cContent, cDue, cTags, rels); err != nil {
fmt.Fprintln(os.Stderr, "failed to create:", err)
} else {
output.PrintNode(cmd.OutOrStdout(), n, jsonFlag)
output.PrintNode(cmd.OutOrStdout(), svc, n, jsonFlag)
}
},
}

View File

@@ -1,7 +1,6 @@
package cmd
import (
"axolotl/db"
"axolotl/output"
"axolotl/service"
"bufio"
@@ -16,12 +15,11 @@ var dForce bool
var delCmd = &cobra.Command{
Use: "del <id>", Short: "Delete a node", Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
d, err := db.GetDB()
svc, err := service.GetNodeService(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, "failed to create service: %v", err)
return
}
svc := service.NewSQLiteNodeService(d.DB, cfg.GetUser())
n, err := svc.GetByID(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, " node not found:", args[0])

View File

@@ -1,7 +1,6 @@
package cmd
import (
"axolotl/db"
"axolotl/output"
"axolotl/service"
"fmt"
@@ -14,12 +13,11 @@ import (
var editCmd = &cobra.Command{
Use: "edit <id>", Short: "Edit node content in $EDITOR", Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
d, err := db.GetDB()
svc, err := service.GetNodeService(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
svc := service.NewSQLiteNodeService(d.DB, cfg.GetUser())
n, err := svc.GetByID(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, "node not found:", args[0])
@@ -53,7 +51,7 @@ var editCmd = &cobra.Command{
return
}
n, _ = svc.GetByID(args[0])
output.PrintNode(cmd.OutOrStdout(), n, jsonFlag)
output.PrintNode(cmd.OutOrStdout(), svc, n, jsonFlag)
} else {
fmt.Fprintln(os.Stderr, "failed to read temp file:", err)
}

View File

@@ -1,8 +1,8 @@
package cmd
import (
"axolotl/db"
"axolotl/output"
"axolotl/service"
"fmt"
"os"
"path/filepath"
@@ -22,7 +22,7 @@ var initCmd = &cobra.Command{
fmt.Fprintln(os.Stderr, "database already exists:", dbPath)
os.Exit(1)
}
if err := db.Init(dbPath); err != nil {
if err := service.InitNodeService(dbPath); err != nil {
fmt.Fprintln(os.Stderr, "failed to initialize:", err)
os.Exit(1)
}

View File

@@ -1,7 +1,6 @@
package cmd
import (
"axolotl/db"
"axolotl/output"
"axolotl/service"
"fmt"
@@ -17,12 +16,11 @@ var lMention string
var listCmd = &cobra.Command{
Use: "list", Short: "List nodes",
Run: func(cmd *cobra.Command, args []string) {
d, err := db.GetDB()
svc, err := service.GetNodeService(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
svc := service.NewSQLiteNodeService(d.DB, cfg.GetUser())
opts := []service.ListOption{}
if len(lTags) > 0 {
opts = append(opts, service.WithTags(lTags...))
@@ -35,7 +33,7 @@ var listCmd = &cobra.Command{
}
if nodes, err := svc.List(opts...); err == nil {
output.PrintNodes(cmd.OutOrStdout(), nodes, jsonFlag)
output.PrintNodes(cmd.OutOrStdout(), svc, nodes, jsonFlag)
} else {
fmt.Fprintf(os.Stderr, "err: %v\n", err)
}

View File

@@ -15,7 +15,7 @@ var rootCmd = &cobra.Command{Use: "ax", Short: "The axolotl issue tracker"}
func Execute() {
var err error
cfg, err = service.LoadConfig()
cfg, err = service.LoadConfigFile()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to load config:", err)
os.Exit(1)
@@ -27,18 +27,48 @@ func Execute() {
}
}
func expandAlias(alias *service.Alias, args []string, currentUser string) []string {
cmd := alias.Command
cmd = strings.ReplaceAll(cmd, "$me", currentUser)
parts := strings.Fields(cmd)
var result []string
for _, part := range parts {
if part == "$@" {
result = append(result, args...)
continue
}
hasCatchAll := strings.Contains(part, "$@")
replaced := part
if hasCatchAll {
replaced = strings.ReplaceAll(replaced, "$@", strings.Join(args, " "))
}
for i := len(args) - 1; i >= 0; i-- {
placeholder := fmt.Sprintf("$%d", i+1)
replaced = strings.ReplaceAll(replaced, placeholder, args[i])
}
result = append(result, replaced)
}
return result
}
func registerAliasCommands() {
rootCmd.AddGroup(&cobra.Group{ID: "aliases", Title: "Aliases:"})
aliases, _ := cfg.ListAliases()
for _, a := range aliases {
a := a
rootCmd.AddCommand(&cobra.Command{
Use: a.Name,
Short: a.Description,
GroupID: "aliases",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
expanded := service.ExpandAlias(a, args, cfg.GetUser())
expanded := expandAlias(a, args, cfg.GetUser())
rootCmd.SetArgs(transformArgs(expanded))
if err := rootCmd.Execute(); err != nil {
os.Exit(1)

View File

@@ -1,7 +1,6 @@
package cmd
import (
"axolotl/db"
"axolotl/output"
"axolotl/service"
"fmt"
@@ -13,14 +12,13 @@ import (
var showCmd = &cobra.Command{
Use: "show <id>", Short: "Show node details", Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
d, err := db.GetDB()
svc, err := service.GetNodeService(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
svc := service.NewSQLiteNodeService(d.DB, cfg.GetUser())
if n, err := svc.GetByID(args[0]); err == nil {
output.PrintNode(cmd.OutOrStdout(), n, jsonFlag)
output.PrintNode(cmd.OutOrStdout(), svc, n, jsonFlag)
} else {
fmt.Fprintln(os.Stderr, "node not found:", args[0])
}

View File

@@ -1,7 +1,6 @@
package cmd
import (
"axolotl/db"
"axolotl/models"
"axolotl/output"
"axolotl/service"
@@ -14,7 +13,7 @@ import (
)
var (
uTitle, uContent, uDue string
uTitle, uContent, uDue string
uClearDue bool
uAddTags, uRmTags, uAddRels, uRmRels []string
)
@@ -22,12 +21,11 @@ var (
var updateCmd = &cobra.Command{
Use: "update <id>", Short: "Update a node", Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
d, err := db.GetDB()
svc, err := service.GetNodeService(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
svc := service.NewSQLiteNodeService(d.DB, cfg.GetUser())
node, err := svc.GetByID(args[0])
if err != nil {
@@ -65,7 +63,7 @@ var updateCmd = &cobra.Command{
} else if slices.Contains(uAddTags, "_status::open") {
uRmTags = append(uRmTags, "_status::done")
}
for _, prefix := range []string{"_type::", "_status::", "_prio::", "_namespace::"} {
if slices.ContainsFunc(uAddTags, func(e string) bool { return strings.HasPrefix(e, prefix) }) {
for _, existing := range node.Tags {
@@ -76,7 +74,6 @@ var updateCmd = &cobra.Command{
}
}
if cmd.Flags().Changed("title") {
node.Title = uTitle
}
@@ -117,7 +114,7 @@ var updateCmd = &cobra.Command{
return
}
if n, err := svc.GetByID(args[0]); err == nil {
output.PrintNode(cmd.OutOrStdout(), n, jsonFlag)
output.PrintNode(cmd.OutOrStdout(), svc, n, jsonFlag)
} else {
fmt.Fprintln(os.Stderr, "failed to fetch node:", err)
}