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