diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 0000000..c54182c --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,33 @@ +name: golangci-lint +on: + push: + branches: + - main + pull_request: + +permissions: + contents: read + +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: stable + + - name: Install Goreleaser + uses: goreleaser/goreleaser-action@v6 + with: + version: "v2.8.2" + install-only: true + + - name: Build railcar binaries + run: make railcar-build-binaries + + - name: golangci-lint + uses: golangci/golangci-lint-action@v7 + with: + version: v2.1.2 diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..9024c83 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,9 @@ +# https://golangci-lint.run/ +version: "2" +linters: + enable: + - gocyclo +formatters: + enable: + - gofumpt + - goimports diff --git a/Makefile b/Makefile index 4253c02..afcca87 100644 --- a/Makefile +++ b/Makefile @@ -12,3 +12,7 @@ railcar-build-binaries: .Phony: go-test go-test: go test ./... --race + +.Phony: lint +lint: + golangci-lint run \ No newline at end of file diff --git a/cmd/conductor/main.go b/cmd/conductor/main.go index 273fd9c..4e3d31b 100644 --- a/cmd/conductor/main.go +++ b/cmd/conductor/main.go @@ -25,7 +25,9 @@ func main() { if err != nil { pterm.Fatal.Println(err) } - defer file.Close() + defer func() { + _ = file.Close() + }() byt, err := io.ReadAll(file) if err != nil { @@ -41,7 +43,7 @@ func main() { case githooks.CommitMsg: commitMsg, err := os.ReadFile(os.Args[2]) if err != nil { - pterm.Fatal.Printfln("Error reading commit message file: ", err) + pterm.Fatal.Println("Error reading commit message file: ", err) } // Process the commit message @@ -57,7 +59,7 @@ func main() { case githooks.PrepareCommitMsg: commitMsg, err := os.ReadFile(os.Args[2]) if err != nil { - pterm.Fatal.Printfln("Error reading commit message file: ", err) + pterm.Fatal.Println("Error reading commit message file: ", err) } if len(cfg.RailCar.CommitOperations.PrepareCommitMsg) != 0 { run(cfg.RailCar.CommitOperations.PrepareCommitMsg, string(commitMsg)) @@ -95,7 +97,7 @@ func run(data []config.HookStep, hookData string) { if strings.Contains(v.Command, "{HOOK_INPUT}") { hookData = strings.TrimSuffix(hookData, "\n") - newStr := strings.Replace(v.Command, "{HOOK_INPUT}", hookData, -1) + newStr := strings.ReplaceAll(v.Command, "{HOOK_INPUT}", hookData) cmd = exec.Command("sh", "-c", newStr) } else { cmd = exec.Command("sh", "-c", fmt.Sprintf("%s %s", v.Command, hookData)) diff --git a/internal/blueprint/blueprint.go b/internal/blueprint/blueprint.go index 4472b29..74761a1 100644 --- a/internal/blueprint/blueprint.go +++ b/internal/blueprint/blueprint.go @@ -39,7 +39,9 @@ func (b *BluePrint) Write() error { if err != nil { return err } - defer file.Close() + defer func() { + _ = file.Close() + }() if err := tmpl.Execute(file, b.Values); err != nil { return err diff --git a/internal/validate/validate_test.go b/internal/validate/validate_test.go index 9b4b985..9696095 100644 --- a/internal/validate/validate_test.go +++ b/internal/validate/validate_test.go @@ -41,7 +41,9 @@ func TestGitDirs(t *testing.T) { oldWd, err := os.Getwd() require.NoError(t, err) require.NoError(t, os.Chdir(tempDir)) - defer os.Chdir(oldWd) + defer func() { + _ = os.Chdir(oldWd) + }() err = validate.GitDirs() if tc.fail {