refactor: clean up NodeService interface; move all integrity logic behind it

This commit is contained in:
2026-03-31 15:55:47 +02:00
parent ed9117951f
commit 8d831d131b
9 changed files with 484 additions and 388 deletions

View File

@@ -1,18 +1,15 @@
package cmd
import (
"axolotl/models"
"axolotl/output"
"axolotl/service"
"fmt"
"os"
"slices"
"strings"
"github.com/spf13/cobra"
)
var cDue, cContent string
var cDue, cContent, cStatus, cPrio, cType, cNamespace, cAssignee string
var cTags, cRels []string
var addCmd = &cobra.Command{
@@ -20,61 +17,51 @@ var addCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
svc, err := service.GetNodeService(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to create:", err)
fmt.Fprintln(os.Stderr, err)
return
}
// default relations
if !slices.ContainsFunc(cRels, func(e string) bool { return strings.HasPrefix(e, "in_namespace:") }) {
cRels = append(cRels, "in_namespace:"+cfg.GetUser())
input := service.AddInput{
Title: args[0],
Content: cContent,
DueDate: cDue,
Type: cType,
Status: cStatus,
Priority: cPrio,
Namespace: cNamespace,
Assignee: cAssignee,
Tags: cTags,
}
// parse relations
rels := make(map[models.RelType][]string)
for _, r := range cRels {
rel, err := parseRelFlag(svc, r)
ri, err := parseRelInput(r)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
rels[rel.Type] = append(rels[rel.Type], rel.Target)
input.Rels = append(input.Rels, ri)
}
// create
n, err := svc.Create(args[0], cContent, cDue, nil, rels)
n, err := svc.Add(input)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to create:", err)
return
}
for _, t := range cTags {
n.AddTag(t)
}
// default tags
if !slices.ContainsFunc(cTags, func(e string) bool { return strings.HasPrefix(e, "_type::") }) {
n.AddTag("_type::issue")
}
if n.HasTag("_type::issue") && !slices.ContainsFunc(cTags, func(e string) bool { return strings.HasPrefix(e, "_status::") }) {
n.AddTag("_status::open")
}
// persist tags added above
if err := svc.Update(n); err != nil {
fmt.Fprintln(os.Stderr, "failed to update with tags:", err)
return
}
output.PrintNode(cmd.OutOrStdout(), svc, n, jsonFlag)
},
}
func init() {
rootCmd.AddCommand(addCmd)
addPropertyFlags(addCmd)
addCmd.Flags().Set("type", "issue")
f := addCmd.Flags()
f.StringVar(&cDue, "due", "", "")
f.StringVar(&cContent, "content", "", "")
f.StringArrayVar(&cTags, "tag", nil, "")
f.StringArrayVar(&cRels, "rel", nil, "")
f.StringVar(&cType, "type", "issue", "node type (issue, note, …)")
f.StringVar(&cStatus, "status", "", "initial status (open, done)")
f.StringVar(&cPrio, "prio", "", "priority (high, medium, low)")
f.StringVar(&cNamespace, "namespace", "", "namespace name or ID")
f.StringVar(&cAssignee, "assignee", "", "assignee username or ID")
f.StringVar(&cDue, "due", "", "due date")
f.StringVar(&cContent, "content", "", "node body")
f.StringArrayVar(&cTags, "tag", nil, "custom tags")
f.StringArrayVar(&cRels, "rel", nil, "additional relations (type:target)")
}