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 builder.WithStatus(lunatask.TaskStatus(status))
151 }
152
153 if note, _ := cmd.Flags().GetString("note"); note != "" {
154 resolved, err := resolveNote(note)
155 if err != nil {
156 return err
157 }
158
159 builder.WithNote(resolved)
160 }
161
162 if priority, _ := cmd.Flags().GetString("priority"); priority != "" {
163 p, err := lunatask.ParsePriority(priority)
164 if err != nil {
165 return err
166 }
167
168 builder.Priority(p)
169 }
170
171 if estimate, _ := cmd.Flags().GetInt("estimate"); estimate != 0 {
172 builder.WithEstimate(estimate)
173 }
174
175 if motivation, _ := cmd.Flags().GetString("motivation"); motivation != "" {
176 builder.WithMotivation(lunatask.Motivation(motivation))
177 }
178
179 applyUpdateEisenhower(cmd, builder)
180
181 return applyUpdateSchedule(cmd, builder)
182}
183
184func applyUpdateEisenhower(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) {
185 if important, _ := cmd.Flags().GetBool("important"); important {
186 builder.Important()
187 } else if notImportant, _ := cmd.Flags().GetBool("not-important"); notImportant {
188 builder.NotImportant()
189 }
190
191 if urgent, _ := cmd.Flags().GetBool("urgent"); urgent {
192 builder.Urgent()
193 } else if notUrgent, _ := cmd.Flags().GetBool("not-urgent"); notUrgent {
194 builder.NotUrgent()
195 }
196}
197
198func applyUpdateSchedule(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) error {
199 schedule, _ := cmd.Flags().GetString("schedule")
200 if schedule == "" {
201 return nil
202 }
203
204 date, err := dateutil.Parse(schedule)
205 if err != nil {
206 return err
207 }
208
209 builder.ScheduledOn(date)
210
211 return nil
212}