Skip to content

Commit

Permalink
build: Add task for linting & formatting Go code
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Schneppenheim authored and weeco committed Jan 11, 2023
1 parent 91bbc4d commit 1fa14e7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
7 changes: 2 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Docker Compose
docker-compose/zk-single-kafka-single

# Misc
.DS_Store
notes.md
Expand All @@ -19,5 +16,5 @@ requests.txt
# Local Formatting
.prettierrc

docker-compose.yml
*.docker-compose.yml
# Local build tools installed via Taskfiles
build
16 changes: 13 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ The basics:

Prerequisites that should be installed before proceeding:

- Latest Node.js v17 & npm v8
- Latest Go version (currently v1.18)
- Latest Node.js v18 & npm v8
- Latest Go version
- Docker (required to start a local redpanda cluster)

Redpanda Console consists of two components:
Expand Down Expand Up @@ -80,7 +80,7 @@ project, and we've designed our review process and standards to reflect that.
However, we're always trying to improve, and welcome any suggestions and
feedback.

### Formatting
### Formatting & linting

All mainstream editors and IDEs should provide support (either natively
or through a plugin) for integrating with code formatting tools that can
Expand All @@ -93,6 +93,16 @@ reformatting, which makes reviewing changes difficult. In this case it would be
appropriate to split the code change and formatting into separate commits. This is
usually unnecessary.

Before submitting your commits, please validate them against the existing linter
rules. This repository uses a task runner in order to install the required
dependencies (build tools, linters, formatters) as well as linting and formatting
code. In order to use them:

1. Install [task](https://taskfile.dev/installation/) on your machine
2. In the repository root run: `task backend:fmt`
3. If you run `task backend:lint` in the repository root the output will highlight
linting issues.

### Commit history

The ordering and structure of commits in a pull request is very important. This
Expand Down
11 changes: 11 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 3

vars:
BUILD_ROOT: "{{ .ROOT_DIR }}/build"
BACKEND_ROOT: "{{ .ROOT_DIR }}/backend"
FRONTEND_ROOT: "{{ .ROOT_DIR }}/frontend"
BRANCH_SLUG:
sh: git rev-parse --abbrev-ref HEAD | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z

includes:
backend: taskfiles/backend.yaml
50 changes: 50 additions & 0 deletions taskfiles/backend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version: 3
tasks:
install-golangci-lint:
desc: install golangci linter
vars:
GO_LINT_VERSION: 1.50.1
cmds:
- mkdir -p {{ .BUILD_ROOT}}/bin
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "{{ .BUILD_ROOT }}"/bin/go v{{ .GO_LINT_VERSION }}
status:
- '[ -f ''{{ .BUILD_ROOT }}/bin/go'' ] || command -v {{ .BUILD_ROOT }}/bin/go/golangci-lint >/dev/null 2>&1'
- '[[ $({{ .BUILD_ROOT }}/bin/go/golangci-lint --version) == *"version {{ .GO_LINT_VERSION }} built"* ]]'

install-gofumpt:
vars:
GOFUMPT_VERSION: 0.4.0
desc: install gofumpt go formater
cmds:
- |
GOBIN={{ .BUILD_ROOT }}/bin/go go install mvdan.cc/gofumpt@v{{ .GOFUMPT_VERSION }}
status:
- '[ -f ''{{ .BUILD_ROOT }}/bin/go'' ] || command -v {{ .BUILD_ROOT }}/bin/go/gofumpt >/dev/null 2>&1'
- '[[ $({{ .BUILD_ROOT }}/bin/go/gofumpt --version) == v{{.GOFUMPT_VERSION}} ]]'

install-gci:
vars:
GCI_VERSION: 0.9.0
desc: install gci
cmds:
- GOBIN={{ .BUILD_ROOT }}/bin/go go install github.com/daixiang0/gci@v{{.GCI_VERSION}}
status:
- '[ -f ''{{ .BUILD_ROOT }}/bin/go'' ] || command -v {{ .BUILD_ROOT }}/bin/go/gci >/dev/null 2>&1'

fmt:
desc: Format Go code
deps:
- install-gci
- install-gofumpt
dir: "{{.BACKEND_ROOT}}"
cmds:
- '{{ .BUILD_ROOT }}/bin/go/gofumpt -l -w .'
- '{{ .BUILD_ROOT }}/bin/go/gci write --NoPrefixComments -s standard -s default -s ''Prefix(github.com/redpanda-data/console/backend)'' .'

lint:
desc: Run Go linters for backend code
deps:
- install-golangci-lint
dir: "{{.BACKEND_ROOT}}"
cmds:
- '{{ .BUILD_ROOT }}/bin/go/golangci-lint run --timeout 10m ./...'

0 comments on commit 1fa14e7

Please sign in to comment.