Return zets as JSON if --json
or -j
is passed
This commit is contained in:
parent
035d575f0e
commit
329d6d34e0
4 changed files with 88 additions and 4 deletions
21
cmd/find.go
21
cmd/find.go
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue