Return zets as JSON if --json or -j is passed

This commit is contained in:
Oliver Davies 2025-09-30 18:00:00 +01:00
parent 035d575f0e
commit 329d6d34e0
4 changed files with 88 additions and 4 deletions

View file

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"log"
"strings"
"github.com/spf13/cobra"
@ -13,12 +14,28 @@ var titlesCmd = &cobra.Command{
Use: "titles",
Aliases: []string{"t"},
Short: "Print IDs and titles of zettels",
Long: `Print the IDs and titles of all zettels.`,
Long: `Print the IDs and titles of all zettels.
If -j or --json is added, the results will be in JSON format.`,
Run: func(cmd *cobra.Command, args []string) {
zets := lib.GetAllZets()
result := lib.ParseZetList(zets)
fmt.Println(strings.Join(result, "\n"))
if jsonOutput {
json, err := lib.AsJSON(result)
if err != nil {
log.Fatal(err)
}
fmt.Println(json)
} else {
fmt.Println(strings.Join(result, "\n"))
}
},
}
func init() {
titlesCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
}