diff options
author | Jon Bratseth <bratseth@gmail.com> | 2021-08-24 11:43:34 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2021-08-24 11:43:34 +0200 |
commit | 20ecdcec71d0443b5b5085c322dc9f48ad24c5a0 (patch) | |
tree | 75be32f3d8d957c8fd9e756440e56d79b1f41792 /client | |
parent | 4cf24301465f021893aa9439c20e4834760930b1 (diff) |
JSON prettyprint responses
Diffstat (limited to 'client')
-rw-r--r-- | client/go/cmd/query_test.go | 15 | ||||
-rw-r--r-- | client/go/util/io.go | 12 | ||||
-rw-r--r-- | client/go/util/print.go | 17 |
3 files changed, 34 insertions, 10 deletions
diff --git a/client/go/cmd/query_test.go b/client/go/cmd/query_test.go index ab48c482a7d..f55956bc1ce 100644 --- a/client/go/cmd/query_test.go +++ b/client/go/cmd/query_test.go @@ -16,6 +16,12 @@ func TestQuery(t *testing.T) { "select from sources * where title contains 'foo'") } +func TestQueryNonJsonResult(t *testing.T) { + assertQuery(t, + "?yql=select+from+sources+%2A+where+title+contains+%27foo%27", + "select from sources * where title contains 'foo'") +} + func TestQueryWithMultipleParameters(t *testing.T) { assertQuery(t, "?hits=5&yql=select+from+sources+%2A+where+title+contains+%27foo%27", @@ -37,6 +43,15 @@ func TestServerError(t *testing.T) { } func assertQuery(t *testing.T, expectedQuery string, query ...string) { + client := &mockHttpClient{ nextBody: "{\"query\":\"result\"}", } + assert.Equal(t, + "{\n \"query\": \"result\"\n}\n", + executeCommand(t, client, []string{"query"}, query), + "query output") + assert.Equal(t, getTarget(queryContext).query + "/search/" + expectedQuery, client.lastRequest.URL.String()) +} + +func assertQueryNonJsonResult(t *testing.T, expectedQuery string, query ...string) { client := &mockHttpClient{ nextBody: "query result", } assert.Equal(t, "query result\n", diff --git a/client/go/util/io.go b/client/go/util/io.go index 217beb085d1..d7b849ba9a4 100644 --- a/client/go/util/io.go +++ b/client/go/util/io.go @@ -5,6 +5,7 @@ package util import ( + "bytes" "errors" "io" "os" @@ -24,8 +25,15 @@ func IsDirectory(path string) bool { } // Returns the content of a reader as a string -func ReaderToString(reader io.ReadCloser) string { +func ReaderToString(reader io.Reader) string { buffer := new(strings.Builder) io.Copy(buffer, reader) return buffer.String() -}
\ No newline at end of file +} + +// Returns the content of a reader as a byte array +func ReaderToBytes(reader io.Reader) []byte { + buffer := new(bytes.Buffer) + buffer.ReadFrom(reader) + return buffer.Bytes() +} diff --git a/client/go/util/print.go b/client/go/util/print.go index 7734a2a1a9e..91a223a3c3a 100644 --- a/client/go/util/print.go +++ b/client/go/util/print.go @@ -5,7 +5,8 @@ package util import ( - "bufio" + "bytes" + "encoding/json" "fmt" "io" "os" @@ -40,13 +41,13 @@ func Detail(messages ...string) { // Prints all the text of the given reader func PrintReader(reader io.Reader) { - // TODO: Pretty-print body - scanner := bufio.NewScanner(reader) - for ;scanner.Scan(); { - Print(scanner.Text()) - } - if err := scanner.Err(); err != nil { - Error(err.Error()) + bodyBytes := ReaderToBytes(reader) + var prettyJSON bytes.Buffer + parseError := json.Indent(&prettyJSON, bodyBytes, "", " ") + if parseError != nil { // Not JSON: Print plainly + Print(string(bodyBytes)) + } else { + Print(string(prettyJSON.Bytes())) } } |