refactor: replace explicit fields with tag-based property system

This commit is contained in:
2026-03-27 02:11:46 +01:00
parent 2d4cff717b
commit b2225cff7b
17 changed files with 485 additions and 825 deletions

View File

@@ -6,63 +6,61 @@ import (
"axolotl/output"
"fmt"
"os"
"slices"
"strings"
"github.com/spf13/cobra"
)
var createType, createStatus, createPrio, createNamespace string = "issue", "open", "", ""
var createDue, createContent string
var createTags, createRels []string
var cDue, cContent, cDummy string
var cTags, cRels []string
var createCmd = &cobra.Command{
Use: "create <title>",
Short: "Create a new node",
Args: cobra.ExactArgs(1),
Use: "create <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 createType == "issue" && createStatus == "" {
createStatus = "open"
if !slices.ContainsFunc(cTags, func(e string) bool { return strings.HasPrefix(e, "_type::") }) {
cTags = append(cTags, "_type::issue")
}
if slices.Contains(cTags, "_type::issue") && !slices.ContainsFunc(cTags, func(e string) bool { return strings.HasPrefix(e, "_status::") }) {
cTags = append(cTags, "_status::open")
}
if !slices.ContainsFunc(cTags, func(e string) bool { return strings.HasPrefix(e, "_namespace::") }) {
cTags = append(cTags, "_namespace::" + db.GetCurrentUser())
}
rels := make(map[models.RelType][]string)
for _, r := range createRels {
relType, target, err := db.ParseRelFlag(r)
for _, r := range cRels {
rt, tgt, err := db.ParseRelFlag(r)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
rels[relType] = append(rels[relType], target)
rels[rt] = append(rels[rt], tgt)
}
n, err := d.CreateNode(db.CreateParams{
Title: args[0],
Content: createContent,
DueDate: createDue,
Type: createType,
Status: createStatus,
Priority: createPrio,
Namespace: createNamespace,
Tags: createTags,
Rels: rels,
})
if err != nil {
if n, err := d.CreateNode(db.CreateParams{Title: args[0], Content: cContent, DueDate: cDue, Tags: cTags, Rels: rels}); err != nil {
fmt.Fprintln(os.Stderr, "failed to create:", err)
return
} else {
output.PrintNode(cmd.OutOrStdout(), n, jsonFlag)
}
output.PrintNode(cmd.OutOrStdout(), n, jsonFlag)
},
}
func init() {
rootCmd.AddCommand(createCmd)
createCmd.Flags().StringVar(&createType, "type", "issue", "node type (issue, note, user, namespace)")
createCmd.Flags().StringVar(&createStatus, "status", "", "status (open, done)")
createCmd.Flags().StringVar(&createPrio, "prio", "", "priority (high, medium, low)")
createCmd.Flags().StringVar(&createNamespace, "namespace", "", "namespace")
createCmd.Flags().StringVar(&createDue, "due", "", "due date")
createCmd.Flags().StringVar(&createContent, "content", "", "content")
createCmd.Flags().StringArrayVar(&createTags, "tag", nil, "tags (repeatable)")
createCmd.Flags().StringArrayVar(&createRels, "rel", nil, "relations (type:id, repeatable)")
f := createCmd.Flags()
f.StringVar(&cDummy, "type", "issue", "")
f.StringVar(&cDummy, "status", "", "")
f.StringVar(&cDummy, "prio", "", "")
f.StringVar(&cDummy, "namespace", "", "")
f.StringVar(&cDue, "due", "", "")
f.StringVar(&cContent, "content", "", "")
f.StringArrayVar(&cTags, "tag", nil, "")
f.StringArrayVar(&cRels, "rel", nil, "")
}