show.go

 1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5package person
 6
 7import (
 8	"encoding/json"
 9	"fmt"
10
11	"git.secluded.site/go-lunatask"
12	"git.secluded.site/lune/internal/client"
13	"git.secluded.site/lune/internal/ui"
14	"git.secluded.site/lune/internal/validate"
15	"github.com/spf13/cobra"
16)
17
18// ShowCmd displays a person by ID. Exported for potential use by shortcuts.
19var ShowCmd = &cobra.Command{
20	Use:   "show ID",
21	Short: "Show person details",
22	Long: `Show detailed information for a person.
23
24Accepts a UUID or lunatask:// deep link.
25
26Note: Due to end-to-end encryption, names are not available
27through the API. Only metadata is shown.`,
28	Args: cobra.ExactArgs(1),
29	RunE: runShow,
30}
31
32func init() {
33	ShowCmd.Flags().Bool("json", false, "Output as JSON")
34}
35
36func runShow(cmd *cobra.Command, args []string) error {
37	id, err := validate.Reference(args[0])
38	if err != nil {
39		return err
40	}
41
42	apiClient, err := client.New()
43	if err != nil {
44		return err
45	}
46
47	person, err := ui.Spin("Fetching person…", func() (*lunatask.Person, error) {
48		return apiClient.GetPerson(cmd.Context(), id)
49	})
50	if err != nil {
51		return err
52	}
53
54	if mustGetBoolFlag(cmd, "json") {
55		return outputPersonJSON(cmd, person)
56	}
57
58	return printPersonDetails(cmd, person)
59}
60
61func outputPersonJSON(cmd *cobra.Command, person *lunatask.Person) error {
62	enc := json.NewEncoder(cmd.OutOrStdout())
63	enc.SetIndent("", "  ")
64
65	if err := enc.Encode(person); err != nil {
66		return fmt.Errorf("encoding JSON: %w", err)
67	}
68
69	return nil
70}
71
72func printPersonDetails(cmd *cobra.Command, person *lunatask.Person) error {
73	link, _ := lunatask.BuildDeepLink(lunatask.ResourcePerson, person.ID)
74
75	fmt.Fprintf(cmd.OutOrStdout(), "%s\n", ui.H1.Render("Person"))
76	fmt.Fprintf(cmd.OutOrStdout(), "  ID:   %s\n", person.ID)
77	fmt.Fprintf(cmd.OutOrStdout(), "  Link: %s\n", link)
78
79	if person.RelationshipStrength != nil {
80		fmt.Fprintf(cmd.OutOrStdout(), "  Relationship: %s\n", *person.RelationshipStrength)
81	}
82
83	if len(person.Sources) > 0 {
84		fmt.Fprintf(cmd.OutOrStdout(), "  Sources:\n")
85
86		for _, src := range person.Sources {
87			fmt.Fprintf(cmd.OutOrStdout(), "    - %s: %s\n", src.Source, src.SourceID)
88		}
89	}
90
91	fmt.Fprintf(cmd.OutOrStdout(), "  Created: %s\n", ui.FormatDate(person.CreatedAt))
92	fmt.Fprintf(cmd.OutOrStdout(), "  Updated: %s\n", ui.FormatDate(person.UpdatedAt))
93
94	return nil
95}