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"
"os"
"strings"
@ -14,7 +15,9 @@ var findCmd = &cobra.Command{
Use: "find",
Aliases: []string{"f", "s", "search"},
Short: "Print IDs and titles of zettels matching QUERY",
Long: `zet find QUERY`,
Long: `zet find QUERY
If -j or --json is added, the results will be in JSON format.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("No query")
@ -26,6 +29,20 @@ var findCmd = &cobra.Command{
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() {
findCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format")
}

View file

@ -6,6 +6,8 @@ import (
"github.com/spf13/cobra"
)
var jsonOutput bool
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "zet",

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")
}

48
internal/lib/json.go Normal file
View file

@ -0,0 +1,48 @@
package lib
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
)
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
type Item struct {
ID int `json:"id"`
Title string `json:"title"`
}
func AsJSON(zets []string) (string, error) {
var items []Item
for _, entry := range zets {
cleanEntry := stripANSI(entry)
parts := strings.SplitN(cleanEntry, " ", 2)
id, _ := strconv.Atoi(parts[0])
item := Item{
ID: id,
Title: parts[1],
}
items = append(items, item)
}
jsonData, err := json.MarshalIndent(items, "", " ")
if err != nil {
fmt.Println("Error marshaling to JSON:", err)
return "", err
}
return string(jsonData), nil
}
func stripANSI(s string) string {
return ansiRegex.ReplaceAllString(s, "")
}