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 := apiClient.GetPerson(cmd.Context(), id)
48	if err != nil {
49		return err
50	}
51
52	if mustGetBoolFlag(cmd, "json") {
53		return outputPersonJSON(cmd, person)
54	}
55
56	return printPersonDetails(cmd, person)
57}
58
59func outputPersonJSON(cmd *cobra.Command, person *lunatask.Person) error {
60	enc := json.NewEncoder(cmd.OutOrStdout())
61	enc.SetIndent("", "  ")
62
63	if err := enc.Encode(person); err != nil {
64		return fmt.Errorf("encoding JSON: %w", err)
65	}
66
67	return nil
68}
69
70func printPersonDetails(cmd *cobra.Command, person *lunatask.Person) error {
71	link, _ := lunatask.BuildDeepLink(lunatask.ResourcePerson, person.ID)
72
73	fmt.Fprintf(cmd.OutOrStdout(), "%s\n", ui.H1.Render("Person"))
74	fmt.Fprintf(cmd.OutOrStdout(), "  ID:   %s\n", person.ID)
75	fmt.Fprintf(cmd.OutOrStdout(), "  Link: %s\n", link)
76
77	if person.RelationshipStrength != nil {
78		fmt.Fprintf(cmd.OutOrStdout(), "  Relationship: %s\n", *person.RelationshipStrength)
79	}
80
81	if len(person.Sources) > 0 {
82		fmt.Fprintf(cmd.OutOrStdout(), "  Sources:\n")
83
84		for _, src := range person.Sources {
85			fmt.Fprintf(cmd.OutOrStdout(), "    - %s: %s\n", src.Source, src.SourceID)
86		}
87	}
88
89	fmt.Fprintf(cmd.OutOrStdout(), "  Created: %s\n", ui.FormatDate(person.CreatedAt))
90	fmt.Fprintf(cmd.OutOrStdout(), "  Updated: %s\n", ui.FormatDate(person.UpdatedAt))
91
92	return nil
93}