show.go

  1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
  2//
  3// SPDX-License-Identifier: AGPL-3.0-or-later
  4
  5package note
  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/config"
 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 note by ID. Exported for potential use by shortcuts.
 20var ShowCmd = &cobra.Command{
 21	Use:   "show ID",
 22	Short: "Show note details",
 23	Long: `Show detailed information for a note.
 24
 25Accepts a UUID or lunatask:// deep link.
 26
 27Note: Due to end-to-end encryption, note name and content
 28are not available through 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	note, err := apiClient.GetNote(cmd.Context(), id)
 49	if err != nil {
 50		return err
 51	}
 52
 53	if mustGetBoolFlag(cmd, "json") {
 54		return outputNoteJSON(cmd, note)
 55	}
 56
 57	return printNoteDetails(cmd, note)
 58}
 59
 60func outputNoteJSON(cmd *cobra.Command, note *lunatask.Note) error {
 61	enc := json.NewEncoder(cmd.OutOrStdout())
 62	enc.SetIndent("", "  ")
 63
 64	if err := enc.Encode(note); err != nil {
 65		return fmt.Errorf("encoding JSON: %w", err)
 66	}
 67
 68	return nil
 69}
 70
 71func printNoteDetails(cmd *cobra.Command, note *lunatask.Note) error {
 72	link, _ := lunatask.BuildDeepLink(lunatask.ResourceNote, note.ID)
 73
 74	fmt.Fprintf(cmd.OutOrStdout(), "%s\n", ui.H1.Render("Note"))
 75	fmt.Fprintf(cmd.OutOrStdout(), "  ID:   %s\n", note.ID)
 76	fmt.Fprintf(cmd.OutOrStdout(), "  Link: %s\n", link)
 77
 78	printNotebook(cmd, note)
 79
 80	if note.DateOn != nil {
 81		fmt.Fprintf(cmd.OutOrStdout(), "  Date: %s\n", ui.FormatDate(note.DateOn.Time))
 82	}
 83
 84	if note.Pinned {
 85		fmt.Fprintf(cmd.OutOrStdout(), "  Pinned: yes\n")
 86	}
 87
 88	if len(note.Sources) > 0 {
 89		fmt.Fprintf(cmd.OutOrStdout(), "  Sources:\n")
 90
 91		for _, src := range note.Sources {
 92			fmt.Fprintf(cmd.OutOrStdout(), "    - %s: %s\n", src.Source, src.SourceID)
 93		}
 94	}
 95
 96	fmt.Fprintf(cmd.OutOrStdout(), "  Created: %s\n", ui.FormatDate(note.CreatedAt))
 97	fmt.Fprintf(cmd.OutOrStdout(), "  Updated: %s\n", ui.FormatDate(note.UpdatedAt))
 98
 99	return nil
100}
101
102func printNotebook(cmd *cobra.Command, note *lunatask.Note) {
103	if note.NotebookID == nil {
104		return
105	}
106
107	notebookDisplay := *note.NotebookID
108	cfg, _ := config.Load()
109
110	if cfg != nil {
111		if notebook := cfg.NotebookByID(*note.NotebookID); notebook != nil {
112			notebookDisplay = fmt.Sprintf("%s (%s)", notebook.Name, notebook.Key)
113		}
114	}
115
116	fmt.Fprintf(cmd.OutOrStdout(), "  Notebook: %s\n", notebookDisplay)
117}