update.go

  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 := ui.Spin("Updating task…", func() (*lunatask.Task, error) {
 83		return builder.Update(cmd.Context())
 84	})
 85	if err != nil {
 86		return err
 87	}
 88
 89	fmt.Fprintln(cmd.OutOrStdout(), ui.Success.Render("Updated task: "+task.ID))
 90
 91	return nil
 92}
 93
 94func applyUpdateName(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) error {
 95	name, _ := cmd.Flags().GetString("name")
 96	if name == "" {
 97		return nil
 98	}
 99
100	resolved, err := resolveName(name)
101	if err != nil {
102		return err
103	}
104
105	builder.Name(resolved)
106
107	return nil
108}
109
110func applyUpdateAreaAndGoal(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) error {
111	areaKey, _ := cmd.Flags().GetString("area")
112	goalKey, _ := cmd.Flags().GetString("goal")
113
114	if areaKey == "" && goalKey == "" {
115		return nil
116	}
117
118	if areaKey == "" && goalKey != "" {
119		fmt.Fprintln(cmd.ErrOrStderr(), ui.Warning.Render("Goal specified without area; ignoring"))
120
121		return nil
122	}
123
124	cfg, err := config.Load()
125	if err != nil {
126		return err
127	}
128
129	area := cfg.AreaByKey(areaKey)
130	if area == nil {
131		return fmt.Errorf("%w: %s", ErrUnknownArea, areaKey)
132	}
133
134	builder.InArea(area.ID)
135
136	if goalKey == "" {
137		return nil
138	}
139
140	goal := area.GoalByKey(goalKey)
141	if goal == nil {
142		return fmt.Errorf("%w: %s", ErrUnknownGoal, goalKey)
143	}
144
145	builder.InGoal(goal.ID)
146
147	return nil
148}
149
150func applyUpdateFlags(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) error {
151	if status, _ := cmd.Flags().GetString("status"); status != "" {
152		s, err := validate.TaskStatus(status)
153		if err != nil {
154			return err
155		}
156
157		builder.WithStatus(s)
158	}
159
160	if note, _ := cmd.Flags().GetString("note"); note != "" {
161		resolved, err := resolveNote(note)
162		if err != nil {
163			return err
164		}
165
166		builder.WithNote(resolved)
167	}
168
169	if priority, _ := cmd.Flags().GetString("priority"); priority != "" {
170		p, err := lunatask.ParsePriority(priority)
171		if err != nil {
172			return err
173		}
174
175		builder.Priority(p)
176	}
177
178	if estimate, _ := cmd.Flags().GetInt("estimate"); estimate != 0 {
179		builder.WithEstimate(estimate)
180	}
181
182	if motivation, _ := cmd.Flags().GetString("motivation"); motivation != "" {
183		m, err := validate.Motivation(motivation)
184		if err != nil {
185			return err
186		}
187
188		builder.WithMotivation(m)
189	}
190
191	applyUpdateEisenhower(cmd, builder)
192
193	return applyUpdateSchedule(cmd, builder)
194}
195
196func applyUpdateEisenhower(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) {
197	if important, _ := cmd.Flags().GetBool("important"); important {
198		builder.Important()
199	} else if notImportant, _ := cmd.Flags().GetBool("not-important"); notImportant {
200		builder.NotImportant()
201	}
202
203	if urgent, _ := cmd.Flags().GetBool("urgent"); urgent {
204		builder.Urgent()
205	} else if notUrgent, _ := cmd.Flags().GetBool("not-urgent"); notUrgent {
206		builder.NotUrgent()
207	}
208}
209
210func applyUpdateSchedule(cmd *cobra.Command, builder *lunatask.TaskUpdateBuilder) error {
211	schedule, _ := cmd.Flags().GetString("schedule")
212	if schedule == "" {
213		return nil
214	}
215
216	date, err := dateutil.Parse(schedule)
217	if err != nil {
218		return err
219	}
220
221	builder.ScheduledOn(date)
222
223	return nil
224}