add.go

 1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5package task
 6
 7import (
 8	"fmt"
 9
10	"git.secluded.site/lune/internal/config"
11	"github.com/spf13/cobra"
12)
13
14// AddCmd creates a new task. Exported for use by the add shortcut.
15var AddCmd = &cobra.Command{
16	Use:   "add NAME",
17	Short: "Create a new task",
18	Long: `Create a new task in Lunatask.
19
20The task name is required. Use flags to set additional properties.
21Use "-" as NAME to read the task name from stdin.`,
22	Args: cobra.MinimumNArgs(1),
23	RunE: func(cmd *cobra.Command, args []string) error {
24		// TODO: implement task creation
25		name := args[0]
26		fmt.Fprintf(cmd.OutOrStdout(), "Creating task: %s (not yet implemented)\n", name)
27
28		return nil
29	},
30}
31
32func init() {
33	AddCmd.Flags().StringP("area", "a", "", "Area key (from config)")
34	AddCmd.Flags().StringP("goal", "g", "", "Goal key (from config)")
35	AddCmd.Flags().StringP("status", "s", "", "Status: later, next, started, waiting")
36	AddCmd.Flags().StringP("note", "n", "", "Task note (use - for stdin)")
37	AddCmd.Flags().IntP("priority", "p", 0, "Priority: -2 to 2")
38	AddCmd.Flags().IntP("estimate", "e", 0, "Estimate in minutes (0-720)")
39	AddCmd.Flags().StringP("motivation", "m", "", "Motivation: must, should, want")
40	AddCmd.Flags().Int("eisenhower", 0, "Eisenhower quadrant: 1-4")
41	AddCmd.Flags().String("schedule", "", "Schedule date (natural language)")
42
43	_ = AddCmd.RegisterFlagCompletionFunc("area", completeAreas)
44	_ = AddCmd.RegisterFlagCompletionFunc("goal", completeGoals)
45	_ = AddCmd.RegisterFlagCompletionFunc("status",
46		func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
47			return []string{"later", "next", "started", "waiting"}, cobra.ShellCompDirectiveNoFileComp
48		})
49	_ = AddCmd.RegisterFlagCompletionFunc("motivation",
50		func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
51			return []string{"must", "should", "want"}, cobra.ShellCompDirectiveNoFileComp
52		})
53	_ = AddCmd.RegisterFlagCompletionFunc("eisenhower",
54		func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
55			return []string{"1", "2", "3", "4"}, cobra.ShellCompDirectiveNoFileComp
56		})
57}
58
59// completeAreas returns area keys from config for shell completion.
60func completeAreas(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
61	cfg, err := config.Load()
62	if err != nil {
63		return nil, cobra.ShellCompDirectiveError
64	}
65
66	keys := make([]string, len(cfg.Areas))
67	for i, a := range cfg.Areas {
68		keys[i] = a.Key
69	}
70
71	return keys, cobra.ShellCompDirectiveNoFileComp
72}
73
74// completeGoals returns goal keys from config for shell completion.
75// Note: This returns all goals across all areas. A smarter completion
76// would filter based on the --area flag value if set.
77func completeGoals(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
78	cfg, err := config.Load()
79	if err != nil {
80		return nil, cobra.ShellCompDirectiveError
81	}
82
83	var keys []string
84
85	for _, a := range cfg.Areas {
86		for _, g := range a.Goals {
87			keys = append(keys, g.Key)
88		}
89	}
90
91	return keys, cobra.ShellCompDirectiveNoFileComp
92}