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("