Skip to content

Commit

Permalink
Setup Go Linter (dagu-org#590)
Browse files Browse the repository at this point in the history
* fix not to use pointer when unnecessary

* fix not to use pointer when unnecessary

* unuse unnecessary library functions

* move generated files into gen/ directory

* add comments

* rename methods

* refactor config

* refactor

* Move service/ to internal/

* fix ci files

* Add tool dependencies

* Update Makefile

* Update golanci-lint config

* Fix lint errors

* Refactor util.ParseCommand function

* refactor filecache.go

* Use any instead of interface{}

* Fix lint errors

* Add golines to tools.go

* Format go files to limit line length

* Fix lint errors

* refactor

* Fix lint errors

* fix race

* Make the tests run parallel

* Fix log message

* Fix data race

* Update vscode settings

* Add gotestsum to tools

* Add build/ to gitignore

* Adjust build command

* Fix lint errors

* fix .golangci.yml

* Adjust cmd tests

* Fix lint errors

* Use gotestsum for github action

* Fix code spell error

* Fix github test workflow

* Fix a bug in github action

* Update vscode settings

* Update github workflow

* Fix github workflow

* Update gotestsum format with standard-quiet

* Update github action

* Update github action

* Update github action
  • Loading branch information
yohamta committed Jun 19, 2024
1 parent 6fd1166 commit db06b99
Show file tree
Hide file tree
Showing 183 changed files with 3,762 additions and 2,290 deletions.
2 changes: 1 addition & 1 deletion .codespellrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[codespell]
# Ref: https://github.com/codespell-project/codespell#using-a-config-file
skip = .git,*.svg,go.sum,*.lock,*.css,.codespellrc
skip = .git,*.svg,go.mod,go.sum,*.lock,*.css,.codespellrc
check-hidden = true
# ignore-regex =
# short variables close to typos are often used
Expand Down
18 changes: 6 additions & 12 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,20 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: 1.22.x

- name: Installing swagger
run: |
go install github.com/go-swagger/go-swagger/cmd/swagger@latest
go-version-file: go.mod

- name: Set up NodeJS
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 16
node-version: 20

- name: Set up yarn
run: npm install --global yarn

- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Yarn install
run: yarn install
Expand All @@ -39,9 +35,7 @@ jobs:

- name: Copy web assets
run: |
cp ui/dist/*.js ./service/frontend/assets/
cp ui/dist/*.woff ./service/frontend/assets/
cp ui/dist/*.woff2 ./service/frontend/assets/
cp ui/dist/* ./internal/service/frontend/assets/
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
48 changes: 33 additions & 15 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
name: Test
on: [push, pull_request]
jobs:
build:
golint:
name: Go Linter
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.59
args: --timeout=10m

test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: 1.22.x
go-version-file: go.mod

- name: Installing swagger
run: |
go install github.com/go-swagger/go-swagger/cmd/swagger@latest
- name: Set up Nodejs
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 16
node-version: 20

- name: Set up yarn
run: npm install --global yarn

- name: Check out code
uses: actions/checkout@v3

- name: Yarn install
run: yarn install
working-directory: ui
Expand All @@ -35,25 +53,25 @@ jobs:

- name: Copy web assets
run: |
cp ui/dist/*.js ./service/frontend/assets/
cp ui/dist/*.woff ./service/frontend/assets/
cp ui/dist/*.woff2 ./service/frontend/assets/
cp ui/dist/* ./internal/service/frontend/assets/
- name: Cache Go modules
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Build
run: |
mkdir ./bin && go build -o ./bin/dagu .
make build-bin
- name: Test
run: |
go test --race -v -coverprofile="coverage.txt" -covermode=atomic ./...
make test-coverage
- name: Upload coverage
uses: codecov/codecov-action@v2
12 changes: 7 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# build
bin/*
# binaries
bin/
dist/
service/frontend/assets/fonts/*
service/frontend/assets/*
!service/frontend/assets/.gitkeep

# frontend assets
internal/service/frontend/assets/fonts/*
internal/service/frontend/assets/*
!internal/service/frontend/assets/.gitkeep

# NVM
.nvmrc
Expand Down
106 changes: 28 additions & 78 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,24 @@
linters:
disable-all: true
enable:
- goerr113
- errcheck
- goimports
# - paralleltest # missing the call to method parallel, but testify does not seem to work well with parallel test: https://github.com/stretchr/testify/issues/187
- revive # revive supersedes golint, which is now archived
- revive
- staticcheck
- vet
- govet
- forbidigo
- exhaustive
- godox
run:
skip-dirs:
- ^api
- ^proto
- ^.git
- gosimple
- ineffassign
- typecheck
- unused
- misspell
- gosec
linters-settings:
godox:
keywords:
- FIXME # marks TODOs that must be fixed before merging
govet:
fieldalignment: 0
forbidigo:
forbid:
- p: ^time\.After$
msg: "time.After may leak resources. Use time.NewTimer instead."
revive:
severity: error
confidence: 0.8
Expand All @@ -35,86 +28,43 @@ linters-settings:
# Disabled rules
- name: add-constant
disabled: true
- name: argument-limit
disabled: true
- name: bare-return
- name: max-public-structs
disabled: true
- name: banned-characters
- name: cognitive-complexity
disabled: true
- name: bool-literal-in-expr
- name: unchecked-type-assertion
disabled: true
- name: confusing-naming
- name: cyclomatic
disabled: true
- name: empty-lines
disabled: true
- name: error-naming
- name: confusing-naming
disabled: true
- name: errorf
- name: bare-return
disabled: true
- name: exported
- name: flag-parameter
disabled: true
- name: file-header
- name: unused-receiver
disabled: true
- name: function-length
disabled: true
- name: imports-blacklist
disabled: true
- name: increment-decrement
disabled: true
- name: line-length-limit
disabled: true
- name: max-public-structs
disabled: true
- name: nested-structs
disabled: true
- name: package-comments
disabled: true
- name: string-format
disabled: true
- name: unexported-naming
disabled: true
- name: unexported-return
disabled: true
- name: unused-parameter
disabled: true
- name: unused-receiver
disabled: true
- name: use-any
disabled: true
- name: var-naming
disabled: true
- name: empty-block
- name: deep-exit
disabled: true
- name: flag-parameter
disabled: true
- name: unnecessary-stmt
disabled: true

# Rule tuning
- name: cognitive-complexity
arguments:
- 25
- name: cyclomatic
arguments:
- 25
- name: function-result-limit
arguments:
- 5
- name: unhandled-error
arguments:
- "fmt.*"
- "bytes.Buffer.*"
- "strings.Builder.*"
issues:
exclude-dirs:
- ui
- assets
- .git
- cert
- bin
- internal/service/frontend/gen
- internal/service/scheduler/filenotify
exclude-rules:
- path: _test\.go|tests/.+\.go
text: "(cyclomatic|cognitive)" # false positives when using subtests
linters:
- err113 # like err = errors.New("test error")
- gosec
- add-constant
- revive
- path: _test\.go|tests/.+\.go
linters:
- goerr113 # like err = errors.New("test error")
- path: ^tools\/.+\.go
linters:
- goerr113
- revive
4 changes: 3 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
"dbaeumer.vscode-eslint",
// Prettier
"esbenp.prettier-vscode",
// Run on save
"emeraldwalk.runonsave",
]
}
}
25 changes: 15 additions & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@
"editor.rulers": [
80
],
"editor.renderWhitespace": "all",
"go.lintTool": "golangci-lint",
"go.lintFlags": [
"--fast"
],
"go.formatTool": "gofmt",
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[markdown]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"files.associations": {
"*.gohtml": "html"
}
},
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.go$",
"cmd": "golines ${file} -w"
}
]
},
}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ WORKDIR /app
COPY . .

RUN go mod download && rm -rf service/frontend/assets
COPY --from=ui-builder /app/dist/ ./service/frontend/assets/
COPY --from=ui-builder /app/dist/ ./internal/service/frontend/assets/

RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -ldflags="${LDFLAGS}" -o ./bin/dagu .

Expand Down
Loading

0 comments on commit db06b99

Please sign in to comment.