Skip to content

Commit 9ea2a31

Browse files
committed
feat: introduce golangci config and help in make
Signed-off-by: Mario Constanti <[email protected]>
1 parent dd6f1e4 commit 9ea2a31

File tree

3 files changed

+92
-25
lines changed

3 files changed

+92
-25
lines changed

.github/workflows/go-tests.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ jobs:
3030
with:
3131
go-version: 'stable'
3232
- uses: actions/checkout@v3
33-
- uses: golangci/golangci-lint-action@v3
34-
with:
35-
skip-cache: true
36-
args: --timeout=8m --build-tags testing
33+
- name: make lint
34+
run: make golangci-lint && GOLANGCI_LINT_EXTRA_ARGS="--timeout=1m --build-tags testing" make lint
3735
- name: Verify go vendor, go modules and gofmt
3836
run: |
3937
sudo apt-get install -y jq

.golangci.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# SPDX-License-Identifier: MIT
2+
linters:
3+
disable-all: true
4+
fast: false
5+
enable:
6+
- gci
7+
- goconst
8+
- gocritic
9+
- gocyclo
10+
- gofmt
11+
- gofumpt
12+
- goimports
13+
- godox
14+
- govet
15+
- gosec
16+
- gosimple
17+
- importas
18+
- ineffassign
19+
- loggercheck
20+
- misspell
21+
- nakedret
22+
- nilerr
23+
- predeclared
24+
- promlinter
25+
- revive
26+
- staticcheck
27+
- unconvert
28+
- unused
29+
- wastedassign
30+
- whitespace
31+
32+
linters-settings:
33+
gci:
34+
sections:
35+
- standard
36+
- default
37+
- prefix(github.com/cloudbase/garm)
38+
39+
goimports:
40+
local-prefixes: github.com/cloudbase/garm

Makefile

+50-21
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,48 @@ VERSION ?= $(shell git describe --tags --match='v[0-9]*' --dirty --always)
1010
GARM_REF ?= $(shell git rev-parse --abbrev-ref HEAD)
1111
GO ?= go
1212

13+
.PHONY: help
14+
help: ## Display this help.
15+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
16+
1317

1418
default: build
1519

20+
##@ Build
21+
1622
.PHONY : build-static test install-lint-deps lint go-test fmt fmtcheck verify-vendor verify create-release-files release
17-
build-static:
23+
build-static: ## Build garm statically
1824
@echo Building garm
1925
docker build --tag $(IMAGE_TAG) -f Dockerfile.build-static .
2026
docker run --rm -e USER_ID=$(USER_ID) -e GARM_REF=$(GARM_REF) -e USER_GROUP=$(USER_GROUP) -v $(PWD)/build:/build/output:z $(IMAGE_TAG) /build-static.sh
2127
@echo Binaries are available in $(PWD)/build
2228

23-
create-release-files:
24-
./scripts/make-release.sh
25-
26-
release: build-static create-release-files
27-
28-
clean:
29+
clean: ## Clean up build artifacts
2930
@rm -rf ./bin ./build ./release
3031

31-
build:
32+
build: ## Build garm
3233
@echo Building garm ${VERSION}
3334
$(shell mkdir -p ./bin)
3435
@$(GO) build -ldflags "-s -w -X main.Version=${VERSION}" -tags osusergo,netgo,sqlite_omit_load_extension -o bin/garm ./cmd/garm
3536
@$(GO) build -ldflags "-s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=${VERSION}" -tags osusergo,netgo,sqlite_omit_load_extension -o bin/garm-cli ./cmd/garm-cli
3637
@echo Binaries are available in $(PWD)/bin
3738

38-
test: verify go-test
39+
test: verify go-test ## Run tests
3940

40-
install-lint-deps:
41-
@$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
41+
##@ Release
42+
create-release-files:
43+
./scripts/make-release.sh
4244

43-
lint:
44-
@golangci-lint run --timeout=8m --build-tags testing
45+
release: build-static create-release-files ## Create a release
4546

46-
go-test:
47-
@$(GO) test -race -mod=vendor -tags testing -v $(TEST_ARGS) -timeout=15m -parallel=4 -count=1 ./...
48-
49-
fmt:
50-
@$(GO) fmt $$(go list ./...)
47+
##@ Lint / Verify
48+
.PHONY: lint
49+
lint: golangci-lint $(GOLANGCI_LINT) ## Run linting.
50+
$(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
5151

52-
fmtcheck:
53-
@gofmt -l -s $$(go list ./... | sed 's|github.com/cloudbase/garm/||g') | grep ".*\.go"; if [ "$$?" -eq 0 ]; then echo "gofmt check failed; please run gofmt -w -s"; exit 1;fi
52+
.PHONY: lint-fix
53+
lint-fix: golangci-lint $(GOLANGCI_LINT) ## Lint the codebase and run auto-fixers if supported by the linte
54+
GOLANGCI_LINT_EXTRA_ARGS=--fix $(MAKE) lint
5455

5556
verify-vendor: ## verify if all the go.mod/go.sum files are up-to-date
5657
$(eval TMPDIR := $(shell mktemp -d))
@@ -59,4 +60,32 @@ verify-vendor: ## verify if all the go.mod/go.sum files are up-to-date
5960
@diff -r -u -q ${ROOTDIR} ${TMPDIR}/garm >/dev/null 2>&1; if [ "$$?" -ne 0 ];then echo "please run: go mod tidy && go mod vendor"; exit 1; fi
6061
@rm -rf ${TMPDIR}
6162

62-
verify: verify-vendor lint fmtcheck
63+
verify: verify-vendor lint fmtcheck ## Run all verify-* targets
64+
65+
##@ Development
66+
67+
go-test: ## Run tests
68+
@$(GO) test -race -mod=vendor -tags testing -v $(TEST_ARGS) -timeout=15m -parallel=4 -count=1 ./...
69+
70+
fmt: ## Run go fmt against code.
71+
@$(GO) fmt $$(go list ./...)
72+
73+
74+
##@ Build Dependencies
75+
76+
## Location to install dependencies to
77+
LOCALBIN ?= $(shell pwd)/bin
78+
$(LOCALBIN):
79+
mkdir -p $(LOCALBIN)
80+
81+
## Tool Binaries
82+
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
83+
84+
## Tool Versions
85+
GOLANGCI_LINT_VERSION ?= v1.55.2
86+
87+
.PHONY: golangci-lint
88+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. If wrong version is installed, it will be overwritten.
89+
$(GOLANGCI_LINT): $(LOCALBIN)
90+
test -s $(LOCALBIN)/golangci-lint && $(LOCALBIN)/golangci-lint --version | grep -q $(GOLANGCI_LINT_VERSION) || \
91+
GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)

0 commit comments

Comments
 (0)