diff --git a/.github/settings.yml b/.github/settings.yml index 3b40773..403be1c 100644 --- a/.github/settings.yml +++ b/.github/settings.yml @@ -74,6 +74,7 @@ branches: # Required. The list of status checks to require in order to merge into this branch contexts: - test + - lint # Required. Require at least one approving review on a pull request, before merging. Set to null to disable. required_pull_request_reviews: diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..458b7d1 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,42 @@ +name: Build +on: [push] +jobs: + test: + name: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.20' + + - name: Run tests + run: go test ./... + + lint: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.20' + cache: false + + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + # Require: The version of golangci-lint to use. + # When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version. + # When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit. + version: v1.53 + + # Optional: golangci-lint command line arguments. + # + # Note: By default, the `.golangci.yml` file should be at the root of the repository. + # The location of the configuration file can be changed by using `--config=` + args: --timeout=30m --out-format=colored-line-number --config=.golangci.yml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml deleted file mode 100644 index 881a403..0000000 --- a/.github/workflows/test.yaml +++ /dev/null @@ -1,16 +0,0 @@ -name: Run tests -on: [push] -jobs: - test: - name: test - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: '1.20' - - - name: Run tests - run: go test ./... diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..9f7050f --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,80 @@ +goVersion: &goVersion "1.20" + +run: + go: *goVersion + timeout: 5m + +linters: + disable-all: true + # Enable specific linter + # https://golangci-lint.run/usage/linters/#enabled-by-default-linters + enable: + - errorlint + - exportloopref + - forcetypeassert + - goconst + - gocritic + - gofumpt + - gosec + - gosimple + - govet + - ineffassign + - makezero + - misspell + - nakedret + - nolintlint + - prealloc + - prealloc + - staticcheck + - stylecheck + - unused + +linters-settings: + errorlint: + errorf: true + + gomodguard: + blocked: + # List of blocked modules. + modules: + # Blocked module. + - github.com/pkg/errors: + # Recommended modules that should be used instead. (Optional) + recommendations: + - errors + - fmt + reason: "This package is deprecated, use `fmt.Errorf` with `%w` instead" + + goconst: + ignore-tests: true + numbers: true + + gocritic: + disabled-checks: + - exitAfterDefer + - ifElseChain + - commentFormatting + + gofumpt: + lang-version: *goVersion + module-path: "pg-roll" + extra-rules: false + + staticcheck: + go: *goVersion + checks: ["all"] + + stylecheck: + go: *goVersion + checks: ["all", "-ST1000", "-ST1005"] + + unused: + go: *goVersion + + makezero: + always: false + + gosec: + exclude-generated: true + severity: low + includes: [] diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..81e27ad --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "gopls": { + "formatting.gofumpt": true, + "formatting.local": "pg-roll" + } +} diff --git a/cmd/analyze.go b/cmd/analyze.go index f7253db..9864f98 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -29,12 +29,12 @@ var analyzeCmd = &cobra.Command{ return err } - schemaJson, err := json.MarshalIndent(s, "", " ") + schemaJSON, err := json.MarshalIndent(s, "", " ") if err != nil { return err } - fmt.Println(string(schemaJson)) + fmt.Println(string(schemaJSON)) return nil }, } diff --git a/cmd/complete.go b/cmd/complete.go index 0f8d164..67283a1 100644 --- a/cmd/complete.go +++ b/cmd/complete.go @@ -3,9 +3,10 @@ package cmd import ( "fmt" "path/filepath" - "pg-roll/pkg/migrations" "strings" + "pg-roll/pkg/migrations" + "github.com/spf13/cobra" ) diff --git a/cmd/rollback.go b/cmd/rollback.go index d012afe..203c216 100644 --- a/cmd/rollback.go +++ b/cmd/rollback.go @@ -3,9 +3,10 @@ package cmd import ( "fmt" "path/filepath" - "pg-roll/pkg/migrations" "strings" + "pg-roll/pkg/migrations" + "github.com/spf13/cobra" ) diff --git a/cmd/root.go b/cmd/root.go index 2db554e..bff38e2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,22 +4,18 @@ import ( "github.com/spf13/cobra" ) -var ( - PGURL string -) +var PGURL string func init() { rootCmd.PersistentFlags().StringVar(&PGURL, "postgres_url", "postgres://postgres:postgres@localhost?sslmode=disable", "Postgres URL") } -var ( - rootCmd = &cobra.Command{ - Use: "pg-roll", - Short: "TODO: Add short description", - Long: `TODO: Add long description`, - SilenceUsage: true, - } -) +var rootCmd = &cobra.Command{ + Use: "pg-roll", + Short: "TODO: Add short description", + Long: `TODO: Add long description`, + SilenceUsage: true, +} // Execute executes the root command. func Execute() error { diff --git a/cmd/start.go b/cmd/start.go index 6a04edc..b6ced1b 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -3,9 +3,10 @@ package cmd import ( "fmt" "path/filepath" - "pg-roll/pkg/migrations" "strings" + "pg-roll/pkg/migrations" + "github.com/spf13/cobra" ) diff --git a/main.go b/main.go index 1d8e2e8..b7e53b9 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,13 @@ package main import ( + "os" + "pg-roll/cmd" ) func main() { - cmd.Execute() + if err := cmd.Execute(); err != nil { + os.Exit(1) + } } diff --git a/pkg/migrations/execute.go b/pkg/migrations/execute.go index ac35cc8..92a0cec 100644 --- a/pkg/migrations/execute.go +++ b/pkg/migrations/execute.go @@ -3,9 +3,10 @@ package migrations import ( "context" "fmt" - "pg-roll/pkg/schema" "strings" + "pg-roll/pkg/schema" + "github.com/lib/pq" ) diff --git a/pkg/migrations/migrations.go b/pkg/migrations/migrations.go index 80811f7..12133da 100644 --- a/pkg/migrations/migrations.go +++ b/pkg/migrations/migrations.go @@ -3,6 +3,7 @@ package migrations import ( "context" "database/sql" + "pg-roll/pkg/schema" _ "github.com/lib/pq" diff --git a/pkg/migrations/op_create_table.go b/pkg/migrations/op_create_table.go index de04d15..8a3f929 100644 --- a/pkg/migrations/op_create_table.go +++ b/pkg/migrations/op_create_table.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "pg-roll/pkg/schema" "github.com/lib/pq" diff --git a/pkg/migrations/op_create_table_test.go b/pkg/migrations/op_create_table_test.go index 5600582..4301c99 100644 --- a/pkg/migrations/op_create_table_test.go +++ b/pkg/migrations/op_create_table_test.go @@ -74,13 +74,13 @@ func TestRecordsCanBeInsertedIntoAndReadFromNewViewAfterMigrationStart(t *testin defer insertStmt.Close() type user struct { - Id int + ID int Name string } - inserted := []user{{Id: 1, Name: "Alice"}, {Id: 2, Name: "Bob"}} + inserted := []user{{ID: 1, Name: "Alice"}, {ID: 2, Name: "Bob"}} for _, v := range inserted { - _, err = insertStmt.Exec(v.Id, v.Name) + _, err = insertStmt.Exec(v.ID, v.Name) if err != nil { t.Fatal(err) } @@ -99,7 +99,7 @@ func TestRecordsCanBeInsertedIntoAndReadFromNewViewAfterMigrationStart(t *testin var retrieved []user for rows.Next() { var user user - if err := rows.Scan(&user.Id, &user.Name); err != nil { + if err := rows.Scan(&user.ID, &user.Name); err != nil { t.Fatal(err) } retrieved = append(retrieved, user)