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 := ui.Spin("Creating task…", func() (*lunatask.Task, error) {
88 return builder.Create(cmd.Context())
89 })
90 if err != nil {
91 return err
92 }
93
94 fmt.Fprintln(cmd.OutOrStdout(), ui.Success.Render("Created task: "+task.ID))
95
96 return nil
97}
98
99func resolveName(arg string) (string, error) {
100 if arg != "-" {
101 return arg, nil
102 }
103
104 scanner := bufio.NewScanner(os.Stdin)
105 if scanner.Scan() {
106 return strings.TrimSpace(scanner.Text()), nil
107 }
108
109 if err := scanner.Err(); err != nil {
110 return "", fmt.Errorf("reading stdin: %w", err)
111 }
112
113 return "", ErrNoInput
114}
115
116func applyAreaAndGoal(cmd *cobra.Command, builder *lunatask.TaskBuilder) error {
117 areaKey, _ := cmd.Flags().GetString("area")
118 goalKey, _ := cmd.Flags().GetString("goal")
119
120 if areaKey == "" && goalKey == "" {
121 return nil
122 }
123
124 if areaKey == "" && goalKey != "" {
125 fmt.Fprintln(cmd.ErrOrStderr(), ui.Warning.Render("Goal specified without area; ignoring"))
126
127 return nil
128 }
129
130 cfg, err := config.Load()
131 if err != nil {
132 return err
133 }
134
135 area := cfg.AreaByKey(areaKey)
136 if area == nil {
137 return fmt.Errorf("%w: %s", ErrUnknownArea, areaKey)
138 }
139
140 builder.InArea(area.ID)
141
142 if goalKey == "" {
143 return nil
144 }
145
146 goal := area.GoalByKey(goalKey)
147 if goal == nil {
148 return fmt.Errorf("%w: %s", ErrUnknownGoal, goalKey)
149 }
150
151 builder.InGoal(goal.ID)
152
153 return nil
154}
155
156func applyOptionalFlags(cmd *cobra.Command, builder *lunatask.TaskBuilder) error {
157 if status, _ := cmd.Flags().GetString("status"); status != "" {
158 s, err := validate.TaskStatus(status)
159 if err != nil {
160 return err
161 }
162
163 builder.WithStatus(s)
164 }
165
166 if note, _ := cmd.Flags().GetString("note"); note != "" {
167 resolved, err := resolveNote(note)
168 if err != nil {
169 return err
170 }
171
172 builder.WithNote(resolved)
173 }
174
175 if priority, _ := cmd.Flags().GetString("priority"); priority != "" {
176 p, err := lunatask.ParsePriority(priority)
177 if err != nil {
178 return err
179 }
180
181 builder.Priority(p)
182 }
183
184 if estimate, _ := cmd.Flags().GetInt("estimate"); estimate != 0 {
185 builder.WithEstimate(estimate)
186 }
187
188 if motivation, _ := cmd.Flags().GetString("motivation"); motivation != "" {
189 m, err := validate.Motivation(motivation)
190 if err != nil {
191 return err
192 }
193
194 builder.WithMotivation(m)
195 }
196
197 applyEisenhower(cmd, builder)
198
199 return applySchedule(cmd, builder)
200}
201
202func applyEisenhower(cmd *cobra.Command, builder *lunatask.TaskBuilder) {
203 if important, _ := cmd.Flags().GetBool("important"); important {
204 builder.Important()
205 } else if notImportant, _ := cmd.Flags().GetBool("not-important"); notImportant {
206 builder.NotImportant()
207 }
208
209 if urgent, _ := cmd.Flags().GetBool("urgent"); urgent {
210 builder.Urgent()
211 } else if notUrgent, _ := cmd.Flags().GetBool("not-urgent"); notUrgent {
212 builder.NotUrgent()
213 }
214}
215
216func applySchedule(cmd *cobra.Command, builder *lunatask.TaskBuilder) error {
217 schedule, _ := cmd.Flags().GetString("schedule")
218 if schedule == "" {
219 return nil
220 }
221
222 date, err := dateutil.Parse(schedule)
223 if err != nil {
224 return err
225 }
226
227 builder.ScheduledOn(date)
228
229 return nil
230}
231
232func resolveNote(note string) (string, error) {
233 if note != "-" {
234 return note, nil
235 }
236
237 data, err := os.ReadFile("/dev/stdin")
238 if err != nil {
239 return "", fmt.Errorf("reading stdin: %w", err)
240 }
241
242 return strings.TrimSpace(string(data)), nil
243}