Files
ax/cmd/create.go

73 lines
1.8 KiB
Go
Raw Normal View History

2026-03-26 12:48:47 +00:00
package cmd
import (
"axolotl/db"
"axolotl/models"
"axolotl/output"
2026-03-29 18:58:34 +02:00
"axolotl/service"
2026-03-26 12:48:47 +00:00
"fmt"
"os"
"slices"
"strings"
2026-03-26 12:48:47 +00:00
"github.com/spf13/cobra"
)
var cDue, cContent, cDummy string
var cTags, cRels []string
2026-03-26 12:48:47 +00:00
var createCmd = &cobra.Command{
Use: "create <title>", Short: "Create a new node", Args: cobra.ExactArgs(1),
2026-03-26 12:48:47 +00:00
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")
}
if slices.Contains(cTags, "_type::issue") && !slices.ContainsFunc(cTags, func(e string) bool { return strings.HasPrefix(e, "_status::") }) {
cTags = append(cTags, "_status::open")
2026-03-26 12:48:47 +00:00
}
2026-03-26 12:48:47 +00:00
rels := make(map[models.RelType][]string)
2026-03-29 18:58:34 +02:00
relNamespace := false
for _, r := range cRels {
2026-03-29 18:58:34 +02:00
rt, tgt, err := parseRelFlag(r)
2026-03-26 12:48:47 +00:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
if rt == models.RelInNamespace {
relNamespace = true
}
rels[rt] = append(rels[rt], tgt)
2026-03-26 12:48:47 +00:00
}
if !relNamespace {
2026-03-29 18:58:34 +02:00
rels[models.RelInNamespace] = append(rels[models.RelInNamespace], cfg.GetUser())
}
2026-03-29 18:58:34 +02:00
svc := service.NewSQLiteNodeService(d.DB, cfg.GetUser())
if n, err := svc.Create(args[0], cContent, cDue, cTags, rels); err != nil {
2026-03-26 12:48:47 +00:00
fmt.Fprintln(os.Stderr, "failed to create:", err)
} else {
output.PrintNode(cmd.OutOrStdout(), n, jsonFlag)
2026-03-26 12:48:47 +00:00
}
},
}
func init() {
rootCmd.AddCommand(createCmd)
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, "")
2026-03-26 12:48:47 +00:00
}