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/go-lunatask"
11 "git.secluded.site/lune/internal/client"
12 "git.secluded.site/lune/internal/completion"
13 "git.secluded.site/lune/internal/config"
14 "git.secluded.site/lune/internal/dateutil"
15 "git.secluded.site/lune/internal/ui"
16 "git.secluded.site/lune/internal/validate"
17 "github.com/spf13/cobra"
18)
19
20// UpdateCmd updates a task. Exported for potential use by shortcuts.
21var UpdateCmd = &cobra.Command{
22 Use: "update ID",
23 Short: "Update a task",
24 Long: `Update an existing task in Lunatask.
25
26Accepts a UUID or lunatask:// deep link.
27Only specified flags are modified; other fields remain unchanged.`,
28 Args: cobra.ExactArgs(1),
29 RunE: runUpdate,
30}
31
32func init() {
33 UpdateCmd.Flags().String("name", "", "New task name (use - for stdin)")
34 UpdateCmd.Flags().StringP("area", "a", "", "Move to area key")
35 UpdateCmd.Flags().StringP("goal", "g", "", "Move to goal key")
36 UpdateCmd.Flags().StringP("status", "s", "", "Status: later, next, in-progress, waiting, completed")
37 UpdateCmd.Flags().StringP("note", "n", "", "Task note (use - for stdin)")
38 UpdateCmd.Flags().StringP("priority", "p", "", "Priority: lowest, low, normal, high, highest")
39 UpdateCmd.Flags().IntP("estimate", "e", 0, "Estimate in minutes (0-720)")
40 UpdateCmd.Flags().StringP("motivation", "m", "", "Motivation: must, should, want")
41 UpdateCmd.Flags().Bool("important", false, "Mark as important (Eisenhower matrix)")
42 UpdateCmd.Flags().Bool("not-important", false, "Mark as not important")
43 UpdateCmd.Flags().Bool("urgent", false, "Mark as urgent (Eisenhower matrix)")
44 UpdateCmd.Flags().Bool("not-urgent", false, "Mark as not urgent")
45 UpdateCmd.Flags().String("schedule", "", "Schedule date (natural language)")
46
47 _ = UpdateCmd.RegisterFlagCompletionFunc("area", completion.Areas)
48 _ = UpdateCmd.RegisterFlagCompletionFunc("goal", completion.Goals)
49 _ = UpdateCmd.RegisterFlagCompletionFunc("status", completion.TaskStatuses)
50 _ = UpdateCmd.RegisterFlagCompletionFunc("priority", completion.Priorities)
51 _ = UpdateCmd.RegisterFlagCompletionFunc("motivation", completion.Motivations)
52}
53
54func runUpdate(cmd *cobra.Command, args []string) error {
55 id, err := validate.Reference(args[0])
56 if err != nil {
57 return err
58 }
59
60 apiClient, err := client.New()
61 if err != nil {
62 return err
63 }
64
65 builder := apiClient.NewTaskUpdate(id)
66
67 if err := applyUpdateName(cmd, builder); err != nil {
68 return err
69 }
70
71 area, err := applyUpdateAreaAndGoal(cmd, builder)
72 if err != nil {
73 return err
74 }
75
76 if err := applyUpdateFlags(cmd, builder, area); err != nil {
77 return err
78 }
79
80 task, err := ui.Spin("Updating task…", func() (*lunatask.Task, error) {
81 return builder.Update(cmd.Context())
82 })
83 if err != nil {
84 return err
85 }
86
87 fmt.Fprintln(cmd.OutOrStdout(), ui.Success.Render("Updated task: "+task.ID))
88
89 return nil
90}
91
92func applyUpdateName(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) error {
93 name, _ := cmd.Flags().GetString("name")
94 if name == "" {
95 return nil
96 }
97
98 resolved, err := resolveName(name)
99 if err != nil {
100 return err
101 }
102
103 builder.Name(resolved)
104
105 return nil
106}
107
108//nolint:nilnil // nil area with no error is valid when area flag not provided
109func applyUpdateAreaAndGoal(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) (*config.Area, error) {
110 areaKey, _ := cmd.Flags().GetString("area")
111 goalKey, _ := cmd.Flags().GetString("goal")
112
113 if areaKey == "" && goalKey == "" {
114 return nil, nil
115 }
116
117 if areaKey == "" && goalKey != "" {
118 fmt.Fprintln(cmd.ErrOrStderr(), ui.Warning.Render("Goal specified without area; ignoring"))
119
120 return nil, nil
121 }
122
123 cfg, err := config.Load()
124 if err != nil {
125 return nil, err
126 }
127
128 area := cfg.AreaByKey(areaKey)
129 if area == nil {
130 return nil, fmt.Errorf("%w: %s", ErrUnknownArea, areaKey)
131 }
132
133 builder.InArea(area.ID)
134
135 if goalKey == "" {
136 return area, nil
137 }
138
139 goal := area.GoalByKey(goalKey)
140 if goal == nil {
141 return nil, fmt.Errorf("%w: %s", ErrUnknownGoal, goalKey)
142 }
143
144 builder.InGoal(goal.ID)
145
146 return area, nil
147}
148
149//nolint:cyclop // flag handling inherently has many branches
150func applyUpdateFlags(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder, area *config.Area) error {
151 if status, _ := cmd.Flags().GetString("status"); status != "" {
152 var (
153 parsedStatus lunatask.TaskStatus
154 err error
155 )
156
157 if area != nil {
158 parsedStatus, err = validate.TaskStatusForWorkflow(status, area.Workflow)
159 } else {
160 parsedStatus, err = validate.TaskStatus(status)
161 }
162
163 if err != nil {
164 return err
165 }
166
167 builder.WithStatus(parsedStatus)
168 }
169
170 if note, _ := cmd.Flags().GetString("note"); note != "" {
171 resolved, err := resolveNote(note)
172 if err != nil {
173 return err
174 }
175
176 builder.WithNote(resolved)
177 }
178
179 if priority, _ := cmd.Flags().GetString("priority"); priority != "" {
180 p, err := lunatask.ParsePriority(priority)
181 if err != nil {
182 return err
183 }
184
185 builder.Priority(p)
186 }
187
188 if estimate, _ := cmd.Flags().GetInt("estimate"); estimate != 0 {
189 builder.WithEstimate(estimate)
190 }
191
192 if motivation, _ := cmd.Flags().GetString("motivation"); motivation != "" {
193 m, err := validate.Motivation(motivation)
194 if err != nil {
195 return err
196 }
197
198 builder.WithMotivation(m)
199 }
200
201 applyUpdateEisenhower(cmd, builder)
202
203 return applyUpdateSchedule(cmd, builder)
204}
205
206func applyUpdateEisenhower(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) {
207 if important, _ := cmd.Flags().GetBool("important"); important {
208 builder.Important()
209 } else if notImportant, _ := cmd.Flags().GetBool("not-important"); notImportant {
210 builder.NotImportant()
211 }
212
213 if urgent, _ := cmd.Flags().GetBool("urgent"); urgent {
214 builder.Urgent()
215 } else if notUrgent, _ := cmd.Flags().GetBool("not-urgent"); notUrgent {
216 builder.NotUrgent()
217 }
218}
219
220func applyUpdateSchedule(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) error {
221 schedule, _ := cmd.Flags().GetString("schedule")
222 if schedule == "" {
223 return nil
224 }
225
226 date, err := dateutil.Parse(schedule)
227 if err != nil {
228 return err
229 }
230
231 builder.ScheduledOn(date)
232
233 return nil
234}