From eb9092494e9c744009fd6589d5a4f482836ed662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Milo=C5=A1evi=C4=87?= Date: Tue, 21 Apr 2020 17:05:19 +0200 Subject: [PATCH] MF-1128 - Add golangci-linter to a CI script (#1131) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add golangci-linter to CI script Signed-off-by: Ivan Milošević * add no-config flag Signed-off-by: Ivan Milošević * disable staticcheck Signed-off-by: Ivan Milošević * disable all except errcheck Signed-off-by: Ivan Milošević * add more linters Signed-off-by: Ivan Milošević * add verbose flag for debug decrease concurrency to 1 Signed-off-by: Ivan Milošević * remove errcheck and verbose flag Signed-off-by: Ivan Milošević * enable just golint Signed-off-by: Ivan Milošević * fix lint errors Signed-off-by: Ivan Milošević --- bootstrap/api/endpoint_test.go | 100 ++++++++++++++++----------------- scripts/ci.sh | 8 +++ sdk/go/channels_test.go | 2 +- sdk/go/things_test.go | 12 ++-- twins/api/logging.go | 2 +- twins/api/metrics.go | 2 +- twins/doc.go | 1 - twins/mocks/authn.go | 2 +- twins/mocks/twins.go | 6 +- twins/mongodb/twins.go | 20 +++---- twins/service.go | 6 +- twins/twins.go | 6 +- 12 files changed, 87 insertions(+), 80 deletions(-) diff --git a/bootstrap/api/endpoint_test.go b/bootstrap/api/endpoint_test.go index 20503bc0..b61d7031 100644 --- a/bootstrap/api/endpoint_test.go +++ b/bootstrap/api/endpoint_test.go @@ -1169,68 +1169,68 @@ func TestBootstrap(t *testing.T) { data := toJSON(s) cases := []struct { - desc string - external_id string - external_key string - status int - res string - secure bool + desc string + externalID string + externalKey string + status int + res string + secure bool }{ { - desc: "bootstrap a Thing with unknown ID", - external_id: unknown, - external_key: c.ExternalKey, - status: http.StatusNotFound, - res: notFoundRes, - secure: false, + desc: "bootstrap a Thing with unknown ID", + externalID: unknown, + externalKey: c.ExternalKey, + status: http.StatusNotFound, + res: notFoundRes, + secure: false, }, { - desc: "bootstrap a Thing with an empty ID", - external_id: "", - external_key: c.ExternalKey, - status: http.StatusBadRequest, - res: malformedRes, - secure: false, + desc: "bootstrap a Thing with an empty ID", + externalID: "", + externalKey: c.ExternalKey, + status: http.StatusBadRequest, + res: malformedRes, + secure: false, }, { - desc: "bootstrap a Thing with unknown key", - external_id: c.ExternalID, - external_key: unknown, - status: http.StatusNotFound, - res: extKeyNotFoundRes, - secure: false, + desc: "bootstrap a Thing with unknown key", + externalID: c.ExternalID, + externalKey: unknown, + status: http.StatusNotFound, + res: extKeyNotFoundRes, + secure: false, }, { - desc: "bootstrap a Thing with an empty key", - external_id: c.ExternalID, - external_key: "", - status: http.StatusForbidden, - res: unauthRes, - secure: false, + desc: "bootstrap a Thing with an empty key", + externalID: c.ExternalID, + externalKey: "", + status: http.StatusForbidden, + res: unauthRes, + secure: false, }, { - desc: "bootstrap known Thing", - external_id: c.ExternalID, - external_key: c.ExternalKey, - status: http.StatusOK, - res: data, - secure: false, + desc: "bootstrap known Thing", + externalID: c.ExternalID, + externalKey: c.ExternalKey, + status: http.StatusOK, + res: data, + secure: false, }, { - desc: "bootstrap secure", - external_id: fmt.Sprintf("secure/%s", c.ExternalID), - external_key: hex.EncodeToString(encExternKey), - status: http.StatusOK, - res: data, - secure: true, + desc: "bootstrap secure", + externalID: fmt.Sprintf("secure/%s", c.ExternalID), + externalKey: hex.EncodeToString(encExternKey), + status: http.StatusOK, + res: data, + secure: true, }, { - desc: "bootstrap secure with unencrypted key", - external_id: fmt.Sprintf("secure/%s", c.ExternalID), - external_key: c.ExternalKey, - status: http.StatusNotFound, - res: extSecKeyNotFoundRes, - secure: true, + desc: "bootstrap secure with unencrypted key", + externalID: fmt.Sprintf("secure/%s", c.ExternalID), + externalKey: c.ExternalKey, + status: http.StatusNotFound, + res: extSecKeyNotFoundRes, + secure: true, }, } @@ -1238,8 +1238,8 @@ func TestBootstrap(t *testing.T) { req := testRequest{ client: bs.Client(), method: http.MethodGet, - url: fmt.Sprintf("%s/things/bootstrap/%s", bs.URL, tc.external_id), - token: tc.external_key, + url: fmt.Sprintf("%s/things/bootstrap/%s", bs.URL, tc.externalID), + token: tc.externalKey, } res, err := req.make() assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err)) diff --git a/scripts/ci.sh b/scripts/ci.sh index dc90676e..8da61193 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -72,14 +72,22 @@ setup_mf() { make -j$NPROC } +setup_lint() { + # binary will be $(go env GOPATH)/bin/golangci-lint + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0 +} + setup() { echo "Setting up..." update_go setup_protoc setup_mf + setup_lint } run_test() { + echo "Running lint..." + golangci-lint run --concurrency=1 --no-config --disable-all --enable=golint echo "Running tests..." echo "" > coverage.txt for d in $(go list ./... | grep -v 'vendor\|cmd'); do diff --git a/sdk/go/channels_test.go b/sdk/go/channels_test.go index 19626e2d..74b3b0ee 100644 --- a/sdk/go/channels_test.go +++ b/sdk/go/channels_test.go @@ -141,7 +141,7 @@ func TestCreateChannels(t *testing.T) { res, err := mainfluxSDK.CreateChannels(tc.channels, tc.token) assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err)) - for idx, _ := range tc.res { + for idx := range tc.res { assert.Equal(t, tc.res[idx].ID, res[idx].ID, fmt.Sprintf("%s: expected response ID %s got %s", tc.desc, tc.res[idx].ID, res[idx].ID)) } } diff --git a/sdk/go/things_test.go b/sdk/go/things_test.go index 489b67d0..1fc22670 100644 --- a/sdk/go/things_test.go +++ b/sdk/go/things_test.go @@ -178,7 +178,7 @@ func TestCreateThings(t *testing.T) { res, err := mainfluxSDK.CreateThings(tc.things, tc.token) assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err)) - for idx, _ := range tc.res { + for idx := range tc.res { assert.Equal(t, tc.res[idx].ID, res[idx].ID, fmt.Sprintf("%s: expected response ID %s got %s", tc.desc, tc.res[idx].ID, res[idx].ID)) } } @@ -205,28 +205,28 @@ func TestThing(t *testing.T) { cases := []struct { desc string - thId string + thID string token string err error response sdk.Thing }{ { desc: "get existing thing", - thId: id, + thID: id, token: token, err: nil, response: thing, }, { desc: "get non-existent thing", - thId: "43", + thID: "43", token: token, err: createError(sdk.ErrFailedFetch, http.StatusNotFound), response: sdk.Thing{}, }, { desc: "get thing with invalid token", - thId: id, + thID: id, token: wrongValue, err: createError(sdk.ErrFailedFetch, http.StatusForbidden), response: sdk.Thing{}, @@ -234,7 +234,7 @@ func TestThing(t *testing.T) { } for _, tc := range cases { - respTh, err := mainfluxSDK.Thing(tc.thId, tc.token) + respTh, err := mainfluxSDK.Thing(tc.thID, tc.token) assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err)) assert.Equal(t, tc.response, respTh, fmt.Sprintf("%s: expected response thing %s, got %s", tc.desc, tc.response, respTh)) diff --git a/twins/api/logging.go b/twins/api/logging.go index 62c328d9..182f7caa 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -66,7 +66,7 @@ func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, id string) (vi return lm.svc.ViewTwin(ctx, token, id) } -func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset uint64, limit uint64, name string, metadata twins.Metadata) (tw twins.TwinsPage, err error) { +func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset uint64, limit uint64, name string, metadata twins.Metadata) (tw twins.Page, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method list_twins for token %s took %s to complete", token, time.Since(begin)) if err != nil { diff --git a/twins/api/metrics.go b/twins/api/metrics.go index a3bbe42f..a70e9c92 100644 --- a/twins/api/metrics.go +++ b/twins/api/metrics.go @@ -59,7 +59,7 @@ func (ms *metricsMiddleware) ViewTwin(ctx context.Context, token, id string) (vi return ms.svc.ViewTwin(ctx, token, id) } -func (ms *metricsMiddleware) ListTwins(ctx context.Context, token string, offset uint64, limit uint64, name string, metadata twins.Metadata) (tw twins.TwinsPage, err error) { +func (ms *metricsMiddleware) ListTwins(ctx context.Context, token string, offset uint64, limit uint64, name string, metadata twins.Metadata) (tw twins.Page, err error) { defer func(begin time.Time) { ms.counter.With("method", "list_twins").Add(1) ms.latency.With("method", "list_twins").Observe(time.Since(begin).Seconds()) diff --git a/twins/doc.go b/twins/doc.go index 2466db8c..be5467a8 100644 --- a/twins/doc.go +++ b/twins/doc.go @@ -7,5 +7,4 @@ // It holds the sequence of attribute based definitions of a real world // thing and refers to the time series of definition based states that // hold the historical data about the represented real world thing. - package twins diff --git a/twins/mocks/authn.go b/twins/mocks/authn.go index 6cd1e77c..75324b02 100644 --- a/twins/mocks/authn.go +++ b/twins/mocks/authn.go @@ -29,6 +29,6 @@ func (svc authNServiceClient) Identify(ctx context.Context, in *mainflux.Token, return nil, users.ErrUnauthorizedAccess } -func (c *authNServiceClient) Issue(ctx context.Context, in *mainflux.IssueReq, opts ...grpc.CallOption) (*mainflux.Token, error) { +func (svc *authNServiceClient) Issue(ctx context.Context, in *mainflux.IssueReq, opts ...grpc.CallOption) (*mainflux.Token, error) { return new(mainflux.Token), nil } diff --git a/twins/mocks/twins.go b/twins/mocks/twins.go index 78b8732a..278bb1ba 100644 --- a/twins/mocks/twins.go +++ b/twins/mocks/twins.go @@ -100,14 +100,14 @@ func (trm *twinRepositoryMock) RetrieveByThing(_ context.Context, thingid string } -func (trm *twinRepositoryMock) RetrieveAll(_ context.Context, owner string, offset uint64, limit uint64, name string, metadata twins.Metadata) (twins.TwinsPage, error) { +func (trm *twinRepositoryMock) RetrieveAll(_ context.Context, owner string, offset uint64, limit uint64, name string, metadata twins.Metadata) (twins.Page, error) { trm.mu.Lock() defer trm.mu.Unlock() items := make([]twins.Twin, 0) if limit <= 0 { - return twins.TwinsPage{}, nil + return twins.Page{}, nil } // This obscure way to examine map keys is enforced by the key structure in mocks/commons.go @@ -130,7 +130,7 @@ func (trm *twinRepositoryMock) RetrieveAll(_ context.Context, owner string, offs return items[i].ID < items[j].ID }) - page := twins.TwinsPage{ + page := twins.Page{ Twins: items, PageMetadata: twins.PageMetadata{ Total: trm.counter, diff --git a/twins/mongodb/twins.go b/twins/mongodb/twins.go index 299118cd..4d86963b 100644 --- a/twins/mongodb/twins.go +++ b/twins/mongodb/twins.go @@ -139,7 +139,7 @@ func (tr *twinRepository) RetrieveByAttribute(ctx context.Context, channel, subt return ids, nil } -func (tr *twinRepository) RetrieveAll(ctx context.Context, owner string, offset uint64, limit uint64, name string, metadata twins.Metadata) (twins.TwinsPage, error) { +func (tr *twinRepository) RetrieveAll(ctx context.Context, owner string, offset uint64, limit uint64, name string, metadata twins.Metadata) (twins.Page, error) { coll := tr.db.Collection(twinsCollection) findOptions := options.Find() @@ -159,20 +159,20 @@ func (tr *twinRepository) RetrieveAll(ctx context.Context, owner string, offset } cur, err := coll.Find(ctx, filter, findOptions) if err != nil { - return twins.TwinsPage{}, err + return twins.Page{}, err } results, err := decodeTwins(ctx, cur) if err != nil { - return twins.TwinsPage{}, err + return twins.Page{}, err } total, err := coll.CountDocuments(ctx, filter) if err != nil { - return twins.TwinsPage{}, err + return twins.Page{}, err } - return twins.TwinsPage{ + return twins.Page{ Twins: results, PageMetadata: twins.PageMetadata{ Total: uint64(total), @@ -182,7 +182,7 @@ func (tr *twinRepository) RetrieveAll(ctx context.Context, owner string, offset }, nil } -func (tr *twinRepository) RetrieveAllByThing(ctx context.Context, thingid string, offset uint64, limit uint64) (twins.TwinsPage, error) { +func (tr *twinRepository) RetrieveAllByThing(ctx context.Context, thingid string, offset uint64, limit uint64) (twins.Page, error) { coll := tr.db.Collection(twinsCollection) findOptions := options.Find() @@ -192,20 +192,20 @@ func (tr *twinRepository) RetrieveAllByThing(ctx context.Context, thingid string filter := bson.D{{"thingid", thingid}} cur, err := coll.Find(ctx, filter, findOptions) if err != nil { - return twins.TwinsPage{}, err + return twins.Page{}, err } results, err := decodeTwins(ctx, cur) if err != nil { - return twins.TwinsPage{}, err + return twins.Page{}, err } total, err := coll.CountDocuments(ctx, filter) if err != nil { - return twins.TwinsPage{}, err + return twins.Page{}, err } - return twins.TwinsPage{ + return twins.Page{ Twins: results, PageMetadata: twins.PageMetadata{ Total: uint64(total), diff --git a/twins/service.go b/twins/service.go index 72313215..66848f74 100644 --- a/twins/service.go +++ b/twins/service.go @@ -63,7 +63,7 @@ type Service interface { // ListTwins retrieves data about subset of twins that belongs to the // user identified by the provided key. - ListTwins(ctx context.Context, token string, offset uint64, limit uint64, name string, metadata Metadata) (TwinsPage, error) + ListTwins(ctx context.Context, token string, offset uint64, limit uint64, name string, metadata Metadata) (Page, error) // ListStates retrieves data about subset of states that belongs to the // twin identified by the id. @@ -261,10 +261,10 @@ func (ts *twinsService) RemoveTwin(ctx context.Context, token, id string) (err e return nil } -func (ts *twinsService) ListTwins(ctx context.Context, token string, offset uint64, limit uint64, name string, metadata Metadata) (TwinsPage, error) { +func (ts *twinsService) ListTwins(ctx context.Context, token string, offset uint64, limit uint64, name string, metadata Metadata) (Page, error) { res, err := ts.auth.Identify(ctx, &mainflux.Token{Value: token}) if err != nil { - return TwinsPage{}, ErrUnauthorizedAccess + return Page{}, ErrUnauthorizedAccess } return ts.twins.RetrieveAll(ctx, res.GetValue(), offset, limit, name, metadata) diff --git a/twins/twins.go b/twins/twins.go index 64871154..79d1072b 100644 --- a/twins/twins.go +++ b/twins/twins.go @@ -49,9 +49,9 @@ type PageMetadata struct { Name string } -// TwinsPage contains page related metadata as well as a list of twins that +// Page contains page related metadata as well as a list of twins that // belong to this page. -type TwinsPage struct { +type Page struct { PageMetadata Twins []Twin } @@ -73,7 +73,7 @@ type TwinRepository interface { RetrieveByAttribute(ctx context.Context, channel, subtopic string) ([]string, error) // RetrieveAll retrieves the subset of things owned by the specified user. - RetrieveAll(context.Context, string, uint64, uint64, string, Metadata) (TwinsPage, error) + RetrieveAll(context.Context, string, uint64, uint64, string, Metadata) (Page, error) // RetrieveByThing retrieves twin that represents specified thing RetrieveByThing(context.Context, string) (Twin, error)