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