refactor: remove service factory wrappers, call store init methods directly

Replace InitNodeService/GetNodeService/GetNodeServiceForUser with thin
NewLocalNodeService/NewRemoteNodeService constructors; move wiring logic
into cmd/root.go (getNodeService helper) and an inline closure in serve.go.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-02 00:56:45 +02:00
parent 03a896d23f
commit 51341eeb84
10 changed files with 37 additions and 40 deletions

View File

@@ -15,7 +15,7 @@ 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) {
svc, err := service.GetNodeService(cfg)
svc, err := getNodeService()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return

View File

@@ -1,7 +1,6 @@
package cmd
import (
"axolotl/service"
"bufio"
"fmt"
"os"
@@ -14,7 +13,7 @@ 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) {
svc, err := service.GetNodeService(cfg)
svc, err := getNodeService()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return

View File

@@ -12,7 +12,7 @@ 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) {
svc, err := service.GetNodeService(cfg)
svc, err := getNodeService()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return

View File

@@ -1,7 +1,6 @@
package cmd
import (
"axolotl/service"
"axolotl/store"
"fmt"
"os"
@@ -25,7 +24,7 @@ var initCmd = &cobra.Command{
fmt.Fprintln(os.Stderr, "database already exists:", dbPath)
os.Exit(1)
}
if err := service.InitNodeService(dbPath); err != nil {
if err := store.InitSQLiteStore(dbPath); err != nil {
fmt.Fprintln(os.Stderr, "failed to initialize:", err)
os.Exit(1)
}

View File

@@ -15,7 +15,7 @@ var lStatus, lPrio, lType, lNamespace, lAssignee, lMention string
var listCmd = &cobra.Command{
Use: "list", Short: "List nodes",
Run: func(cmd *cobra.Command, args []string) {
svc, err := service.GetNodeService(cfg)
svc, err := getNodeService()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return

View File

@@ -11,6 +11,22 @@ import (
"github.com/spf13/cobra"
)
func getNodeService() (service.NodeService, error) {
user := cfg.GetUser()
if user == "" {
return nil, fmt.Errorf("no user configured: run 'ax user set <username>' first")
}
if rc, ok := cfg.GetRemoteConfig(); ok {
base := fmt.Sprintf("http://%s:%d", rc.Host, rc.Port)
return service.NewRemoteNodeService(base, user), nil
}
st, err := store.FindAndOpenSQLiteStore()
if err != nil {
return nil, err
}
return service.NewLocalNodeService(st, user), nil
}
var jsonFlag bool
var cfg *store.Config
var rootCmd = &cobra.Command{Use: "ax", Short: "The axolotl issue tracker"}

View File

@@ -23,7 +23,16 @@ var serveCmd = &cobra.Command{
oidcCfg = oc
}
handler, err := serve.New(service.GetNodeServiceForUser, oidcCfg)
handler, err := serve.New(func(user string) (service.NodeService, error) {
if user == "" {
return nil, fmt.Errorf("user is required")
}
st, err := store.FindOrInitSQLiteStore()
if err != nil {
return nil, err
}
return service.NewLocalNodeService(st, user), nil
}, oidcCfg)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)

View File

@@ -1,7 +1,6 @@
package cmd
import (
"axolotl/service"
"fmt"
"os"
@@ -11,7 +10,7 @@ import (
var showCmd = &cobra.Command{
Use: "show <id>", Short: "Show node details", Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
svc, err := service.GetNodeService(cfg)
svc, err := getNodeService()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return

View File

@@ -20,7 +20,7 @@ var (
var updateCmd = &cobra.Command{
Use: "update <id>", Short: "Update a node", Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
svc, err := service.GetNodeService(cfg)
svc, err := getNodeService()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return

View File

@@ -3,7 +3,6 @@ package service
import (
"axolotl/models"
"axolotl/store"
"fmt"
"net/http"
)
@@ -12,7 +11,6 @@ import (
// invalid state by interacting with this interface alone.
//
// Every NodeService instance is bound to a specific user (see User()).
// GetNodeService returns an error when no user is configured.
type NodeService interface {
// User returns the name/ID of the user this service instance acts on behalf of.
User() string
@@ -68,33 +66,10 @@ type RelInput struct {
Target string // name or node ID; the service resolves names. Empty = tag rel.
}
func InitNodeService(path string) error {
return store.InitSQLiteStore(path)
func NewLocalNodeService(st store.GraphStore, user string) NodeService {
return &nodeServiceImpl{store: st, userID: user}
}
func GetNodeService(cfg *store.Config) (NodeService, error) {
user := cfg.GetUser()
if user == "" {
return nil, fmt.Errorf("no user configured: run 'ax user set <username>' first")
}
if rc, ok := cfg.GetRemoteConfig(); ok {
base := fmt.Sprintf("http://%s:%d", rc.Host, rc.Port)
return &apiClient{base: base, user: user, http: &http.Client{}}, nil
}
st, err := store.FindAndOpenSQLiteStore()
if err != nil {
return nil, err
}
return &nodeServiceImpl{store: st, userID: user}, nil
}
func GetNodeServiceForUser(user string) (NodeService, error) {
if user == "" {
return nil, fmt.Errorf("user is required")
}
st, err := store.FindOrInitSQLiteStore()
if err != nil {
return nil, err
}
return &nodeServiceImpl{store: st, userID: user}, nil
func NewRemoteNodeService(base, user string) NodeService {
return &apiClient{base: base, user: user, http: &http.Client{}}
}