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().IntP("priority", "p", 0, "Priority: -2 to 2")
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("motivation",
60 completion.Static("must", "should", "want"))
61}
62
63func runAdd(cmd *cobra.Command, args []string) error {
64 name, err := resolveName(args[0])
65 if err != nil {
66 return err
67 }
68
69 apiClient, err := client.New()
70 if err != nil {
71 return err
72 }
73
74 builder := apiClient.NewTask(name)
75
76 if err := applyAreaAndGoal(cmd, builder); err != nil {
77 return err
78 }
79
80 if err := applyOptionalFlags(cmd, builder); err != nil {
81 return err
82 }
83
84 task, err := builder.Create(cmd.Context())
85 if err != nil {
86 return err
87 }
88
89 fmt.Fprintln(cmd.OutOrStdout(), ui.Success.Render("Created task: "+task.ID))
90
91 return nil
92}
93
94func resolveName(arg string) (string, error) {
95 if arg != "-" {
96 return arg, nil
97 }
98
99 scanner := bufio.NewScanner(os.Stdin)
100 if scanner.Scan() {
101 return strings.TrimSpace(scanner.Text()), nil
102 }
103
104 if err := scanner.Err(); err != nil {
105 return "", fmt.Errorf("reading stdin: %w", err)
106 }
107
108 return "", ErrNoInput
109}
110
111func applyAreaAndGoal(cmd *cobra.Command, builder *lunatask.TaskBuilder) error {
112 areaKey, _ := cmd.Flags().GetString("area")
113 goalKey, _ := cmd.Flags().GetString("goal")
114
115 if areaKey == "" && goalKey == "" {
116 return nil
117 }
118
119 if areaKey == "" && goalKey != "" {
120 fmt.Fprintln(cmd.ErrOrStderr(), ui.Warning.Render("Goal specified without area; ignoring"))
121
122 return nil
123 }
124
125 cfg, err := config.Load()
126 if err != nil {
127 return err
128 }
129
130 area := cfg.AreaByKey(areaKey)
131 if area == nil {
132 return fmt.Errorf("%w: %s", ErrUnknownArea, areaKey)
133 }
134
135 builder.InArea(area.ID)
136
137 if goalKey == "" {
138 return nil
139 }
140
141 goal := area.GoalByKey(goalKey)
142 if goal == nil {
143 return fmt.Errorf("%w: %s", ErrUnknownGoal, goalKey)
144 }
145
146 builder.InGoal(goal.ID)
147
148 return nil
149}
150
151func applyOptionalFlags(cmd *cobra.Command, builder *lunatask.TaskBuilder) error {
152 if status, _ := cmd.Flags().GetString("status"); status != "" {
153 builder.WithStatus(lunatask.TaskStatus(status))
154 }
155
156 if note, _ := cmd.Flags().GetString("note"); note != "" {
157 resolved, err := resolveNote(note)
158 if err != nil {
159 return err
160 }
161
162 builder.WithNote(resolved)
163 }
164
165 if priority, _ := cmd.Flags().GetInt("priority"); priority != 0 {
166 builder.Priority(lunatask.Priority(priority))
167 }
168
169 if estimate, _ := cmd.Flags().GetInt("estimate"); estimate != 0 {
170 builder.WithEstimate(estimate)
171 }
172
173 if motivation, _ := cmd.Flags().GetString("motivation"); motivation != "" {
174 builder.WithMotivation(lunatask.Motivation(motivation))
175 }
176
177 applyEisenhower(cmd, builder)
178
179 return applySchedule(cmd, builder)
180}
181
182func applyEisenhower(cmd *cobra.Command, builder *lunatask.TaskBuilder) {
183 if important, _ := cmd.Flags().GetBool("important"); important {
184 builder.Important()
185 } else if notImportant, _ := cmd.Flags().GetBool("not-important"); notImportant {
186 builder.NotImportant()
187 }
188
189 if urgent, _ := cmd.Flags().GetBool("urgent"); urgent {
190 builder.Urgent()
191 } else if notUrgent, _ := cmd.Flags().GetBool("not-urgent"); notUrgent {
192 builder.NotUrgent()
193 }
194}
195
196func applySchedule(cmd *cobra.Command, builder *lunatask.TaskBuilder) error {
197 schedule, _ := cmd.Flags().GetString("schedule")
198 if schedule == "" {
199 return nil
200 }
201
202 date, err := dateutil.Parse(schedule)
203 if err != nil {
204 return err
205 }
206
207 builder.ScheduledOn(date)
208
209 return nil
210}
211
212func resolveNote(note string) (string, error) {
213 if note != "-" {
214 return note, nil
215 }
216
217 data, err := os.ReadFile("/dev/stdin")
218 if err != nil {
219 return "", fmt.Errorf("reading stdin: %w", err)
220 }
221
222 return strings.TrimSpace(string(data)), nil
223}