Skip to content

Commit

Permalink
Merge pull request ThreeDotsLabs#46 from ThreeDotsLabs/added-linters
Browse files Browse the repository at this point in the history
Added linters
  • Loading branch information
roblaszczak authored Dec 1, 2021
2 parents ca092e9 + 74a0270 commit 33e3c49
Show file tree
Hide file tree
Showing 20 changed files with 251 additions and 59 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ proto:

.PHONY: lint
lint:
@go-cleanarch
@./scripts/lint.sh common
@./scripts/lint.sh trainer
@./scripts/lint.sh trainings
@./scripts/lint.sh users
Expand Down
17 changes: 13 additions & 4 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
steps:
- id: common-lint
name: golang
entrypoint: ./scripts/lint.sh
args: ['common', '-install']
- id: trainer-lint
name: golang
entrypoint: ./scripts/lint.sh
args: [trainer]
args: ['trainer', '-install']
- id: trainings-lint
name: golang
entrypoint: ./scripts/lint.sh
args: [trainings]
args: ['trainings', '-install']
- id: users-lint
name: golang
entrypoint: ./scripts/lint.sh
args: [users]
args: ['users', '-install']

- id: trainer-docker
name: gcr.io/cloud-builders/docker
Expand All @@ -35,6 +39,11 @@ steps:
- 'PROJECT_ID=$PROJECT_ID'
waitFor: [trainer-docker, trainings-docker, users-docker]

- id: common-tests
name: golang
entrypoint: ./scripts/test.sh
args: ["common", ".test.ci.env"]
waitFor: [docker-compose]
- id: trainer-tests
name: golang
entrypoint: ./scripts/test.sh
Expand Down Expand Up @@ -96,7 +105,7 @@ steps:
dir: web
waitFor: ['-']
- id: openapi-js
name: openapitools/openapi-generator-cli:v4.3.0
name: openapitools/openapi-generator-cli:v4.3.0
entrypoint: "./scripts/openapi-js.sh"
waitFor: ['-']
- id: web-build
Expand Down
47 changes: 47 additions & 0 deletions internal/common/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
linters:
disable-all: true
enable:
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
- asciicheck
- bodyclose
- dogsled
- exhaustive
- exportloopref
- gocognit
- goconst
- gofmt
- goheader
- goimports
- gosec
- misspell
- nakedret
- nestif
- noctx
- rowserrcheck
- sqlclosecheck
- unconvert
- unparam
- whitespace

issues:
exclude:
- "composite literal uses unkeyed fields"
exclude-rules:
- path: _test\.go
linters:
- gosec
- noctx
- unparam
- bodyclose
- path: fixtures.go
linters:
- gosec
3 changes: 2 additions & 1 deletion internal/common/client/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func grpcDialOpts(grpcAddr string) ([]grpc.DialOption, error) {
return nil, errors.Wrap(err, "cannot load root CA cert")
}
creds := credentials.NewTLS(&tls.Config{
RootCAs: systemRoots,
RootCAs: systemRoots,
MinVersion: tls.VersionTLS12,
})

return []grpc.DialOption{
Expand Down
2 changes: 1 addition & 1 deletion internal/common/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func RunHTTPServerOnAddr(addr string, createHandler func(router chi.Router) http

logrus.Info("Starting HTTP server")

http.ListenAndServe(addr, rootRouter)
_ = http.ListenAndServe(addr, rootRouter)
}

func setMiddlewares(router *chi.Mux) {
Expand Down
8 changes: 8 additions & 0 deletions internal/common/tests/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func (c TrainerHTTPClient) MakeHourAvailable(t *testing.T, hour time.Time) int {
Hours: []time.Time{hour},
})
require.NoError(t, err)
require.NoError(t, response.Body.Close())

return response.StatusCode
}

Expand All @@ -57,6 +59,8 @@ func (c TrainerHTTPClient) MakeHourUnavailable(t *testing.T, hour time.Time) {
Hours: []time.Time{hour},
})
require.NoError(t, err)
require.NoError(t, response.Body.Close())

require.Equal(t, http.StatusNoContent, response.StatusCode)
}

Expand Down Expand Up @@ -113,6 +117,8 @@ func (c TrainingsHTTPClient) CreateTrainingShouldFail(t *testing.T, note string,
Time: hour,
})
require.NoError(t, err)
require.NoError(t, response.Body.Close())

require.Equal(t, http.StatusInternalServerError, response.StatusCode)
}

Expand All @@ -127,6 +133,8 @@ func (c TrainingsHTTPClient) GetTrainings(t *testing.T) trainings.Trainings {
func (c TrainingsHTTPClient) CancelTraining(t *testing.T, trainingUUID string, expectedStatusCode int) {
response, err := c.client.CancelTraining(context.Background(), trainingUUID)
require.NoError(t, err)
require.NoError(t, response.Body.Close())

require.Equal(t, expectedStatusCode, response.StatusCode)
}

Expand Down
47 changes: 47 additions & 0 deletions internal/trainer/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
linters:
disable-all: true
enable:
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
- asciicheck
- bodyclose
- dogsled
- exhaustive
- exportloopref
- gocognit
- goconst
- gofmt
- goheader
- goimports
- gosec
- misspell
- nakedret
- nestif
- noctx
- rowserrcheck
- sqlclosecheck
- unconvert
- unparam
- whitespace

issues:
exclude:
- "composite literal uses unkeyed fields"
exclude-rules:
- path: _test\.go
linters:
- gosec
- noctx
- unparam
- bodyclose
- path: fixtures.go
linters:
- gosec
3 changes: 2 additions & 1 deletion internal/trainer/adapters/dates_firestore_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainer/app/query"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainer/domain/hour"
"github.com/pkg/errors"

"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (d DatesFirestoreRepository) AvailableHours(ctx context.Context, from time.

for {
doc, err := iter.Next()
if err == iterator.Done {
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/trainer/adapters/hour_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func testUpdateHour_rollback(t *testing.T, repository hour.Repository) {
require.NoError(t, h.MakeAvailable())
return h, nil
})
require.Error(t, err)

err = repository.UpdateHour(ctx, hourTime, func(h *hour.Hour) (*hour.Hour, error) {
assert.True(t, h.IsAvailable())
Expand Down
4 changes: 2 additions & 2 deletions internal/trainer/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func loadFixtures(app app.Application) {
return
}

logrus.WithField("after", time.Now().Sub(start)).Debug("Trainer service is available")
logrus.WithField("after", time.Since(start)).Debug("Trainer service is available")

if !canLoadFixtures(app, ctx) {
logrus.Debug("Trainer fixtures are already loaded")
Expand All @@ -42,7 +42,7 @@ func loadFixtures(app app.Application) {
time.Sleep(10 * time.Second)
}

logrus.WithField("after", time.Now().Sub(start)).Debug("Trainer fixtures loaded")
logrus.WithField("after", time.Since(start)).Debug("Trainer fixtures loaded")
}

func loadTrainerFixtures(ctx context.Context, application app.Application) error {
Expand Down
33 changes: 6 additions & 27 deletions internal/trainer/ports/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package ports

import (
"context"
"errors"
"time"

"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/genproto/trainer"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainer/app"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/empty"
"github.com/golang/protobuf/ptypes/timestamp"
"google.golang.org/grpc/codes"
Expand All @@ -23,10 +21,7 @@ func NewGrpcServer(application app.Application) GrpcServer {
}

func (g GrpcServer) MakeHourAvailable(ctx context.Context, request *trainer.UpdateHourRequest) (*empty.Empty, error) {
trainingTime, err := protoTimestampToTime(request.Time)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "unable to parse time")
}
trainingTime := protoTimestampToTime(request.Time)

if err := g.app.Commands.MakeHoursAvailable.Handle(ctx, []time.Time{trainingTime}); err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand All @@ -36,10 +31,7 @@ func (g GrpcServer) MakeHourAvailable(ctx context.Context, request *trainer.Upda
}

func (g GrpcServer) ScheduleTraining(ctx context.Context, request *trainer.UpdateHourRequest) (*empty.Empty, error) {
trainingTime, err := protoTimestampToTime(request.Time)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "unable to parse time")
}
trainingTime := protoTimestampToTime(request.Time)

if err := g.app.Commands.ScheduleTraining.Handle(ctx, trainingTime); err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand All @@ -49,10 +41,7 @@ func (g GrpcServer) ScheduleTraining(ctx context.Context, request *trainer.Updat
}

func (g GrpcServer) CancelTraining(ctx context.Context, request *trainer.UpdateHourRequest) (*empty.Empty, error) {
trainingTime, err := protoTimestampToTime(request.Time)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "unable to parse time")
}
trainingTime := protoTimestampToTime(request.Time)

if err := g.app.Commands.CancelTraining.Handle(ctx, trainingTime); err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand All @@ -62,10 +51,7 @@ func (g GrpcServer) CancelTraining(ctx context.Context, request *trainer.UpdateH
}

func (g GrpcServer) IsHourAvailable(ctx context.Context, request *trainer.IsHourAvailableRequest) (*trainer.IsHourAvailableResponse, error) {
trainingTime, err := protoTimestampToTime(request.Time)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "unable to parse time")
}
trainingTime := protoTimestampToTime(request.Time)

isAvailable, err := g.app.Queries.HourAvailability.Handle(ctx, trainingTime)
if err != nil {
Expand All @@ -75,13 +61,6 @@ func (g GrpcServer) IsHourAvailable(ctx context.Context, request *trainer.IsHour
return &trainer.IsHourAvailableResponse{IsAvailable: isAvailable}, nil
}

func protoTimestampToTime(timestamp *timestamp.Timestamp) (time.Time, error) {
t, err := ptypes.Timestamp(timestamp)
if err != nil {
return time.Time{}, errors.New("unable to parse time")
}

t = t.UTC().Truncate(time.Hour)

return t, nil
func protoTimestampToTime(timestamp *timestamp.Timestamp) time.Time {
return timestamp.AsTime().UTC().Truncate(time.Hour)
}
1 change: 0 additions & 1 deletion internal/trainer/ports/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func NewHttpServer(application app.Application) HttpServer {
}

func (h HttpServer) GetTrainerAvailableHours(w http.ResponseWriter, r *http.Request, params GetTrainerAvailableHoursParams) {

dateModels, err := h.app.Queries.TrainerAvailableHours.Handle(r.Context(), query.AvailableHours{
From: params.DateFrom,
To: params.DateTo,
Expand Down
47 changes: 47 additions & 0 deletions internal/trainings/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
linters:
disable-all: true
enable:
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
- asciicheck
- bodyclose
- dogsled
- exhaustive
- exportloopref
- gocognit
- goconst
- gofmt
- goheader
- goimports
- gosec
- misspell
- nakedret
- nestif
- noctx
- rowserrcheck
- sqlclosecheck
- unconvert
- unparam
- whitespace

issues:
exclude:
- "composite literal uses unkeyed fields"
exclude-rules:
- path: _test\.go
linters:
- gosec
- noctx
- unparam
- bodyclose
- path: fixtures.go
linters:
- gosec
Loading

0 comments on commit 33e3c49

Please sign in to comment.