1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5package task
6
7import (
8 "bufio"
9 "errors"
10 "fmt"
11 "os"
12 "strings"
13
14 "git.secluded.site/go-lunatask"
15 "git.secluded.site/lune/internal/client"
16 "git.secluded.site/lune/internal/completion"
17 "git.secluded.site/lune/internal/config"
18 "git.secluded.site/lune/internal/dateutil"
19 "git.secluded.site/lune/internal/ui"
20 "github.com/spf13/cobra"
21)
22
23// ErrUnknownGoal indicates the specified goal key was not found in config.
24var ErrUnknownGoal = errors.New("unknown goal key")
25
26// ErrNoInput indicates no input was provided on stdin.
27var ErrNoInput = errors.New("no input provided on stdin")
28
29// AddCmd creates a new task. Exported for use by the add shortcut.
30var AddCmd = &cobra.Command{
31 Use: "add NAME",
32 Short: "Create a new task",
33 Long: `Create a new task in Lunatask.
34
35The task name is required. Use flags to set additional properties.
36Use "-" as NAME to read the task name from stdin.`,
37 Args: cobra.MinimumNArgs(1),
38 RunE: runAdd,
39}
40
41func init() {
42 AddCmd.Flags().StringP("area", "a", "", "Area key (from config)")
43 AddCmd.Flags().StringP("goal", "g", "", "Goal key (from config)")
44 AddCmd.Flags().StringP("status", "s", "", "Status: later, next, started, waiting")
45 AddCmd.Flags().StringP("note", "n", "", "Task note (use - for stdin)")
46 AddCmd.Flags().StringP("priority", "p", "", "Priority: lowest, low, normal, high, highest")
47 AddCmd.Flags().IntP("estimate", "e", 0, "Estimate in minutes (0-720)")
48 AddCmd.Flags().StringP("motivation", "m", "", "Motivation: must, should, want")
49 AddCmd.Flags().Bool("important", false, "Mark as important (Eisenhower matrix)")
50 AddCmd.Flags().Bool("not-important", false, "Mark as not important")
51 AddCmd.Flags().Bool("urgent", false, "Mark as urgent (Eisenhower matrix)")
52 AddCmd.Flags().Bool("not-urgent", false, "Mark as not urgent")
53 AddCmd.Flags().String("schedule", "", "Schedule date (YYYY-MM-DD)")
54
55 _ = AddCmd.RegisterFlagCompletionFunc("area", completion.Areas)
56 _ = AddCmd.RegisterFlagCompletionFunc("goal", completion.Goals)
57 _ = AddCmd.RegisterFlagCompletionFunc("status",
58 completion.Static("later", "next", "started", "waiting"))
59 _ = AddCmd.RegisterFlagCompletionFunc("priority",
60 completion.Static("lowest", "low", "normal", "high", "highest"))
61 _ = AddCmd.RegisterFlagCompletionFunc("motivation",
62 completion.Static("must", "should", "want"))
63}
64
65func runAdd(cmd *cobra.Command, args []string) error {
66 name, err := resolveName(args[0])
67 if err != nil {
68 return err
69 }
70
71 apiClient, err := client.New()
72 if err != nil {
73 return err
74 }
75
76 builder := apiClient.NewTask(name)
77
78 if err := applyAreaAndGoal(cmd, builder); err != nil {
79 return err
80 }
81
82 if err := applyOptionalFlags(cmd, builder); err != nil {
83 return err
84 }
85
86 task, err := builder.Create(cmd.Context())
87 if err != nil {
88 return err
89 }
90
91 fmt.Fprintln(cmd.OutOrStdout(), ui.Success.Render("Created task: "+task.ID))
92
93 return nil
94}
95
96func resolveName(arg string) (string, error) {
97 if arg != "-" {
98 return arg, nil
99 }
100
101 scanner := bufio.NewScanner(os.Stdin)
102 if scanner.Scan() {
103 return strings.TrimSpace(scanner.Text()), nil
104 }
105
106 if err := scanner.Err(); err != nil {
107 return "", fmt.Errorf("reading stdin: %w", err)
108 }
109
110 return "", ErrNoInput
111}
112
113func applyAreaAndGoal(cmd *cobra.Command, builder *lunatask.TaskBuilder) error {
114 areaKey, _ := cmd.Flags().GetString("area")
115 goalKey, _ := cmd.Flags().GetString("goal")
116
117 if areaKey == "" && goalKey == "" {
118 return nil
119 }
120
121 if areaKey == "" && goalKey != "" {
122 fmt.Fprintln(cmd.ErrOrStderr(), ui.Warning.Render("Goal specified without area; ignoring"))
123
124 return nil
125 }
126
127 cfg, err := config.Load()
128 if err != nil {
129 return err
130 }
131
132 area := cfg.AreaByKey(areaKey)
133 if area == nil {
134 return fmt.Errorf("%w: %s", ErrUnknownArea, areaKey)
135 }
136
137 builder.InArea(area.ID)
138
139 if goalKey == "" {
140 return nil
141 }
142
143 goal := area.GoalByKey(goalKey)
144 if goal == nil {
145 return fmt.Errorf("%w: %s", ErrUnknownGoal, goalKey)
146 }
147
148 builder.InGoal(goal.ID)
149
150 return nil
151}
152
153func applyOptionalFlags(cmd *cobra.Command, builder *lunatask.TaskBuilder) error {
154 if status, _ := cmd.Flags().GetString("status"); status != "" {
155 builder.WithStatus(lunatask.TaskStatus(status))
156 }
157
158 if note, _ := cmd.Flags().GetString("note"); note != "" {
159 resolved, err := resolveNote(note)
160 if err != nil {
161 return err
162 }
163
164 builder.WithNote(resolved)
165 }
166
167 if priority, _ := cmd.Flags().GetString("priority"); priority != "" {
168 p, err := lunatask.ParsePriority(priority)
169 if err != nil {
170 return err
171 }
172
173 builder.Priority(p)
174 }
175
176 if estimate, _ := cmd.Flags().GetInt("estimate"); estimate != 0 {
177 builder.WithEstimate(estimate)
178 }
179
180 if motivation, _ := cmd.Flags().GetString("motivation"); motivation != "" {
181 builder.WithMotivation(lunatask.Motivation(motivation))
182 }
183
184 applyEisenhower(cmd, builder)
185
186 return applySchedule(cmd, builder)
187}
188
189func applyEisenhower(cmd *cobra.Command, builder *lunatask.TaskBuilder) {
190 if important, _ := cmd.Flags().GetBool("important"); important {
191 builder.Important()
192 } else if notImportant, _ := cmd.Flags().GetBool("not-important"); notImportant {
193 builder.NotImportant()
194 }
195
196 if urgent, _ := cmd.Flags().GetBool("urgent"); urgent {
197 builder.Urgent()
198 } else if notUrgent, _ := cmd.Flags().GetBool("not-urgent"); notUrgent {
199 builder.NotUrgent()
200 }
201}
202
203func applySchedule(cmd *cobra.Command, builder *lunatask.TaskBuilder) error {
204 schedule, _ := cmd.Flags().GetString("schedule")
205 if schedule == "" {
206 return nil
207 }
208
209 date, err := dateutil.Parse(schedule)
210 if err != nil {
211 return err
212 }
213
214 builder.ScheduledOn(date)
215
216 return nil
217}
218
219func resolveNote(note string) (string, error) {
220 if note != "-" {
221 return note, nil
222 }
223
224 data, err := os.ReadFile("/dev/stdin")
225 if err != nil {
226 return "", fmt.Errorf("reading stdin: %w", err)
227 }
228
229 return strings.TrimSpace(string(data)), nil
230}