Skip to content

Commit

Permalink
adding githooks
Browse files Browse the repository at this point in the history
  • Loading branch information
fwidjaya20 committed Dec 25, 2023
1 parent f78f546 commit aa4d226
Show file tree
Hide file tree
Showing 46 changed files with 409 additions and 375 deletions.
7 changes: 7 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

goimports -l -w $(find . -type f -name '*.go' -not -path "*/vendor/*" -not -path "*/.go/*")
gofmt -l -s -w $(find . -type f -name '*.go' -not -path "*/vendor/*" -not -path "*/.go/*")
golines -w $(find . -type f -name '*.go' -not -path "*/vendor/*" -not -path "*/.go/*")
wsl --fix ./...
golangci-lint run
65 changes: 65 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
linters-settings:
errcheck:
check-type-assertions: true
exhaustive:
check-generated: false
explicit-exhaustive-switch: false
explicit-exhaustive-map: false
default-signifies-exhaustive: true
package-scope-only: false
gocognit:
min-complexity: 30
goconst:
min-len: 2
min-occurrences: 3
gocritic:
enabled-tags:
- diagnostic
- performance
- style
gocyclo:
min-complexity: 30
govet:
check-shadowing: true
nolintlint:
require-explanation: true
require-specific: true

linters:
disable-all: true
enable:
- bodyclose
- dogsled
- dupl
- errcheck
- exportloopref
- exhaustive
- exhaustruct
- goconst
- gocritic
- gofmt
- goimports
- gocyclo
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nolintlint
- nakedret
- prealloc
- predeclared
- revive
- staticcheck
- stylecheck
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- whitespace
- wsl

run:
issues-exit-code: 1
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include .env
export

.PHONY: init
init:
@./scripts/install-git-hooks.shg
12 changes: 8 additions & 4 deletions config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Application struct {
}

func NewApplication(filePath string) *Application {
if _, err := os.Stat(filePath); nil != err {
if _, err := os.Stat(filePath); err != nil {
log.Fatal("Environment file was not found.")
}

Expand All @@ -25,7 +25,7 @@ func NewApplication(filePath string) *Application {
app.viper.SetConfigFile("env")
app.viper.SetConfigFile(filePath)

if err := app.viper.ReadInConfig(); nil != err {
if err := app.viper.ReadInConfig(); err != nil {
log.Fatalln(err.Error())
}

Expand Down Expand Up @@ -74,10 +74,14 @@ func (app *Application) GetString(name string, defaultValue ...string) string {
return cast.ToString(app.Get(name, defaultValue))
}

func (app *Application) GetArrayString(name string, delimiter string, defaultValues ...string) []string {
func (app *Application) GetArrayString(
name string,
delimiter string,
defaultValues ...string,
) []string {
str := app.GetString(name, "")

if len(str) == 0 {
if str == "" {
return defaultValues
}

Expand Down
2 changes: 1 addition & 1 deletion config/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Binding = "symphonic.config"

type ServiceProvider struct{}

func (c *ServiceProvider) Boot(app foundation.Application) {}
func (c *ServiceProvider) Boot(_ foundation.Application) {}

func (c *ServiceProvider) Register(app foundation.Application) {
app.Instance(Binding, func(app foundation.Application) (any, error) {
Expand Down
4 changes: 2 additions & 2 deletions console/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (a *Application) Run(arguments []string, isExitAfterComplete bool) {
artisanIndex := -1

for i, it := range arguments {
if "artisan" == it {
if it == "artisan" {
artisanIndex = i
break
}
Expand All @@ -49,7 +49,7 @@ func (a *Application) Run(arguments []string, isExitAfterComplete bool) {
return
}

if err := a.Engine().Run(append([]string{arguments[0]}, arguments[artisanIndex+1:]...)); nil != err {
if err := a.Engine().Run(append([]string{arguments[0]}, arguments[artisanIndex+1:]...)); err != nil {
panic(err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion console/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Binding = "symphonic.console"

type ServiceProvider struct{}

func (provider *ServiceProvider) Boot(app foundation.Application) {}
func (provider *ServiceProvider) Boot(_ foundation.Application) {}

func (provider *ServiceProvider) Register(app foundation.Application) {
app.Instance(Binding, func(app foundation.Application) (any, error) {
Expand Down
26 changes: 19 additions & 7 deletions database/console/migrate.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package console

//nolint:revive // ignore due to golang-mgirate requirement
import (
"fmt"
"os"

"github.com/fwidjaya20/symphonic/constant"
"github.com/fwidjaya20/symphonic/contracts/config"
ContractConfig "github.com/fwidjaya20/symphonic/contracts/config"
"github.com/fwidjaya20/symphonic/database/driver"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/source/file"
)

func getMigrate(config config.Config) (*migrate.Migrate, error) {
func getMigrate(config ContractConfig.Config) (*migrate.Migrate, error) {
rootDir, _ := os.Getwd()
dbDriver := driver.GetDatabaseDriver(config)

artifactsDir := fmt.Sprintf("%s/%s/%s", rootDir, config.Get("database.dir", constant.DefaultDatabasePath), constant.DefaultMigrationDir)
databaseName := config.Get(fmt.Sprintf("database.connections.%s.database", config.Get("database.default")))
artifactsDir := fmt.Sprintf(
"%s/%s/%s",
rootDir,
config.Get("database.dir", constant.DefaultDatabasePath),
constant.DefaultMigrationDir,
)
databaseName := config.Get(
fmt.Sprintf("database.connections.%s.database", config.Get("database.default")),
)

entries, err := os.ReadDir(artifactsDir)
if nil != err {
if err != nil {
return nil, err
}

Expand All @@ -28,9 +36,13 @@ func getMigrate(config config.Config) (*migrate.Migrate, error) {
}

instance, err := dbDriver.GetInstance("schema_migrations")
if nil != err {
if err != nil {
return nil, err
}

return migrate.NewWithDatabaseInstance(fmt.Sprintf("file://%s", artifactsDir), databaseName.(string), instance)
return migrate.NewWithDatabaseInstance(
fmt.Sprintf("file://%s", artifactsDir),
databaseName.(string),
instance,
)
}
20 changes: 12 additions & 8 deletions database/console/migrate_command.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
//nolint:dupl // ignore due to database migrate logic
package console

import (
"errors"

"github.com/fwidjaya20/symphonic/contracts/config"
"github.com/fwidjaya20/symphonic/contracts/console"
ContractConfig "github.com/fwidjaya20/symphonic/contracts/config"
ContractConsole "github.com/fwidjaya20/symphonic/contracts/console"
"github.com/golang-migrate/migrate/v4"
"github.com/gookit/color"
"github.com/urfave/cli/v2"
)

type MigrateCommand struct {
config config.Config
config ContractConfig.Config
}

func NewMigrateCommand(config config.Config) console.Command {
func NewMigrateCommand(config ContractConfig.Config) ContractConsole.Command {
return &MigrateCommand{
config: config,
}
}

func (cmd *MigrateCommand) Setup() *cli.Command {
return &cli.Command{
return &cli.Command{ //nolint:exhaustruct // ignore due to cli configuration
Name: "migrate",
Description: "Run all database migrations",
Action: cmd.Handle,
Expand All @@ -30,23 +31,26 @@ func (cmd *MigrateCommand) Setup() *cli.Command {

func (cmd *MigrateCommand) Handle(*cli.Context) error {
instance, err := getMigrate(cmd.config)
if nil != err {
if err != nil {
if errors.Is(err, ErrEmptyMigrationDir) {
color.Yellowln("There is no migration files yet.")
return nil
}

return err
}
if nil == instance {

if instance == nil {
color.Yellowln("Database configuration was invalid!")
return nil
}

if err := instance.Up(); nil != err {
if err := instance.Up(); err != nil {
if errors.Is(err, migrate.ErrNoChange) {
color.Greenln("There is no new migration files.")
return nil
}

return err
}

Expand Down
26 changes: 15 additions & 11 deletions database/console/migrate_reset_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ package console
import (
"errors"

"github.com/fwidjaya20/symphonic/contracts/config"
"github.com/fwidjaya20/symphonic/contracts/console"
ContractConfig "github.com/fwidjaya20/symphonic/contracts/config"
ContractConsole "github.com/fwidjaya20/symphonic/contracts/console"
"github.com/golang-migrate/migrate/v4"
"github.com/gookit/color"
"github.com/urfave/cli/v2"
)

type MigrateResetCommand struct {
config config.Config
config ContractConfig.Config
}

func NewMigrateResetCommand(config config.Config) console.Command {
func NewMigrateResetCommand(config ContractConfig.Config) ContractConsole.Command {
return &MigrateResetCommand{
config: config,
}
}

func (cmd *MigrateResetCommand) Setup() *cli.Command {
return &cli.Command{
return &cli.Command{ //nolint:exhaustruct // ignore due to cli configuration
Name: "migrate:reset",
Category: "migrate",
Description: "Rollback all database migrations",
Expand All @@ -31,36 +31,40 @@ func (cmd *MigrateResetCommand) Setup() *cli.Command {

func (cmd *MigrateResetCommand) Handle(*cli.Context) error {
instance, err := getMigrate(cmd.config)
if nil != err {
if err != nil {
if errors.Is(err, ErrEmptyMigrationDir) {
color.Yellowln("There is no migration files yet.")
return nil
}

return err
}
if nil == instance {

if instance == nil {
color.Yellowln("Database configuration was invalid!")
return nil
}

if err := instance.Down(); nil != err && !errors.Is(err, migrate.ErrNoChange) {
if err = instance.Down(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
return err
}

instance, err = getSeeder(cmd.config)
if nil != err {
if err != nil {
if errors.Is(err, ErrEmptyMigrationDir) {
color.Yellowln("There is no seeder files yet.")
return nil
}

return err
}
if nil == instance {

if instance == nil {
color.Yellowln("Database configuration was invalid!")
return nil
}

if err := instance.Down(); nil != err && !errors.Is(err, migrate.ErrNoChange) {
if err := instance.Down(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
return err
}

Expand Down
Loading

0 comments on commit aa4d226

Please sign in to comment.