Files
ax/src/cmd/add.go

85 lines
2.4 KiB
Go
Raw Normal View History

2026-03-26 12:48:47 +00:00
package cmd
import (
"axolotl/models"
2026-03-26 12:48:47 +00:00
"axolotl/output"
2026-03-29 18:58:34 +02:00
"axolotl/service"
2026-03-26 12:48:47 +00:00
"fmt"
"os"
"github.com/spf13/cobra"
)
var cDue, cContent, cType, cStatus, cPrio, cNamespace, cAssignee string
var cTags, cRels []string
2026-03-26 12:48:47 +00:00
var addCmd = &cobra.Command{
Use: "add <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) {
svc, err := service.GetNodeService(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
input := service.AddInput{
Title: args[0],
Content: cContent,
DueDate: cDue,
}
// --tag is an alias for --rel with no target.
for _, tag := range cTags {
input.Rels = append(input.Rels, service.RelInput{Type: models.RelType(tag), Target: ""})
}
// Shorthand flags expand to property rels or edge rels.
if cType != "" {
input.Rels = append(input.Rels, service.RelInput{Type: models.RelType("_type::" + cType), Target: ""})
}
if cStatus != "" {
input.Rels = append(input.Rels, service.RelInput{Type: models.RelType("_status::" + cStatus), Target: ""})
}
if cPrio != "" {
input.Rels = append(input.Rels, service.RelInput{Type: models.RelType("_prio::" + cPrio), Target: ""})
}
if cNamespace != "" {
input.Rels = append(input.Rels, service.RelInput{Type: models.RelInNamespace, Target: cNamespace})
}
if cAssignee != "" {
input.Rels = append(input.Rels, service.RelInput{Type: models.RelAssignee, Target: cAssignee})
2026-03-26 12:48:47 +00:00
}
for _, r := range cRels {
ri, err := parseRelInput(r)
2026-03-26 12:48:47 +00:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
input.Rels = append(input.Rels, ri)
}
n, err := svc.Add(input)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to create:", err)
return
2026-03-26 12:48:47 +00:00
}
output.PrintNode(cmd.OutOrStdout(), svc, n, jsonFlag)
2026-03-26 12:48:47 +00:00
},
}
func init() {
rootCmd.AddCommand(addCmd)
f := addCmd.Flags()
f.StringVar(&cType, "type", "", "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, "label tag (alias for --rel tagname)")
f.StringArrayVar(&cRels, "rel", nil, "relation (prefix::value or relname:target)")
2026-03-26 12:48:47 +00:00
}