Skip to content

Commit

Permalink
ci: introduce golangci-lint (#5)
Browse files Browse the repository at this point in the history
also fix all issues 😅
  • Loading branch information
tessig authored Oct 19, 2023
1 parent c091de1 commit 1c368e8
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 14 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: golangci-lint
on:
push:
branches:
- master
pull_request:
branches:
- master

permissions:
contents: read
pull-requests: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 1.x
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
130 changes: 130 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Options for analysis running.
run:
concurrency: 4
timeout: 5m
tests: true
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true
modules-download-mode: readonly
allow-parallel-runners: false
go: '1.21'

# output configuration options
output:
# Format: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions
#
# Multiple can be specified by separating them by comma, output can be provided
# for each of them by separating format name and path by colon symbol.
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Example: "checkstyle:report.json,colored-line-number"
#
# Default: colored-line-number
format: colored-line-number
print-issued-lines: true
print-linter-name: true
uniq-by-line: true
sort-results: true

linters:
disable-all: true
enable:
- bidichk
- bodyclose
- containedctx
- contextcheck
- cyclop
- durationcheck
- errcheck
- errname
- errorlint
- exhaustive
- exportloopref
- forbidigo
- forcetypeassert
- gocognit
- goconst
- gocritic
- goerr113
- gofmt
- gomnd
- gomoddirectives
- gosec
- gosimple
- govet
- grouper
- ineffassign
- makezero
- misspell
- nakedret
- nestif
- nilerr
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
- revive
- staticcheck
- tenv
- testpackage
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- varnamelen
- wrapcheck
- wsl

issues:
new: false
fix: false
exclude-rules:
- path: _test\.go
linters:
- containedctx
- forcetypeassert
- goconst
- goerr113
- varnamelen
- wrapcheck
# for tests, we prioritize readability over memory optimization
- path: _test\.go
text: "fieldalignment: struct with"
linters:
- govet
include:
- EXC0005

linters-settings:
gomnd:
ignored-functions:
- context.WithTimeout
govet:
settings:
printf:
funcs:
- (flamingo.me/flamingo/v3/framework/flamingo.Logger).Debugf
enable:
- fieldalignment
nolintlint:
require-specific: true
require-explanation: true
revive:
rules:
- name: var-naming
disabled: true
varnamelen:
max-distance: 10
ignore-type-assert-ok: true
ignore-map-index-ok: true
ignore-chan-recv-ok: true
ignore-names:
- err
- id
ignore-decls:
- i int
8 changes: 6 additions & 2 deletions db/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ type (
}

dbConfig struct {
ConnectionOptions config.Map `inject:"config:mysql.db.connectionOptions,optional"`
Host string `inject:"config:mysql.db.host,optional"`
Port string `inject:"config:mysql.db.port,optional"`
DatabaseName string `inject:"config:mysql.db.databaseName,optional"`
Username string `inject:"config:mysql.db.user,optional"`
Password string `inject:"config:mysql.db.password,optional"`
MaxConnectionLifetime float64 `inject:"config:mysql.db.maxConnectionLifetime,optional"`
ConnectionOptions config.Map `inject:"config:mysql.db.connectionOptions,optional"`
}
)

Expand Down Expand Up @@ -84,9 +84,13 @@ func dbProvider(cfg *dbConfig, logger flamingo.Logger) DB {

if len(cfg.ConnectionOptions) > 0 {
options := url.Values{}

for key, value := range cfg.ConnectionOptions {
options.Set(key, value.(string))
if v, ok := value.(string); ok {
options.Set(key, v)
}
}

dataSourceURL += "?" + options.Encode()
}

Expand Down
2 changes: 2 additions & 0 deletions db/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
)

func TestModule_Configure(t *testing.T) {
t.Parallel()

if err := config.TryModules(nil, new(db.Module)); err != nil {
t.Error(err)
}
Expand Down
6 changes: 6 additions & 0 deletions db/shutdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import (
)

func TestShutdownSubscriber_Notify(t *testing.T) {
t.Parallel()

type args struct {
event flamingo.Event
}

tests := []struct {
name string
args args
Expand All @@ -36,8 +39,11 @@ func TestShutdownSubscriber_Notify(t *testing.T) {
wantClose: false,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

mockDB, mock, err := sqlmock.New()
if err != nil {
Expand Down
14 changes: 11 additions & 3 deletions migration/application/migrator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package application

import (
"errors"
"fmt"
"os"

"flamingo.me/flamingo/v3/framework/flamingo"
Expand All @@ -15,18 +17,20 @@ type (
Migrator struct {
db db.DB
logger flamingo.Logger
migration *migrate.Migrate
databaseName string
migrationDirectory string
migration *migrate.Migrate
}
)

func migrationFactory(m *Migrator) *migrate.Migrate {
conn := m.db.Connection()

driver, err := mysql.WithInstance(conn.DB, &mysql.Config{})
if err != nil {
panic(err)
}

migration, err := migrate.NewWithDatabaseInstance(
"file://"+m.migrationDirectory,
m.databaseName,
Expand All @@ -51,9 +55,11 @@ func (m *Migrator) Inject(
m.db = db
m.logger = logger
m.databaseName = conf.DatabaseName

if dbname, ok := os.LookupEnv("DBNAME"); ok {
m.databaseName = dbname
}

m.migrationDirectory = conf.MigrationDirectory
}

Expand All @@ -68,10 +74,12 @@ func (m *Migrator) Up(steps *int) error {
// and will migrate step versions down or applying all down migrations if step is not given
func (m *Migrator) Down(steps *int) error {
m.migration = migrationFactory(m)

if steps != nil {
tmpSteps := -*steps
steps = &tmpSteps
}

return m.runMigration(m.migration.Down, steps)
}

Expand All @@ -88,7 +96,7 @@ func (m *Migrator) runMigration(migratorFunc func() error, steps *int) error {
err = m.migration.Steps(*steps)
}

if err == migrate.ErrNoChange {
if errors.Is(err, migrate.ErrNoChange) {
logger.Info("migrations: No change")
return nil
} else if err != nil {
Expand All @@ -97,5 +105,5 @@ func (m *Migrator) runMigration(migratorFunc func() error, steps *int) error {

logger.Info("Migrations complete")

return err
return fmt.Errorf("db migration failed: %w", err)
}
11 changes: 8 additions & 3 deletions migration/application/seeder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package application

import (
"io/ioutil"
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -36,10 +36,10 @@ func (s *Seeder) Inject(
func (s *Seeder) Seed() error {
logger := s.logger.WithField(flamingo.LogKeyCategory, "seeds")

return filepath.Walk(s.seedsDirectory, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(s.seedsDirectory, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && filepath.Ext(info.Name()) == ".sql" {
logger.Info("Seed file %s ...", info.Name())
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
logger.Fatal("Problem while reading file content of %s:", info.Name())
panic(err)
Expand All @@ -52,4 +52,9 @@ func (s *Seeder) Seed() error {

return nil
})
if err != nil {
return fmt.Errorf("seeding failed: %w", err)
}

return nil
}
1 change: 1 addition & 0 deletions migration/application/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (s *StartUpMigrations) Inject(
func (s *StartUpMigrations) Notify(_ context.Context, event flamingo.Event) {
if _, ok := event.(*flamingo.StartupEvent); ok {
s.logger.Info("Run auto migrations...")

err := s.migrator.Up(nil)
if err != nil {
panic(err)
Expand Down
Loading

0 comments on commit 1c368e8

Please sign in to comment.