From cb2308c07767f990b3f3ddffd37ce2b7eeade03e Mon Sep 17 00:00:00 2001 From: AliabbasMerchant Date: Mon, 29 Jun 2020 00:15:34 +0530 Subject: [PATCH 1/4] --silent flag in api --- pkg/cmd/api/api.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index e1edabbc0..6b393cc42 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -34,6 +34,7 @@ type ApiOptions struct { RequestHeaders []string ShowResponseHeaders bool Paginate bool + Silent bool HttpClient func() (*http.Client, error) BaseRepo func() (ghrepo.Interface, error) @@ -134,6 +135,7 @@ original query accepts an '$endCursor: String' variable and that it fetches the cmd.Flags().BoolVarP(&opts.ShowResponseHeaders, "include", "i", false, "Include HTTP response headers in the output") cmd.Flags().BoolVar(&opts.Paginate, "paginate", false, "Make additional HTTP requests to fetch all pages of results") cmd.Flags().StringVar(&opts.RequestInputFile, "input", "", "The file to use as body for the HTTP request") + cmd.Flags().BoolVar(&opts.Silent, "silent", false, "Silence the output") return cmd } @@ -178,6 +180,10 @@ func apiRun(opts *ApiOptions) error { return err } + if opts.Silent { + opts.IO.Out = ioutil.Discard + } + hasNextPage := true for hasNextPage { resp, err := httpRequest(httpClient, method, requestPath, requestBody, requestHeaders) From 5e56e3138449a1a12ff2f7838ad70ea0ac8d1be9 Mon Sep 17 00:00:00 2001 From: AliabbasMerchant Date: Mon, 29 Jun 2020 00:30:24 +0530 Subject: [PATCH 2/4] Added Test for --silent flag in api --- pkg/cmd/api/api_test.go | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/pkg/cmd/api/api_test.go b/pkg/cmd/api/api_test.go index 7b89d715c..a41f82c54 100644 --- a/pkg/cmd/api/api_test.go +++ b/pkg/cmd/api/api_test.go @@ -39,6 +39,7 @@ func Test_NewCmdApi(t *testing.T) { RequestHeaders: []string(nil), ShowResponseHeaders: false, Paginate: false, + Silent: false, }, wantsErr: false, }, @@ -55,6 +56,7 @@ func Test_NewCmdApi(t *testing.T) { RequestHeaders: []string(nil), ShowResponseHeaders: false, Paginate: false, + Silent: false, }, wantsErr: false, }, @@ -71,6 +73,7 @@ func Test_NewCmdApi(t *testing.T) { RequestHeaders: []string(nil), ShowResponseHeaders: false, Paginate: false, + Silent: false, }, wantsErr: false, }, @@ -87,6 +90,7 @@ func Test_NewCmdApi(t *testing.T) { RequestHeaders: []string{"accept: text/plain"}, ShowResponseHeaders: true, Paginate: false, + Silent: false, }, wantsErr: false, }, @@ -103,6 +107,24 @@ func Test_NewCmdApi(t *testing.T) { RequestHeaders: []string(nil), ShowResponseHeaders: false, Paginate: true, + Silent: false, + }, + wantsErr: false, + }, + { + name: "with silenced output", + cli: "repos/OWNER/REPO/issues --silent", + wants: ApiOptions{ + RequestMethod: "GET", + RequestMethodPassed: false, + RequestPath: "repos/OWNER/REPO/issues", + RequestInputFile: "", + RawFields: []string(nil), + MagicFields: []string(nil), + RequestHeaders: []string(nil), + ShowResponseHeaders: false, + Paginate: false, + Silent: true, }, wantsErr: false, }, @@ -124,6 +146,7 @@ func Test_NewCmdApi(t *testing.T) { RequestHeaders: []string(nil), ShowResponseHeaders: false, Paginate: true, + Silent: false, }, wantsErr: false, }, @@ -145,6 +168,7 @@ func Test_NewCmdApi(t *testing.T) { RequestHeaders: []string(nil), ShowResponseHeaders: false, Paginate: false, + Silent: false, }, wantsErr: false, }, @@ -425,6 +449,37 @@ func Test_apiRun_paginationGraphQL(t *testing.T) { assert.Equal(t, "PAGE1_END", endCursor) } +func Test_apiRun_silent(t *testing.T) { + io, _, stdout, stderr := iostreams.Test() + response := &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString(`body`)), + Header: http.Header{"Content-Type": []string{"text/plain"}}, + } + + options := ApiOptions{ + IO: io, + HttpClient: func() (*http.Client, error) { + var tr roundTripper = func(req *http.Request) (*http.Response, error) { + resp := response + resp.Request = req + return resp, nil + } + return &http.Client{Transport: tr}, nil + }, + RequestPath: "issues", + Silent: true, + } + + err := apiRun(&options) + assert.NoError(t, err) + + assert.Equal(t, "", stdout.String(), "stdout") + assert.Equal(t, "", stderr.String(), "stderr") + + assert.Equal(t, "https://api.github.com/issues", response.Request.URL.String()) +} + func Test_apiRun_inputFile(t *testing.T) { tests := []struct { name string From aa43c55f60d4818f482ae64ef74f46e7e957c2c3 Mon Sep 17 00:00:00 2001 From: AliabbasMerchant Date: Mon, 29 Jun 2020 07:13:06 +0530 Subject: [PATCH 3/4] Skip printing headers when --silent in api --- pkg/cmd/api/api.go | 2 +- pkg/cmd/api/api_test.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index 6b393cc42..15a2b3be4 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -218,7 +218,7 @@ func apiRun(opts *ApiOptions) error { } func processResponse(resp *http.Response, opts *ApiOptions) (endCursor string, err error) { - if opts.ShowResponseHeaders { + if opts.ShowResponseHeaders && !opts.Silent { fmt.Fprintln(opts.IO.Out, resp.Proto, resp.Status) printHeaders(opts.IO.Out, resp.Header, opts.IO.ColorEnabled()) fmt.Fprint(opts.IO.Out, "\r\n") diff --git a/pkg/cmd/api/api_test.go b/pkg/cmd/api/api_test.go index a41f82c54..75c8eaa4a 100644 --- a/pkg/cmd/api/api_test.go +++ b/pkg/cmd/api/api_test.go @@ -467,8 +467,9 @@ func Test_apiRun_silent(t *testing.T) { } return &http.Client{Transport: tr}, nil }, - RequestPath: "issues", - Silent: true, + RequestPath: "issues", + ShowResponseHeaders: true, + Silent: true, } err := apiRun(&options) From 7a04bf1672eeffaa544b6f10e461d277b5180e41 Mon Sep 17 00:00:00 2001 From: AliabbasMerchant Date: Tue, 30 Jun 2020 19:18:28 +0530 Subject: [PATCH 4/4] `api --silent` Changes: Show Response Headers (if requested) even with `--silent` flag Shift silent tests to `Test_apiRun` Changed usage string of `--silent` flag --- pkg/cmd/api/api.go | 15 +++++----- pkg/cmd/api/api_test.go | 62 ++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index 15a2b3be4..170c41b55 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -135,7 +135,7 @@ original query accepts an '$endCursor: String' variable and that it fetches the cmd.Flags().BoolVarP(&opts.ShowResponseHeaders, "include", "i", false, "Include HTTP response headers in the output") cmd.Flags().BoolVar(&opts.Paginate, "paginate", false, "Make additional HTTP requests to fetch all pages of results") cmd.Flags().StringVar(&opts.RequestInputFile, "input", "", "The file to use as body for the HTTP request") - cmd.Flags().BoolVar(&opts.Silent, "silent", false, "Silence the output") + cmd.Flags().BoolVar(&opts.Silent, "silent", false, "Do not print the response body") return cmd } @@ -180,6 +180,7 @@ func apiRun(opts *ApiOptions) error { return err } + headersOutputStream := opts.IO.Out if opts.Silent { opts.IO.Out = ioutil.Discard } @@ -191,7 +192,7 @@ func apiRun(opts *ApiOptions) error { return err } - endCursor, err := processResponse(resp, opts) + endCursor, err := processResponse(resp, opts, headersOutputStream) if err != nil { return err } @@ -217,11 +218,11 @@ func apiRun(opts *ApiOptions) error { return nil } -func processResponse(resp *http.Response, opts *ApiOptions) (endCursor string, err error) { - if opts.ShowResponseHeaders && !opts.Silent { - fmt.Fprintln(opts.IO.Out, resp.Proto, resp.Status) - printHeaders(opts.IO.Out, resp.Header, opts.IO.ColorEnabled()) - fmt.Fprint(opts.IO.Out, "\r\n") +func processResponse(resp *http.Response, opts *ApiOptions, headersOutputStream io.Writer) (endCursor string, err error) { + if opts.ShowResponseHeaders { + fmt.Fprintln(headersOutputStream, resp.Proto, resp.Status) + printHeaders(headersOutputStream, resp.Header, opts.IO.ColorEnabled()) + fmt.Fprint(headersOutputStream, "\r\n") } if resp.StatusCode == 204 { diff --git a/pkg/cmd/api/api_test.go b/pkg/cmd/api/api_test.go index 75c8eaa4a..f590d93b4 100644 --- a/pkg/cmd/api/api_test.go +++ b/pkg/cmd/api/api_test.go @@ -288,6 +288,36 @@ func Test_apiRun(t *testing.T) { stdout: `gateway timeout`, stderr: "gh: HTTP 502\n", }, + { + name: "silent", + options: ApiOptions{ + Silent: true, + }, + httpResponse: &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString(`body`)), + }, + err: nil, + stdout: ``, + stderr: ``, + }, + { + name: "show response headers even when silent", + options: ApiOptions{ + ShowResponseHeaders: true, + Silent: true, + }, + httpResponse: &http.Response{ + Proto: "HTTP/1.1", + Status: "200 Okey-dokey", + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString(`body`)), + Header: http.Header{"Content-Type": []string{"text/plain"}}, + }, + err: nil, + stdout: "HTTP/1.1 200 Okey-dokey\nContent-Type: text/plain\r\n\r\n", + stderr: ``, + }, } for _, tt := range tests { @@ -449,38 +479,6 @@ func Test_apiRun_paginationGraphQL(t *testing.T) { assert.Equal(t, "PAGE1_END", endCursor) } -func Test_apiRun_silent(t *testing.T) { - io, _, stdout, stderr := iostreams.Test() - response := &http.Response{ - StatusCode: 200, - Body: ioutil.NopCloser(bytes.NewBufferString(`body`)), - Header: http.Header{"Content-Type": []string{"text/plain"}}, - } - - options := ApiOptions{ - IO: io, - HttpClient: func() (*http.Client, error) { - var tr roundTripper = func(req *http.Request) (*http.Response, error) { - resp := response - resp.Request = req - return resp, nil - } - return &http.Client{Transport: tr}, nil - }, - RequestPath: "issues", - ShowResponseHeaders: true, - Silent: true, - } - - err := apiRun(&options) - assert.NoError(t, err) - - assert.Equal(t, "", stdout.String(), "stdout") - assert.Equal(t, "", stderr.String(), "stderr") - - assert.Equal(t, "https://api.github.com/issues", response.Request.URL.String()) -} - func Test_apiRun_inputFile(t *testing.T) { tests := []struct { name string