1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5package goal
6
7import (
8 "errors"
9 "fmt"
10 "strings"
11
12 "git.secluded.site/go-lunatask"
13 "git.secluded.site/lune/internal/client"
14 "git.secluded.site/lune/internal/completion"
15 "git.secluded.site/lune/internal/config"
16 "git.secluded.site/lune/internal/stats"
17 "git.secluded.site/lune/internal/ui"
18 "github.com/spf13/cobra"
19)
20
21// ErrAmbiguousGoal indicates the goal key exists in multiple areas.
22var ErrAmbiguousGoal = errors.New("ambiguous goal key")
23
24// ShowCmd displays details for a specific goal.
25var ShowCmd = &cobra.Command{
26 Use: "show KEY",
27 Short: "Show goal details",
28 Long: `Show detailed information for a configured goal.
29
30If the goal key exists in multiple areas, use --area to disambiguate.
31Displays the goal's name, ID, deeplink, and uncompleted task count.`,
32 Args: cobra.ExactArgs(1),
33 ValidArgsFunction: completion.Goals,
34 RunE: runShow,
35}
36
37func init() {
38 ShowCmd.Flags().StringP("area", "a", "", "Area key (required if goal key is ambiguous)")
39 _ = ShowCmd.RegisterFlagCompletionFunc("area", completion.Areas)
40}
41
42func runShow(cmd *cobra.Command, args []string) error {
43 goalKey := args[0]
44 areaKey, _ := cmd.Flags().GetString("area")
45
46 cfg, err := config.Load()
47 if err != nil {
48 return err
49 }
50
51 match, err := resolveGoal(cfg, goalKey, areaKey)
52 if err != nil {
53 return err
54 }
55
56 apiClient, err := client.New()
57 if err != nil {
58 return err
59 }
60
61 counter := stats.NewTaskCounter(apiClient)
62
63 return printGoalDetails(cmd, match.Goal, match.Area, counter)
64}
65
66func resolveGoal(cfg *config.Config, goalKey, areaKey string) (*config.GoalMatch, error) {
67 if areaKey != "" {
68 area := cfg.AreaByKey(areaKey)
69 if area == nil {
70 return nil, fmt.Errorf("%w: %s", ErrUnknownArea, areaKey)
71 }
72
73 goal := area.GoalByKey(goalKey)
74 if goal == nil {
75 return nil, fmt.Errorf("%w: %s", ErrUnknownGoal, goalKey)
76 }
77
78 return &config.GoalMatch{Goal: goal, Area: area}, nil
79 }
80
81 matches := cfg.FindGoalsByKey(goalKey)
82
83 switch len(matches) {
84 case 0:
85 return nil, fmt.Errorf("%w: %s", ErrUnknownGoal, goalKey)
86
87 case 1:
88 return &matches[0], nil
89
90 default:
91 areas := make([]string, len(matches))
92 for i, m := range matches {
93 areas[i] = m.Area.Key
94 }
95
96 return nil, fmt.Errorf(
97 "%w: %s exists in %s; use --area to specify",
98 ErrAmbiguousGoal, goalKey, strings.Join(areas, ", "),
99 )
100 }
101}
102
103func printGoalDetails(cmd *cobra.Command, goal *config.Goal, area *config.Area, counter *stats.TaskCounter) error {
104 link, _ := lunatask.BuildDeepLink(lunatask.ResourceGoal, goal.ID)
105
106 fmt.Fprintf(cmd.OutOrStdout(), "%s (%s)\n", ui.H1.Render(goal.Name), goal.Key)
107 fmt.Fprintf(cmd.OutOrStdout(), " ID: %s\n", goal.ID)
108 fmt.Fprintf(cmd.OutOrStdout(), " Area: %s (%s)\n", area.Name, area.Key)
109 fmt.Fprintf(cmd.OutOrStdout(), " Link: %s\n", link)
110
111 taskCount, err := counter.UncompletedInGoal(cmd.Context(), goal.ID)
112 if err != nil {
113 fmt.Fprintf(cmd.OutOrStdout(), " Tasks: %s\n", ui.Warning.Render("unable to fetch"))
114 } else {
115 fmt.Fprintf(cmd.OutOrStdout(), " Tasks: %d uncompleted\n", taskCount)
116 }
117
118 return nil
119}