diff options
author | Martin Polden <mpolden@mpolden.no> | 2021-08-26 11:46:20 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2021-08-26 11:46:20 +0200 |
commit | fee0f6489a6aa734e008b10ef275e464e66bfc17 (patch) | |
tree | 80bc9272a7b09d22f762c392130608c872246126 /client/go/util | |
parent | 58224ccdae4531dac1b0634d27cf894a6a61a779 (diff) |
Cleanup printing and colorization
Diffstat (limited to 'client/go/util')
-rw-r--r-- | client/go/util/http.go | 21 | ||||
-rw-r--r-- | client/go/util/http_test.go | 9 | ||||
-rw-r--r-- | client/go/util/io.go | 18 | ||||
-rw-r--r-- | client/go/util/print.go | 78 |
4 files changed, 31 insertions, 95 deletions
diff --git a/client/go/util/http.go b/client/go/util/http.go index 6a4bab6c108..38224d2a842 100644 --- a/client/go/util/http.go +++ b/client/go/util/http.go @@ -5,6 +5,7 @@ package util import ( + "fmt" "net/http" "net/url" "strings" @@ -36,20 +37,18 @@ func CreateClient(timeout time.Duration) HttpClient { } // Convenience function for doing a HTTP GET -func HttpGet(host string, path string, description string) *http.Response { - url, urlError := url.Parse(host + path) - if urlError != nil { - Error("Invalid target url '" + host + path + "'") - return nil +func HttpGet(host string, path string, description string) (*http.Response, error) { + url, err := url.Parse(host + path) + if err != nil { + return nil, fmt.Errorf("Invalid target URL: %s: %w", host+path, err) } return HttpDo(&http.Request{URL: url}, time.Second*10, description) } -func HttpDo(request *http.Request, timeout time.Duration, description string) *http.Response { - response, error := ActiveHttpClient.Do(request, timeout) - if error != nil { - Error("Could not connect to", strings.ToLower(description), "at", request.URL.Host) - Detail(error.Error()) +func HttpDo(request *http.Request, timeout time.Duration, description string) (*http.Response, error) { + response, err := ActiveHttpClient.Do(request, timeout) + if err != nil { + return nil, fmt.Errorf("Could not connect to %s at %s: %w", strings.ToLower(description), request.URL.Host, err) } - return response + return response, nil } diff --git a/client/go/util/http_test.go b/client/go/util/http_test.go index 731fc935a1d..54114aefb64 100644 --- a/client/go/util/http_test.go +++ b/client/go/util/http_test.go @@ -6,11 +6,12 @@ package util import ( "bytes" - "github.com/stretchr/testify/assert" "io/ioutil" "net/http" "testing" "time" + + "github.com/stretchr/testify/assert" ) type mockHttpClient struct{} @@ -37,9 +38,11 @@ func (c mockHttpClient) Do(request *http.Request, timeout time.Duration) (respon func TestHttpRequest(t *testing.T) { ActiveHttpClient = mockHttpClient{} - response := HttpGet("http://host", "/okpath", "description") + response, err := HttpGet("http://host", "/okpath", "description") + assert.Nil(t, err) assert.Equal(t, 200, response.StatusCode) - response = HttpGet("http://host", "/otherpath", "description") + response, err = HttpGet("http://host", "/otherpath", "description") + assert.Nil(t, err) assert.Equal(t, 500, response.StatusCode) } diff --git a/client/go/util/io.go b/client/go/util/io.go index cbde22ad0eb..5ce9708ed7a 100644 --- a/client/go/util/io.go +++ b/client/go/util/io.go @@ -6,6 +6,7 @@ package util import ( "bytes" + "encoding/json" "errors" "io" "os" @@ -26,14 +27,25 @@ func IsDirectory(path string) bool { // Returns the content of a reader as a string func ReaderToString(reader io.Reader) string { - buffer := new(strings.Builder) - io.Copy(buffer, reader) + var buffer strings.Builder + io.Copy(&buffer, reader) return buffer.String() } // Returns the content of a reader as a byte array func ReaderToBytes(reader io.Reader) []byte { - buffer := new(bytes.Buffer) + var buffer bytes.Buffer buffer.ReadFrom(reader) return buffer.Bytes() } + +// Returns the contents of reader as indented JSON +func ReaderToJSON(reader io.Reader) string { + bodyBytes := ReaderToBytes(reader) + var prettyJSON bytes.Buffer + parseError := json.Indent(&prettyJSON, bodyBytes, "", " ") + if parseError != nil { // Not JSON: Print plainly + return string(bodyBytes) + } + return string(prettyJSON.Bytes()) +} diff --git a/client/go/util/print.go b/client/go/util/print.go deleted file mode 100644 index ce116378222..00000000000 --- a/client/go/util/print.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -// Print functions for color-coded text. -// Author: bratseth - -package util - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - "os" - "strings" -) - -// Set this to have output written somewhere else than os.Stdout -var Out io.Writer - -func init() { - Out = os.Stdout -} - -// Prints in default color -func Print(messages ...string) { - print("", messages) -} - -// Prints in a color appropriate for errors -func Error(messages ...string) { - print("\033[31m", messages) -} - -// FatalIfError prints error and exists if given err is non-nill. -func FatalIfErr(err error) { - if err != nil { - Error(err.Error()) - os.Exit(1) - } -} - -// Prints in a color appropriate for success messages -func Success(messages ...string) { - print("\033[32m", messages) -} - -// Prints in a color appropriate for detail messages -func Detail(messages ...string) { - print("\033[33m", messages) -} - -// Prints all the text of the given reader -func PrintReader(reader io.Reader) { - 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())) - } -} - -func print(prefix string, messages []string) { - fmt.Fprint(Out, prefix) - for i := 0; i < len(messages); i++ { - fmt.Fprint(Out, messages[i]) - if i < len(messages)-1 { - fmt.Fprint(Out, " ") - } - } - // TODO: Use "log" instead of this package and something like https://github.com/logrusorgru/aurora for colorization - // Since automatic colorisation needs to support Windows, we probably need github.com/mattn/go-isatty to - // detect TTY - if strings.HasPrefix(prefix, "\033") { - fmt.Fprint(Out, "\033[0m") // Terminate colors - } - fmt.Fprintln(Out, "") -} |