Skip to content

Commit 8ec72f9

Browse files
cmd/zoekt: add support for jsonl output (#997)
Currently the zoekt command's output isn't able to surface information like which branch a match corresponds to. By streaming out the matches as lines of json, it can be easily processed by other programs like jq. This is potentially useful for a lot of cli tools. Personally I'm looking to use this with fzf and bat to build a little search tui.
1 parent 83afd4c commit 8ec72f9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

cmd/zoekt/main.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818
import (
1919
"bytes"
2020
"context"
21+
"encoding/json"
2122
"flag"
2223
"fmt"
2324
"io"
@@ -54,6 +55,13 @@ func displayMatches(files []zoekt.FileMatch, pat string, withRepo bool, list boo
5455
}
5556
}
5657

58+
func displayMatchesJSONL(files []zoekt.FileMatch) {
59+
encoder := json.NewEncoder(os.Stdout)
60+
for _, f := range files {
61+
encoder.Encode(f)
62+
}
63+
}
64+
5765
func addTabIfNonEmpty(s string) string {
5866
if s != "" {
5967
return "\t" + s
@@ -165,6 +173,7 @@ func main() {
165173
verbose := flag.Bool("v", false, "print some background data")
166174
withRepo := flag.Bool("r", false, "print the repo before the file name")
167175
list := flag.Bool("l", false, "print matching filenames only")
176+
jsonl := flag.Bool("jsonl", false, "print results in jsonl format")
168177
sym := flag.Bool("sym", false, "do experimental symbol search")
169178

170179
flag.Usage = func() {
@@ -229,7 +238,12 @@ func main() {
229238
sres, _ = searcher.Search(context.Background(), q, &sOpts)
230239
}
231240

232-
displayMatches(sres.Files, pat, *withRepo, *list)
241+
if *jsonl {
242+
displayMatchesJSONL(sres.Files)
243+
} else {
244+
displayMatches(sres.Files, pat, *withRepo, *list)
245+
}
246+
233247
if *verbose {
234248
log.Printf("stats: %#v", sres.Stats)
235249
}

0 commit comments

Comments
 (0)