@@ -24,7 +24,6 @@ import (
2424 "github.com/cli/cli/pkg/cmdutil"
2525 "github.com/cli/cli/pkg/iostreams"
2626 "github.com/cli/cli/pkg/jsoncolor"
27- "github.com/itchyny/gojq"
2827 "github.com/spf13/cobra"
2928)
3029
@@ -99,6 +98,10 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
9998 original query accepts an %[1]s$endCursor: String%[1]s variable and that it fetches the
10099 %[1]spageInfo{ hasNextPage, endCursor }%[1]s set of fields from a collection.
101100
101+ The %[1]s--filter%[1]s option accepts a query in jq syntax and will print only the resulting
102+ values that match the query. This is equivalent to piping the output to %[1]sjq -r%[1]s.
103+ To learn more about the query syntax, see: https://stedolan.github.io/jq/manual/v1.6/
104+
102105 With %[1]s--template%[1]s, the provided Go template is rendered using the JSON data as input.
103106 For the syntax of Go templates, see: https://golang.org/pkg/text/template/
104107
@@ -123,6 +126,9 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
123126 # set a custom HTTP header
124127 $ gh api -H 'Accept: application/vnd.github.XYZ-preview+json' ...
125128
129+ # print only specific fields from the response
130+ $ gh api repos/:owner/:repo/issues --filter '.[].title'
131+
126132 # use a template for the output
127133 $ gh api repos/:owner/:repo/issues --template \
128134 '{{range .}}{{.title}} ({{.labels | pluck "name" | join ", " | color "yellow"}}){{"\n"}}{{end}}'
@@ -199,7 +205,7 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
199205 cmd .Flags ().StringVar (& opts .RequestInputFile , "input" , "" , "The `file` to use as body for the HTTP request" )
200206 cmd .Flags ().BoolVar (& opts .Silent , "silent" , false , "Do not print the response body" )
201207 cmd .Flags ().StringVarP (& opts .Template , "template" , "t" , "" , "Format the response using a Go template" )
202- cmd .Flags ().StringVar (& opts .FilterOutput , "filter" , "" , "Filter the response using jq syntax" )
208+ cmd .Flags ().StringVar (& opts .FilterOutput , "filter" , "" , "Filter fields from the response using jq syntax" )
203209 cmd .Flags ().DurationVar (& opts .CacheTTL , "cache" , 0 , "Cache the response, e.g. \" 3600s\" , \" 60m\" , \" 1h\" " )
204210 return cmd
205211}
@@ -328,38 +334,11 @@ func processResponse(resp *http.Response, opts *ApiOptions, headersOutputStream
328334 }
329335
330336 if opts .FilterOutput != "" {
331- var query * gojq.Query
332- query , err = gojq .Parse (opts .FilterOutput )
333- if err != nil {
334- return
335- }
336-
337- var jsonData []byte
338- jsonData , err = ioutil .ReadAll (responseBody )
337+ // TODO: reuse parsed query across pagination invocations
338+ err = filterJSON (opts .IO .Out , responseBody , opts .FilterOutput )
339339 if err != nil {
340340 return
341341 }
342-
343- var responseData interface {}
344- err = json .Unmarshal (jsonData , & responseData )
345- if err != nil {
346- return
347- }
348-
349- iter := query .Run (responseData )
350- for {
351- var v interface {}
352- var ok bool
353- v , ok = iter .Next ()
354- if ! ok {
355- break
356- }
357- err , ok = v .(error )
358- if ok {
359- return
360- }
361- fmt .Fprintf (opts .IO .Out , "%v\n " , v )
362- }
363342 } else if opts .Template != "" {
364343 // TODO: reuse parsed template across pagination invocations
365344 var t * template.Template
0 commit comments