show.go

  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/lune/internal/client"
 13	"git.secluded.site/lune/internal/completion"
 14	"git.secluded.site/lune/internal/config"
 15	"git.secluded.site/lune/internal/deeplink"
 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	RunE: runShow,
 34}
 35
 36func init() {
 37	ShowCmd.Flags().StringP("area", "a", "", "Area key (required if goal key is ambiguous)")
 38	_ = ShowCmd.RegisterFlagCompletionFunc("area", completion.Areas)
 39}
 40
 41func runShow(cmd *cobra.Command, args []string) error {
 42	goalKey := args[0]
 43	areaKey, _ := cmd.Flags().GetString("area")
 44
 45	cfg, err := config.Load()
 46	if err != nil {
 47		return err
 48	}
 49
 50	match, err := resolveGoal(cfg, goalKey, areaKey)
 51	if err != nil {
 52		return err
 53	}
 54
 55	apiClient, err := client.New()
 56	if err != nil {
 57		return err
 58	}
 59
 60	counter := stats.NewTaskCounter(apiClient)
 61
 62	return printGoalDetails(cmd, match.Goal, match.Area, counter)
 63}
 64
 65func resolveGoal(cfg *config.Config, goalKey, areaKey string) (*config.GoalMatch, error) {
 66	if areaKey != "" {
 67		area := cfg.AreaByKey(areaKey)
 68		if area == nil {
 69			return nil, fmt.Errorf("%w: %s", ErrUnknownArea, areaKey)
 70		}
 71
 72		goal := area.GoalByKey(goalKey)
 73		if goal == nil {
 74			return nil, fmt.Errorf("%w: %s", ErrUnknownGoal, goalKey)
 75		}
 76
 77		return &config.GoalMatch{Goal: goal, Area: area}, nil
 78	}
 79
 80	matches := cfg.FindGoalsByKey(goalKey)
 81
 82	switch len(matches) {
 83	case 0:
 84		return nil, fmt.Errorf("%w: %s", ErrUnknownGoal, goalKey)
 85
 86	case 1:
 87		return &matches[0], nil
 88
 89	default:
 90		areas := make([]string, len(matches))
 91		for i, m := range matches {
 92			areas[i] = m.Area.Key
 93		}
 94
 95		return nil, fmt.Errorf(
 96			"%w: %s exists in %s; use --area to specify",
 97			ErrAmbiguousGoal, goalKey, strings.Join(areas, ", "),
 98		)
 99	}
100}
101
102func printGoalDetails(cmd *cobra.Command, goal *config.Goal, area *config.Area, counter *stats.TaskCounter) error {
103	link, _ := deeplink.Build(deeplink.Goal, goal.ID)
104
105	fmt.Fprintf(cmd.OutOrStdout(), "%s (%s)\n", ui.H1.Render(goal.Name), goal.Key)
106	fmt.Fprintf(cmd.OutOrStdout(), "  ID:   %s\n", goal.ID)
107	fmt.Fprintf(cmd.OutOrStdout(), "  Area: %s (%s)\n", area.Name, area.Key)
108	fmt.Fprintf(cmd.OutOrStdout(), "  Link: %s\n", link)
109
110	taskCount, err := counter.UncompletedInGoal(cmd.Context(), goal.ID)
111	if err != nil {
112		fmt.Fprintf(cmd.OutOrStdout(), "  Tasks: %s\n", ui.Warning.Render("unable to fetch"))
113	} else {
114		fmt.Fprintf(cmd.OutOrStdout(), "  Tasks: %d uncompleted\n", taskCount)
115	}
116
117	return nil
118}