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/lune/internal/client"
11 "git.secluded.site/lune/internal/ui"
12 "git.secluded.site/lune/internal/validate"
13 "github.com/spf13/cobra"
14)
15
16// DeleteCmd deletes a task. Exported for potential use by shortcuts.
17var DeleteCmd = &cobra.Command{
18 Use: "delete ID",
19 Short: "Delete a task",
20 Long: `Permanently delete a task from Lunatask.
21
22Accepts a UUID or lunatask:// deep link.
23Prompts for confirmation unless --force is specified.`,
24 Args: cobra.ExactArgs(1),
25 RunE: runDelete,
26}
27
28func init() {
29 DeleteCmd.Flags().BoolP("force", "f", false, "Skip confirmation prompt")
30}
31
32func runDelete(cmd *cobra.Command, args []string) error {
33 id, err := validate.Reference(args[0])
34 if err != nil {
35 return err
36 }
37
38 force, _ := cmd.Flags().GetBool("force")
39 if !force {
40 if !ui.Confirm(fmt.Sprintf("Delete task %s?", id)) {
41 fmt.Fprintln(cmd.OutOrStdout(), "Cancelled")
42
43 return nil
44 }
45 }
46
47 apiClient, err := client.New()
48 if err != nil {
49 return err
50 }
51
52 if err := ui.SpinVoid("Deleting task…", func() error {
53 _, err := apiClient.DeleteTask(cmd.Context(), id)
54
55 return err
56 }); err != nil {
57 return err
58 }
59
60 fmt.Fprintln(cmd.OutOrStdout(), ui.Success.Render("Task deleted"))
61
62 return nil
63}