From 80a0796bec2d8bc99bc8d271534089ce73dda0f9 Mon Sep 17 00:00:00 2001 From: Charles Xu Date: Wed, 28 Nov 2018 16:55:13 -0800 Subject: [PATCH] V-style logging with glog and check errs (#18) * Level logging with glog and check errs * fix typo and linter and makefile * address code review * remove glog vendor dep --- Makefile | 10 +++++----- bucket.go | 3 ++- client.go | 16 ++++++++++------ client_test.go | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 6fce0fc..cedb4b1 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SOURCEDIR=. SOURCES = $(shell find $(SOURCEDIR) -name '*.go') -GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor) -VERSION=$(git describe --always --tags) +GOFMT_FILES?=$$(shell find . -name '*.go' | grep -v vendor) +VERSION=$(shell git describe --always --tags) BINARY=bin/runscope bin: $(BINARY) @@ -11,9 +11,9 @@ $(BINARY): $(SOURCES) build: go get github.com/golang/lint/golint - go test $(go list ./... | grep -v /vendor/) - go vet $(go list ./... | grep -v /vendor/) - golint $(go list ./... | grep -v /vendor/) + go test $(shell go list ./... | grep -v /vendor/) + go vet $(shell go list ./... | grep -v /vendor/) + golint $(shell go list ./... | grep -v /vendor/) fmt: gofmt -w $(GOFMT_FILES) diff --git a/bucket.go b/bucket.go index 926b1f6..cf84bd0 100644 --- a/bucket.go +++ b/bucket.go @@ -110,12 +110,13 @@ func (client *Client) ListBuckets() ([]*Bucket, error) { return buckets, err } +// ListTestsInput represents the input to ListTests func type ListTestsInput struct { BucketName string Count int } -// ListBuckets lists all buckets for an account +// ListTests lists all tests for a bucket func (client *Client) ListTests(input *ListTestsInput) ([]*Test, error) { count := input.Count if count == 0 { diff --git a/client.go b/client.go index 214c31c..409f1ef 100644 --- a/client.go +++ b/client.go @@ -5,13 +5,13 @@ import ( "encoding/json" "fmt" "io" + "io/ioutil" "net/http" "net/url" - - "github.com/hashicorp/go-cleanhttp" - "io/ioutil" "strings" "sync" + + "github.com/hashicorp/go-cleanhttp" ) // APIURL is the default runscope api uri @@ -156,7 +156,10 @@ func (client *Client) readResource(resourceType string, resourceName string, end } defer resp.Body.Close() - bodyBytes, _ := ioutil.ReadAll(resp.Body) + bodyBytes, err := ioutil.ReadAll(resp.Body) + if err != nil { + return response, err + } bodyString := string(bodyBytes) DebugF(2, " response: %d %s", resp.StatusCode, bodyString) @@ -166,12 +169,13 @@ func (client *Client) readResource(resourceType string, resourceName string, end return response, fmt.Errorf("Status: %s Error reading %s: %s", resp.Status, resourceType, resourceName) } - return response, fmt.Errorf("Status: %s Error reading %s: %s, reason: %q", resp.Status, resourceType, resourceName, errorResp.ErrorMessage) } - json.Unmarshal(bodyBytes, &response) + if err = json.Unmarshal(bodyBytes, &response); err != nil { + return response, fmt.Errorf("failed to Unmarshal response body: %v", err) + } return response, nil } diff --git a/client_test.go b/client_test.go index 171145b..d72d97d 100644 --- a/client_test.go +++ b/client_test.go @@ -51,7 +51,7 @@ func clientConfigure() *Client { func testPreCheck(t *testing.T) { skip := os.Getenv("RUNSCOPE_ACC") == "" if skip { - t.Log("runscope client.go tests require setting RUNSCOPE") + t.Log("runscope client.go tests require setting RUNSCOPE_ACC") t.Skip() }