Change the api --filter flag to api --jq

This commit is contained in:
Mislav Marohnić 2021-03-04 17:05:31 +01:00
parent 4c26d617d3
commit 06eeea0737
2 changed files with 38 additions and 4 deletions

View file

@ -98,9 +98,10 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
original query accepts an %[1]s$endCursor: String%[1]s variable and that it fetches the
%[1]spageInfo{ hasNextPage, endCursor }%[1]s set of fields from a collection.
The %[1]s--filter%[1]s option accepts a query in jq syntax and will print only the resulting
values that match the query. This is equivalent to piping the output to %[1]sjq -r%[1]s.
To learn more about the query syntax, see: https://stedolan.github.io/jq/manual/v1.6/
The %[1]s--jq%[1]s option accepts a query in jq syntax and will print only the resulting
values that match the query. This is equivalent to piping the output to %[1]sjq -r%[1]s,
but does not require the jq utility to be installed on the system. To learn more
about the query syntax, see: https://stedolan.github.io/jq/manual/v1.6/
With %[1]s--template%[1]s, the provided Go template is rendered using the JSON data as input.
For the syntax of Go templates, see: https://golang.org/pkg/text/template/
@ -209,7 +210,7 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
cmd.Flags().StringVar(&opts.RequestInputFile, "input", "", "The `file` to use as body for the HTTP request")
cmd.Flags().BoolVar(&opts.Silent, "silent", false, "Do not print the response body")
cmd.Flags().StringVarP(&opts.Template, "template", "t", "", "Format the response using a Go template")
cmd.Flags().StringVar(&opts.FilterOutput, "filter", "", "Filter fields from the response using jq syntax")
cmd.Flags().StringVarP(&opts.FilterOutput, "jq", "q", "", "Query to select values from the response using jq syntax")
cmd.Flags().DurationVar(&opts.CacheTTL, "cache", 0, "Cache the response, e.g. \"3600s\", \"60m\", \"1h\"")
return cmd
}

View file

@ -48,6 +48,7 @@ func Test_NewCmdApi(t *testing.T) {
Silent: false,
CacheTTL: 0,
Template: "",
FilterOutput: "",
},
wantsErr: false,
},
@ -68,6 +69,7 @@ func Test_NewCmdApi(t *testing.T) {
Silent: false,
CacheTTL: 0,
Template: "",
FilterOutput: "",
},
wantsErr: false,
},
@ -88,6 +90,7 @@ func Test_NewCmdApi(t *testing.T) {
Silent: false,
CacheTTL: 0,
Template: "",
FilterOutput: "",
},
wantsErr: false,
},
@ -108,6 +111,7 @@ func Test_NewCmdApi(t *testing.T) {
Silent: false,
CacheTTL: 0,
Template: "",
FilterOutput: "",
},
wantsErr: false,
},
@ -128,6 +132,7 @@ func Test_NewCmdApi(t *testing.T) {
Silent: false,
CacheTTL: 0,
Template: "",
FilterOutput: "",
},
wantsErr: false,
},
@ -148,6 +153,7 @@ func Test_NewCmdApi(t *testing.T) {
Silent: true,
CacheTTL: 0,
Template: "",
FilterOutput: "",
},
wantsErr: false,
},
@ -173,6 +179,7 @@ func Test_NewCmdApi(t *testing.T) {
Silent: false,
CacheTTL: 0,
Template: "",
FilterOutput: "",
},
wantsErr: false,
},
@ -198,6 +205,7 @@ func Test_NewCmdApi(t *testing.T) {
Silent: false,
CacheTTL: 0,
Template: "",
FilterOutput: "",
},
wantsErr: false,
},
@ -223,6 +231,7 @@ func Test_NewCmdApi(t *testing.T) {
Silent: false,
CacheTTL: 0,
Template: "",
FilterOutput: "",
},
wantsErr: false,
},
@ -243,6 +252,7 @@ func Test_NewCmdApi(t *testing.T) {
Silent: false,
CacheTTL: time.Minute * 5,
Template: "",
FilterOutput: "",
},
wantsErr: false,
},
@ -263,6 +273,28 @@ func Test_NewCmdApi(t *testing.T) {
Silent: false,
CacheTTL: 0,
Template: "hello {{.name}}",
FilterOutput: "",
},
wantsErr: false,
},
{
name: "with jq filter",
cli: "user -q .name",
wants: ApiOptions{
Hostname: "",
RequestMethod: "GET",
RequestMethodPassed: false,
RequestPath: "user",
RequestInputFile: "",
RawFields: []string(nil),
MagicFields: []string(nil),
RequestHeaders: []string(nil),
ShowResponseHeaders: false,
Paginate: false,
Silent: false,
CacheTTL: 0,
Template: "",
FilterOutput: ".name",
},
wantsErr: false,
},
@ -301,6 +333,7 @@ func Test_NewCmdApi(t *testing.T) {
assert.Equal(t, tt.wants.Silent, opts.Silent)
assert.Equal(t, tt.wants.CacheTTL, opts.CacheTTL)
assert.Equal(t, tt.wants.Template, opts.Template)
assert.Equal(t, tt.wants.FilterOutput, opts.FilterOutput)
})
}
}