From 2b53a932bfdede083db45017be73f8369b28fc24 Mon Sep 17 00:00:00 2001 From: Vasco Guita Date: Wed, 23 Nov 2022 14:17:20 +0100 Subject: [PATCH] Enable the goimports and usestdlibvars linters and solve issues --- .golangci.yaml | 2 -- changelog/unreleased/enhancement-goimports.md | 5 +++++ cmd/reva/download.go | 2 +- cmd/reva/upload.go | 2 +- internal/http/interceptors/auth/auth.go | 2 +- internal/http/interceptors/cors/cors.go | 12 +++++++----- .../providerauthorizer/providerauthorizer.go | 2 +- .../http/services/datagateway/datagateway.go | 16 ++++++++-------- internal/http/services/owncloud/ocdav/copy.go | 4 ++-- internal/http/services/owncloud/ocdav/tpc.go | 8 ++++---- internal/http/services/owncloud/ocs/cache.go | 2 +- pkg/app/provider/demo/demo.go | 3 ++- pkg/app/provider/wopi/wopi.go | 10 +++++----- pkg/cbox/utils/tokenmanagement.go | 4 ++-- pkg/datatx/manager/rclone/rclone.go | 8 ++++---- pkg/eosclient/eosgrpc/eoshttp.go | 19 +++++++++---------- pkg/metrics/driver/xcloud/xcloud.go | 2 +- pkg/ocm/invite/manager/json/json.go | 2 +- pkg/ocm/invite/manager/memory/memory.go | 2 +- pkg/ocm/provider/authorizer/mentix/mentix.go | 2 +- pkg/ocm/share/sender/sender.go | 2 +- pkg/rhttp/datatx/manager/simple/simple.go | 4 ++-- pkg/rhttp/datatx/manager/spaces/spaces.go | 4 ++-- pkg/rhttp/datatx/manager/tus/tus.go | 8 ++++---- pkg/rhttp/datatx/utils/download/download.go | 2 +- pkg/sdk/action/download.go | 3 ++- pkg/sdk/action/upload.go | 3 ++- pkg/sdk/common/net/tus.go | 2 +- pkg/siteacc/manager/gocdb/account.go | 2 +- pkg/storage/fs/cback/cback.go | 2 +- pkg/storage/fs/nextcloud/nextcloud.go | 4 ++-- 31 files changed, 76 insertions(+), 69 deletions(-) create mode 100644 changelog/unreleased/enhancement-goimports.md diff --git a/.golangci.yaml b/.golangci.yaml index 3c0ae8f5b2..6b077f56f2 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -8,12 +8,10 @@ linters: - wrapcheck # TODO: consider enabling the 'wrapcheck' linter to check that errors from external packages are wrapped during return to help identify the error source during debugging. - cyclop # TODO: consider enabling the 'cyclop' linter to calculate the cyclomatic complexities of functions/packages. - varnamelen # TODO: consider enabling the 'varnamelen' linter to check that the length of a variable's name matches its usage scope. - - goimports # TODO: consider enabling the 'goimports' linter to fix imports and format the code in the same style as gofmt. - testpackage # TODO: consider enabling the 'testpackage' linter to make sure that separate _test packages are used. - gosec # TODO: consider enabling the 'gosec' linter to inspect source code for security problems. - tagliatelle # TODO: consider enabling the 'tagliatelle' linter to check the struct tags. - stylecheck # TODO: consider enabling the 'stylecheck' linter to enforce style rules. - - usestdlibvars # TODO: consider enabling the 'usestdlibvars' linter to detect the possibility to use variables/constants from the Go standard library. - thelper # TODO: consider enabling the 'thelper' linter to detect golang test helpers without t.Helper() call and check the consistency of test helpers. - predeclared # TODO: consider enabling the 'predeclared' linter to find code that shadows one of Go's predeclared identifiers. - paralleltest # TODO: consider enabling the 'paralleltest' linter to detect missing usage of t.Parallel() method in Go test. diff --git a/changelog/unreleased/enhancement-goimports.md b/changelog/unreleased/enhancement-goimports.md new file mode 100644 index 0000000000..002a524f77 --- /dev/null +++ b/changelog/unreleased/enhancement-goimports.md @@ -0,0 +1,5 @@ +Enhancement: Enable goimports and usestdlibvars in golangci-lint + +We've enabled the goimports and usestdlibvars linters in golangci-lint and solved the related issues. + +https://github.com/cs3org/reva/pull/3471 \ No newline at end of file diff --git a/cmd/reva/download.go b/cmd/reva/download.go index 82da692842..716101a27f 100644 --- a/cmd/reva/download.go +++ b/cmd/reva/download.go @@ -95,7 +95,7 @@ func downloadCommand() *command { dataServerURL := p.DownloadEndpoint // TODO(labkode): do a protocol switch - httpReq, err := rhttp.NewRequest(ctx, "GET", dataServerURL, nil) + httpReq, err := rhttp.NewRequest(ctx, http.MethodGet, dataServerURL, nil) if err != nil { return err } diff --git a/cmd/reva/upload.go b/cmd/reva/upload.go index 9dd54df050..8fb567e538 100644 --- a/cmd/reva/upload.go +++ b/cmd/reva/upload.go @@ -148,7 +148,7 @@ func uploadCommand() *command { dataServerURL := p.UploadEndpoint if *protocolFlag == "simple" { - httpReq, err := rhttp.NewRequest(ctx, "PUT", dataServerURL, fd) + httpReq, err := rhttp.NewRequest(ctx, http.MethodPut, dataServerURL, fd) if err != nil { return err } diff --git a/internal/http/interceptors/auth/auth.go b/internal/http/interceptors/auth/auth.go index 3348e07947..cca7cc4e17 100644 --- a/internal/http/interceptors/auth/auth.go +++ b/internal/http/interceptors/auth/auth.go @@ -158,7 +158,7 @@ func New(m map[string]interface{}, unprotected []string) (global.Middleware, err // OPTION requests need to pass for preflight requests // TODO(labkode): this will break options for auth protected routes. // Maybe running the CORS middleware before auth kicks in is enough. - if r.Method == "OPTIONS" { + if r.Method == http.MethodOptions { h.ServeHTTP(w, r) return } diff --git a/internal/http/interceptors/cors/cors.go b/internal/http/interceptors/cors/cors.go index 1d7080ac09..0d978fb929 100644 --- a/internal/http/interceptors/cors/cors.go +++ b/internal/http/interceptors/cors/cors.go @@ -19,6 +19,8 @@ package cors import ( + "net/http" + "github.com/cs3org/reva/pkg/rhttp/global" "github.com/mitchellh/mapstructure" "github.com/rs/cors" @@ -62,11 +64,11 @@ func New(m map[string]interface{}) (global.Middleware, int, error) { if len(conf.AllowedMethods) == 0 { conf.AllowedMethods = []string{ - "OPTIONS", - "HEAD", - "GET", - "PUT", - "POST", + http.MethodOptions, + http.MethodHead, + http.MethodGet, + http.MethodPut, + http.MethodPost, "DELETE", "MKCOL", "PROPFIND", diff --git a/internal/http/interceptors/providerauthorizer/providerauthorizer.go b/internal/http/interceptors/providerauthorizer/providerauthorizer.go index 450614dcc8..76ac951f80 100644 --- a/internal/http/interceptors/providerauthorizer/providerauthorizer.go +++ b/internal/http/interceptors/providerauthorizer/providerauthorizer.go @@ -79,7 +79,7 @@ func New(m map[string]interface{}, unprotected []string, ocmPrefix string) (glob log := appctx.GetLogger(ctx) head, _ := router.ShiftPath(r.URL.Path) - if r.Method == "OPTIONS" || head != ocmPrefix || utils.Skip(r.URL.Path, unprotected) { + if r.Method == http.MethodOptions || head != ocmPrefix || utils.Skip(r.URL.Path, unprotected) { log.Info().Msg("skipping provider authorizer check for: " + r.URL.Path) h.ServeHTTP(w, r) return diff --git a/internal/http/services/datagateway/datagateway.go b/internal/http/services/datagateway/datagateway.go index dba255f6be..65d201e801 100644 --- a/internal/http/services/datagateway/datagateway.go +++ b/internal/http/services/datagateway/datagateway.go @@ -117,17 +117,17 @@ func (s *svc) Unprotected() []string { func (s *svc) setHandler() { s.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.Method { - case "HEAD": + case http.MethodHead: addCorsHeader(w) s.doHead(w, r) return - case "GET": + case http.MethodGet: s.doGet(w, r) return - case "PUT": + case http.MethodPut: s.doPut(w, r) return - case "PATCH": + case http.MethodPatch: s.doPatch(w, r) return default: @@ -183,7 +183,7 @@ func (s *svc) doHead(w http.ResponseWriter, r *http.Request) { log.Debug().Str("target", claims.Target).Msg("sending request to internal data server") httpClient := s.client - httpReq, err := rhttp.NewRequest(ctx, "HEAD", claims.Target, nil) + httpReq, err := rhttp.NewRequest(ctx, http.MethodHead, claims.Target, nil) if err != nil { log.Error().Err(err).Msg("wrong request") w.WriteHeader(http.StatusInternalServerError) @@ -229,7 +229,7 @@ func (s *svc) doGet(w http.ResponseWriter, r *http.Request) { log.Debug().Str("target", claims.Target).Msg("sending request to internal data server") httpClient := s.client - httpReq, err := rhttp.NewRequest(ctx, "GET", claims.Target, nil) + httpReq, err := rhttp.NewRequest(ctx, http.MethodGet, claims.Target, nil) if err != nil { log.Error().Err(err).Msg("wrong request") w.WriteHeader(http.StatusInternalServerError) @@ -300,7 +300,7 @@ func (s *svc) doPut(w http.ResponseWriter, r *http.Request) { log.Debug().Str("target", claims.Target).Msg("sending request to internal data server") httpClient := s.client - httpReq, err := rhttp.NewRequest(ctx, "PUT", target, r.Body) + httpReq, err := rhttp.NewRequest(ctx, http.MethodPut, target, r.Body) if err != nil { log.Err(err).Msg("wrong request") w.WriteHeader(http.StatusInternalServerError) @@ -359,7 +359,7 @@ func (s *svc) doPatch(w http.ResponseWriter, r *http.Request) { log.Debug().Str("target", claims.Target).Msg("sending request to internal data server") httpClient := s.client - httpReq, err := rhttp.NewRequest(ctx, "PATCH", target, r.Body) + httpReq, err := rhttp.NewRequest(ctx, http.MethodPatch, target, r.Body) if err != nil { log.Err(err).Msg("wrong request") w.WriteHeader(http.StatusInternalServerError) diff --git a/internal/http/services/owncloud/ocdav/copy.go b/internal/http/services/owncloud/ocdav/copy.go index 55a6ea6fc0..ac8caa70c0 100644 --- a/internal/http/services/owncloud/ocdav/copy.go +++ b/internal/http/services/owncloud/ocdav/copy.go @@ -237,7 +237,7 @@ func (s *svc) executePathCopy(ctx context.Context, client gateway.GatewayAPIClie // 3. do download - httpDownloadReq, err := rhttp.NewRequest(ctx, "GET", downloadEP, nil) + httpDownloadReq, err := rhttp.NewRequest(ctx, http.MethodGet, downloadEP, nil) if err != nil { return err } @@ -254,7 +254,7 @@ func (s *svc) executePathCopy(ctx context.Context, client gateway.GatewayAPIClie // 4. do upload - httpUploadReq, err := rhttp.NewRequest(ctx, "PUT", uploadEP, httpDownloadRes.Body) + httpUploadReq, err := rhttp.NewRequest(ctx, http.MethodPut, uploadEP, httpDownloadRes.Body) if err != nil { return err } diff --git a/internal/http/services/owncloud/ocdav/tpc.go b/internal/http/services/owncloud/ocdav/tpc.go index c31b6066a4..aa324064bf 100644 --- a/internal/http/services/owncloud/ocdav/tpc.go +++ b/internal/http/services/owncloud/ocdav/tpc.go @@ -178,7 +178,7 @@ func (s *svc) performHTTPPull(ctx context.Context, client gateway.GatewayAPIClie // get http client for remote httpClient := &http.Client{} - req, err := http.NewRequest("GET", src, nil) + req, err := http.NewRequest(http.MethodGet, src, nil) if err != nil { w.WriteHeader(http.StatusInternalServerError) return err @@ -240,7 +240,7 @@ func (s *svc) performHTTPPull(ctx context.Context, client gateway.GatewayAPIClie tempReader := io.TeeReader(httpDownloadRes.Body, &wc) // do Upload - httpUploadReq, err := rhttp.NewRequest(ctx, "PUT", uploadEP, tempReader) + httpUploadReq, err := rhttp.NewRequest(ctx, http.MethodPut, uploadEP, tempReader) if err != nil { w.WriteHeader(http.StatusInternalServerError) return err @@ -362,7 +362,7 @@ func (s *svc) performHTTPPush(ctx context.Context, client gateway.GatewayAPIClie } // do download - httpDownloadReq, err := rhttp.NewRequest(ctx, "GET", downloadEP, nil) + httpDownloadReq, err := rhttp.NewRequest(ctx, http.MethodGet, downloadEP, nil) if err != nil { w.WriteHeader(http.StatusInternalServerError) return err @@ -387,7 +387,7 @@ func (s *svc) performHTTPPush(ctx context.Context, client gateway.GatewayAPIClie // get http client for a remote call httpClient := &http.Client{} - req, err := http.NewRequest("PUT", dst, tempReader) + req, err := http.NewRequest(http.MethodPut, dst, tempReader) if err != nil { w.WriteHeader(http.StatusInternalServerError) return err diff --git a/internal/http/services/owncloud/ocs/cache.go b/internal/http/services/owncloud/ocs/cache.go index 8ab887d4c6..3b82135110 100644 --- a/internal/http/services/owncloud/ocs/cache.go +++ b/internal/http/services/owncloud/ocs/cache.go @@ -48,7 +48,7 @@ func (s *svc) cacheWarmup(w http.ResponseWriter, r *http.Request) { ctx = ctxpkg.ContextSetToken(ctx, tkn) ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, tkn) - req, _ := http.NewRequest("GET", "", nil) + req, _ := http.NewRequest(http.MethodGet, "", nil) req = req.WithContext(ctx) req.URL = r.URL diff --git a/pkg/app/provider/demo/demo.go b/pkg/app/provider/demo/demo.go index 9376251aa1..ce36738188 100644 --- a/pkg/app/provider/demo/demo.go +++ b/pkg/app/provider/demo/demo.go @@ -21,6 +21,7 @@ package demo import ( "context" "fmt" + "net/http" appprovider "github.com/cs3org/go-cs3apis/cs3/app/provider/v1beta1" appregistry "github.com/cs3org/go-cs3apis/cs3/app/registry/v1beta1" @@ -42,7 +43,7 @@ func (p *demoProvider) GetAppURL(ctx context.Context, resource *provider.Resourc url := fmt.Sprintf("