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