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, started, 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",
50 completion.Static("later", "next", "started", "waiting", "completed"))
51 _ = UpdateCmd.RegisterFlagCompletionFunc("priority",
52 completion.Static("lowest", "low", "normal", "high", "highest"))
53 _ = UpdateCmd.RegisterFlagCompletionFunc("motivation",
54 completion.Static("must", "should", "want"))
55}
56
57func runUpdate(cmd *cobra.Command, args []string) error {
58 id, err := validate.Reference(args[0])
59 if err != nil {
60 return err
61 }
62
63 apiClient, err := client.New()
64 if err != nil {
65 return err
66 }
67
68 builder := apiClient.NewTaskUpdate(id)
69
70 if err := applyUpdateName(cmd, builder); err != nil {
71 return err
72 }
73
74 if err := applyUpdateAreaAndGoal(cmd, builder); err != nil {
75 return err
76 }
77
78 if err := applyUpdateFlags(cmd, builder); err != nil {
79 return err
80 }
81
82 task, err := builder.Update(cmd.Context())
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
108func applyUpdateAreaAndGoal(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) error {
109 areaKey, _ := cmd.Flags().GetString("area")
110 goalKey, _ := cmd.Flags().GetString("goal")
111
112 if areaKey == "" && goalKey == "" {
113 return nil
114 }
115
116 if areaKey == "" && goalKey != "" {
117 fmt.Fprintln(cmd.ErrOrStderr(), ui.Warning.Render("Goal specified without area; ignoring"))
118
119 return nil
120 }
121
122 cfg, err := config.Load()
123 if err != nil {
124 return err
125 }
126
127 area := cfg.AreaByKey(areaKey)
128 if area == nil {
129 return fmt.Errorf("%w: %s", ErrUnknownArea, areaKey)
130 }
131
132 builder.InArea(area.ID)
133
134 if goalKey == "" {
135 return nil
136 }
137
138 goal := area.GoalByKey(goalKey)
139 if goal == nil {
140 return fmt.Errorf("%w: %s", ErrUnknownGoal, goalKey)
141 }
142
143 builder.InGoal(goal.ID)
144
145 return nil
146}
147
148func applyUpdateFlags(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) error {
149 if status, _ := cmd.Flags().GetString("status"); status != "" {
150 s, err := validate.TaskStatus(status)
151 if err != nil {
152 return err
153 }
154
155 builder.WithStatus(s)
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 m, err := validate.Motivation(motivation)
182 if err != nil {
183 return err
184 }
185
186 builder.WithMotivation(m)
187 }
188
189 applyUpdateEisenhower(cmd, builder)
190
191 return applyUpdateSchedule(cmd, builder)
192}
193
194func applyUpdateEisenhower(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) {
195 if important, _ := cmd.Flags().GetBool("important"); important {
196 builder.Important()
197 } else if notImportant, _ := cmd.Flags().GetBool("not-important"); notImportant {
198 builder.NotImportant()
199 }
200
201 if urgent, _ := cmd.Flags().GetBool("urgent"); urgent {
202 builder.Urgent()
203 } else if notUrgent, _ := cmd.Flags().GetBool("not-urgent"); notUrgent {
204 builder.NotUrgent()
205 }
206}
207
208func applyUpdateSchedule(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) error {
209 schedule, _ := cmd.Flags().GetString("schedule")
210 if schedule == "" {
211 return nil
212 }
213
214 date, err := dateutil.Parse(schedule)
215 if err != nil {
216 return err
217 }
218
219 builder.ScheduledOn(date)
220
221 return nil
222}