Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable the goimports and usestdlibvars linters in golangci-lint and solve issues (STACKED ON #3467) #3471

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions changelog/unreleased/enhancement-goimports.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion cmd/reva/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/reva/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/http/interceptors/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 7 additions & 5 deletions internal/http/interceptors/cors/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package cors

import (
"net/http"

"github.com/cs3org/reva/pkg/rhttp/global"
"github.com/mitchellh/mapstructure"
"github.com/rs/cors"
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions internal/http/services/datagateway/datagateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/http/services/owncloud/ocdav/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions internal/http/services/owncloud/ocdav/tpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/http/services/owncloud/ocs/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion pkg/app/provider/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -42,7 +43,7 @@ func (p *demoProvider) GetAppURL(ctx context.Context, resource *provider.Resourc
url := fmt.Sprintf("<iframe src=%s/open/%s?view-mode=%s&access-token=%s&lang=%s />", p.iframeUIProvider, resource.Id.StorageId+":"+resource.Id.OpaqueId, viewMode.String(), token, language)
return &appprovider.OpenInAppURL{
AppUrl: url,
Method: "GET",
Method: http.MethodGet,
}, nil
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/app/provider/wopi/wopi.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (p *wopiProvider) GetAppURL(ctx context.Context, resource *provider.Resourc
}
wopiurl.Path = path.Join(wopiurl.Path, "/wopi/iop/openinapp")

httpReq, err := rhttp.NewRequest(ctx, "GET", wopiurl.String(), nil)
httpReq, err := rhttp.NewRequest(ctx, http.MethodGet, wopiurl.String(), nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -247,14 +247,14 @@ func (p *wopiProvider) GetAppURL(ctx context.Context, resource *provider.Resourc
// Depending on whether the WOPI server returned any form parameters or not,
// we decide whether the request method is POST or GET
var formParams map[string]string
method := "GET"
method := http.MethodGet
if form, ok := result["form-parameters"].(map[string]interface{}); ok {
if tkn, ok := form["access_token"].(string); ok {
formParams = map[string]string{
"access_token": tkn,
"access_token_ttl": tokenTTL,
}
method = "POST"
method = http.MethodPost
}
}

Expand Down Expand Up @@ -302,7 +302,7 @@ func getAppURLs(c *config) (map[string]map[string]string, error) {
}
appurl.Path = path.Join(appurl.Path, "/hosting/discovery")

discReq, err := http.NewRequest("GET", appurl.String(), nil)
discReq, err := http.NewRequest(http.MethodGet, appurl.String(), nil)
if err != nil {
return nil, err
}
Expand All @@ -321,7 +321,7 @@ func getAppURLs(c *config) (map[string]map[string]string, error) {
}
} else if discRes.StatusCode == http.StatusNotFound {
// this may be a bridge-supported app
discReq, err = http.NewRequest("GET", c.AppIntURL, nil)
discReq, err = http.NewRequest(http.MethodGet, c.AppIntURL, nil)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cbox/utils/tokenmanagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (a *APITokenManager) getAPIToken(ctx context.Context) (string, time.Time, e
"audience": {a.conf.TargetAPI},
}

httpReq, err := http.NewRequest("POST", a.conf.OIDCTokenEndpoint, strings.NewReader(params.Encode()))
httpReq, err := http.NewRequest(http.MethodPost, a.conf.OIDCTokenEndpoint, strings.NewReader(params.Encode()))
if err != nil {
return "", time.Time{}, err
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func (a *APITokenManager) SendAPIGetRequest(ctx context.Context, url string, for
return nil, err
}

httpReq, err := http.NewRequest("GET", url, nil)
httpReq, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/datatx/manager/rclone/rclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (driver *rclone) startJob(ctx context.Context, transferID string, srcRemote
}
u.Path = path.Join(u.Path, transferFileMethod)
requestURL := u.String()
req, err := http.NewRequest("POST", requestURL, bytes.NewReader(data))
req, err := http.NewRequest(http.MethodPost, requestURL, bytes.NewReader(data))
if err != nil {
err = errors.Wrap(err, "rclone: error pulling transfer: error framing post request")
transfer.TransferStatus = datatx.Status_STATUS_TRANSFER_FAILED
Expand Down Expand Up @@ -474,7 +474,7 @@ func (driver *rclone) startJob(ctx context.Context, transferID string, srcRemote
u.Path = path.Join(u.Path, transferFileMethod)
requestURL := u.String()

req, err := http.NewRequest("POST", requestURL, bytes.NewReader(data))
req, err := http.NewRequest(http.MethodPost, requestURL, bytes.NewReader(data))
if err != nil {
logger.Error().Err(err).Msgf("rclone driver: error framing post request: %v", err)
transfer.TransferStatus = datatx.Status_STATUS_INVALID
Expand Down Expand Up @@ -653,7 +653,7 @@ func (driver *rclone) CancelTransfer(ctx context.Context, transferID string) (*d
u.Path = path.Join(u.Path, transferFileMethod)
requestURL := u.String()

req, err := http.NewRequest("POST", requestURL, bytes.NewReader(data))
req, err := http.NewRequest(http.MethodPost, requestURL, bytes.NewReader(data))
if err != nil {
err = errors.Wrap(err, "rclone driver: error framing post request")
return &datatx.TxInfo{
Expand Down Expand Up @@ -781,7 +781,7 @@ func (driver *rclone) remotePathIsFolder(remote string, remotePath string, remot
u.Path = path.Join(u.Path, listMethod)
requestURL := u.String()

req, err := http.NewRequest("POST", requestURL, bytes.NewReader(data))
req, err := http.NewRequest(http.MethodPost, requestURL, bytes.NewReader(data))
if err != nil {
return false, errors.Wrap(err, "rclone driver: error framing post request")
}
Expand Down
19 changes: 9 additions & 10 deletions pkg/eosclient/eosgrpc/eoshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,15 @@ func (c *EOSHTTPClient) getRespError(rsp *http.Response, err error) error {
}

switch rsp.StatusCode {
case 0, 200, 201:
case 0, http.StatusOK, http.StatusCreated:
return nil
case 403:
case http.StatusForbidden:
return errtypes.PermissionDenied(rspdesc(rsp))
case 404:
case http.StatusNotFound:
return errtypes.NotFound(rspdesc(rsp))
}

err2 := errtypes.InternalError("Err from EOS: " + rspdesc(rsp))
return err2
return errtypes.InternalError("Err from EOS: " + rspdesc(rsp))
}

// From the basepath and the file path... build an url
Expand Down Expand Up @@ -262,7 +261,7 @@ func (c *EOSHTTPClient) GETFile(ctx context.Context, remoteuser string, auth eos
log.Error().Str("func", "GETFile").Str("url", finalurl).Str("err", err.Error()).Msg("can't create request")
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "GET", finalurl, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, finalurl, nil)
if err != nil {
log.Error().Str("func", "GETFile").Str("url", finalurl).Str("err", err.Error()).Msg("can't create request")
return nil, err
Expand Down Expand Up @@ -298,7 +297,7 @@ func (c *EOSHTTPClient) GETFile(ctx context.Context, remoteuser string, auth eos
return nil, err
}

req, err = http.NewRequestWithContext(ctx, "GET", loc.String(), nil)
req, err = http.NewRequestWithContext(ctx, http.MethodGet, loc.String(), nil)
if err != nil {
log.Error().Str("func", "GETFile").Str("url", loc.String()).Str("err", err.Error()).Msg("can't create redirected request")
return nil, err
Expand Down Expand Up @@ -354,7 +353,7 @@ func (c *EOSHTTPClient) PUTFile(ctx context.Context, remoteuser string, auth eos
log.Error().Str("func", "PUTFile").Str("url", finalurl).Str("err", err.Error()).Msg("can't create request")
return err
}
req, err := http.NewRequestWithContext(ctx, "PUT", finalurl, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, finalurl, nil)
if err != nil {
log.Error().Str("func", "PUTFile").Str("url", finalurl).Str("err", err.Error()).Msg("can't create request")
return err
Expand Down Expand Up @@ -392,7 +391,7 @@ func (c *EOSHTTPClient) PUTFile(ctx context.Context, remoteuser string, auth eos
return err
}

req, err = http.NewRequestWithContext(ctx, "PUT", loc.String(), stream)
req, err = http.NewRequestWithContext(ctx, http.MethodPut, loc.String(), stream)
if err != nil {
log.Error().Str("func", "PUTFile").Str("url", loc.String()).Str("err", err.Error()).Msg("can't create redirected request")
return err
Expand Down Expand Up @@ -454,7 +453,7 @@ func (c *EOSHTTPClient) Head(ctx context.Context, remoteuser string, auth eoscli
return err
}

req, err := http.NewRequestWithContext(ctx, "HEAD", finalurl, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodHead, finalurl, nil)
if err != nil {
log.Error().Str("func", "Head").Str("remoteuser", remoteuser).Str("uid,gid", auth.Role.UID+","+auth.Role.GID).Str("url", finalurl).Str("err", err.Error()).Msg("can't create request")
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/driver/xcloud/xcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (d *CloudDriver) refresh() error {
// endpoint example: https://mybox.com or https://mybox.com/owncloud
endpoint := fmt.Sprintf("%s/index.php/apps/sciencemesh/internal_metrics", d.instance)

req, err := http.NewRequest("GET", endpoint, nil)
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
log.Err(err).Msgf("xcloud: error creating request to %s", d.instance)
return err
Expand Down
Loading