diff options
| -rw-r--r-- | .golangci.yaml | 36 | ||||
| -rw-r--r-- | Makefile | 15 | ||||
| -rw-r--r-- | account.go | 10 | ||||
| -rw-r--r-- | bulk.go | 4 | ||||
| -rw-r--r-- | container.go | 15 |
5 files changed, 58 insertions, 22 deletions
diff --git a/.golangci.yaml b/.golangci.yaml index 08a5f85..232fb42 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -37,6 +37,10 @@ linters-settings: forbid: # ioutil package has been deprecated: https://github.com/golang/go/issues/42026 - ^ioutil\..*$ + # Using http.DefaultServeMux is discouraged because it's a global variable that some packages silently and magically add handlers to (esp. net/http/pprof). + # Applications wishing to use http.ServeMux should obtain local instances through http.NewServeMux() instead of using the global default instance. + - ^http.DefaultServeMux$ + - ^http.Handle(?:Func)?$ gocritic: enabled-checks: - boolExprSimplify @@ -76,14 +80,22 @@ linters-settings: check-shadowing: true nolintlint: require-specific: true + stylecheck: + dot-import-whitelist: + - github.com/onsi/ginkgo/v2 + - github.com/onsi/gomega usestdlibvars: - http-method: true - http-status-code: true - time-weekday: true - time-month: true - time-layout: true - crypto-hash: true - default-rpc-path: true + constant-kind: true + crypto-hash: true + default-rpc-path: true + http-method: true + http-status-code: true + os-dev-null: true + rpc-default-path: true + time-weekday: true + time-month: true + time-layout: true + tls-signature-scheme: true whitespace: # Enforce newlines (or comments) after multi-line function signatures. multi-func: true @@ -93,10 +105,17 @@ linters: # does not introduce new linters unexpectedly. disable-all: true enable: + - bodyclose + - containedctx - dupl + - dupword + - durationcheck - errcheck + - errorlint - exportloopref - forbidigo + - ginkgolinter + - gocheckcompilerdirectives - gocritic - gofmt - goimports @@ -105,11 +124,14 @@ linters: - govet - ineffassign - misspell + - noctx - nolintlint + - nosprintfhostport - rowserrcheck - sqlclosecheck - staticcheck - stylecheck + - tenv - typecheck - unconvert - unparam @@ -29,7 +29,8 @@ GO_TESTENV = GO_TESTPKGS := $(shell go list -f '{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...) # which packages to measure coverage for GO_COVERPKGS := $(shell go list ./... | grep -Ev '/util') -# to get around weird Makefile syntax restrictions, we need variables containing a space and comma +# to get around weird Makefile syntax restrictions, we need variables containing nothing, a space and comma +null := space := $(null) $(null) comma := , @@ -58,19 +59,22 @@ tidy-deps: FORCE go mod tidy go mod verify -license-headers: FORCE - @if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi - find * \( -name vendor -type d -prune \) -o \( -name \*.go -exec addlicense -c "SAP SE" -- {} + \) - clean: FORCE git clean -dxf build +vars: FORCE + @printf "GO_BUILDFLAGS=$(GO_BUILDFLAGS)\n" + @printf "GO_COVERPKGS=$(GO_COVERPKGS)\n" + @printf "GO_LDFLAGS=$(GO_LDFLAGS)\n" + @printf "GO_TESTENV=$(GO_TESTENV)\n" + @printf "GO_TESTPKGS=$(GO_TESTPKGS)\n" help: FORCE @printf "\n" @printf "\e[1mUsage:\e[0m\n" @printf " make \e[36m<target>\e[0m\n" @printf "\n" @printf "\e[1mGeneral\e[0m\n" + @printf " \e[36mvars\e[0m Display values of relevant Makefile variables.\n" @printf " \e[36mhelp\e[0m Display this help.\n" @printf "\n" @printf "\e[1mTest\e[0m\n" @@ -82,7 +86,6 @@ help: FORCE @printf "\n" @printf "\e[1mDevelopment\e[0m\n" @printf " \e[36mtidy-deps\e[0m Run go mod tidy and go mod verify.\n" - @printf " \e[36mlicense-headers\e[0m Add license headers to all .go files excluding the vendor directory.\n" @printf " \e[36mclean\e[0m Run git clean.\n" .PHONY: FORCE @@ -19,6 +19,7 @@ package schwift import ( + "context" "encoding/json" "fmt" "net/http" @@ -109,6 +110,7 @@ func (a *Account) Headers() (AccountHeaders, error) { if err != nil { return AccountHeaders{}, err } + defer resp.Body.Close() headers := AccountHeaders{headersFromHTTP(resp.Header)} err = headers.Validate() @@ -133,13 +135,14 @@ func (a *Account) Invalidate() { // // A successful POST request implies Invalidate() since it may change metadata. func (a *Account) Update(headers AccountHeaders, opts *RequestOptions) error { - _, err := Request{ + resp, err := Request{ Method: "POST", Options: cloneRequestOptions(opts, headers.Headers), ExpectStatusCodes: []int{204}, }.Do(a.backend) if err == nil { a.Invalidate() + resp.Body.Close() } return err } @@ -149,7 +152,7 @@ func (a *Account) Update(headers AccountHeaders, opts *RequestOptions) error { // // A successful PUT request implies Invalidate() since it may change metadata. func (a *Account) Create(opts *RequestOptions) error { - _, err := Request{ + resp, err := Request{ Method: "PUT", Options: opts, ExpectStatusCodes: []int{201, 202}, @@ -157,6 +160,7 @@ func (a *Account) Create(opts *RequestOptions) error { }.Do(a.backend) if err == nil { a.Invalidate() + resp.Body.Close() } return err } @@ -214,7 +218,7 @@ func (a *Account) Capabilities() (Capabilities, error) { func (a *Account) RawCapabilities() ([]byte, error) { //This method is the only one in Schwift that bypasses struct Request since //the request URL is not below the endpoint URL. - req, err := http.NewRequest(http.MethodGet, a.baseURL+"info", http.NoBody) + req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, a.baseURL+"info", http.NoBody) if err != nil { return nil, err } @@ -88,7 +88,7 @@ func (a *Account) BulkUpload(uploadPath string, format BulkUploadFormat, content req.ObjectName = fields[1] } - resp, err := req.Do(a.backend) + resp, err := req.Do(a.backend) //nolint:bodyclose // parseBulkResponse does the close if err != nil { return 0, err } @@ -265,7 +265,7 @@ func (a *Account) bulkDelete(names []string, opts *RequestOptions) (numDeleted, req.Options.Headers.Set("Accept", "application/json") req.Options.Headers.Set("Content-Type", "text/plain") req.Options.Values.Set("bulk-delete", "true") - resp, err := req.Do(a.backend) + resp, err := req.Do(a.backend) //nolint:bodyclose // parseBulkResponse does the close if err != nil { return 0, 0, err } diff --git a/container.go b/container.go index 476a704..a1692d6 100644 --- a/container.go +++ b/container.go @@ -90,6 +90,7 @@ func (c *Container) Headers() (ContainerHeaders, error) { if err != nil { return ContainerHeaders{}, err } + defer resp.Body.Close() headers := ContainerHeaders{headersFromHTTP(resp.Header)} err = headers.Validate() @@ -107,7 +108,7 @@ func (c *Container) Headers() (ContainerHeaders, error) { // // A successful POST request implies Invalidate() since it may change metadata. func (c *Container) Update(headers ContainerHeaders, opts *RequestOptions) error { - _, err := Request{ + resp, err := Request{ Method: "POST", ContainerName: c.name, Options: cloneRequestOptions(opts, headers.Headers), @@ -115,6 +116,7 @@ func (c *Container) Update(headers ContainerHeaders, opts *RequestOptions) error }.Do(c.a.backend) if err == nil { c.Invalidate() + resp.Body.Close() } return err } @@ -126,7 +128,7 @@ func (c *Container) Update(headers ContainerHeaders, opts *RequestOptions) error // // A successful PUT request implies Invalidate() since it may change metadata. func (c *Container) Create(opts *RequestOptions) error { - _, err := Request{ + resp, err := Request{ Method: "PUT", ContainerName: c.name, Options: opts, @@ -135,6 +137,7 @@ func (c *Container) Create(opts *RequestOptions) error { }.Do(c.a.backend) if err == nil { c.Invalidate() + resp.Body.Close() } return err } @@ -148,7 +151,7 @@ func (c *Container) Create(opts *RequestOptions) error { // // A successful DELETE request implies Invalidate(). func (c *Container) Delete(opts *RequestOptions) error { - _, err := Request{ + resp, err := Request{ Method: "DELETE", ContainerName: c.name, Options: opts, @@ -156,6 +159,7 @@ func (c *Container) Delete(opts *RequestOptions) error { }.Do(c.a.backend) if err == nil { c.Invalidate() + resp.Body.Close() } return err } @@ -177,12 +181,15 @@ func (c *Container) Invalidate() { // // container, err := account.Container("documents").EnsureExists() func (c *Container) EnsureExists() (*Container, error) { - _, err := Request{ + resp, err := Request{ Method: "PUT", ContainerName: c.name, ExpectStatusCodes: []int{201, 202}, DrainResponseBody: true, }.Do(c.a.backend) + if err == nil { + resp.Body.Close() + } return c, err } |
