diff --git a/.github/workflows/go-tests.yml b/.github/workflows/go-tests.yml index 148c60de..9f8d82a9 100644 --- a/.github/workflows/go-tests.yml +++ b/.github/workflows/go-tests.yml @@ -30,10 +30,8 @@ jobs: with: go-version: 'stable' - uses: actions/checkout@v3 - - uses: golangci/golangci-lint-action@v3 - with: - skip-cache: true - args: --timeout=8m --build-tags testing + - name: make lint + run: make golangci-lint && GOLANGCI_LINT_EXTRA_ARGS="--timeout=1m --build-tags testing" make lint - name: Verify go vendor, go modules and gofmt run: | sudo apt-get install -y jq diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..e9004a23 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,40 @@ +# SPDX-License-Identifier: MIT +linters: + disable-all: true + fast: false + enable: + - gci + - goconst + - gocritic + - gocyclo + - gofmt + - gofumpt + - goimports + - godox + - govet + - gosec + - gosimple + - importas + - ineffassign + - loggercheck + - misspell + - nakedret + - nilerr + - predeclared + - promlinter + - revive + - staticcheck + - unconvert + - unused + - wastedassign + - whitespace + +linters-settings: + gci: + sections: + - standard + - default + - prefix(github.com/cloudbase/garm) + + goimports: + local-prefixes: github.com/cloudbase/garm diff --git a/Makefile b/Makefile index 74c09dc0..3a883f4c 100644 --- a/Makefile +++ b/Makefile @@ -10,47 +10,48 @@ VERSION ?= $(shell git describe --tags --match='v[0-9]*' --dirty --always) GARM_REF ?= $(shell git rev-parse --abbrev-ref HEAD) GO ?= go +.PHONY: help +help: ## Display this help. + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\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) + default: build +##@ Build + .PHONY : build-static test install-lint-deps lint go-test fmt fmtcheck verify-vendor verify create-release-files release -build-static: +build-static: ## Build garm statically @echo Building garm docker build --tag $(IMAGE_TAG) -f Dockerfile.build-static . 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 @echo Binaries are available in $(PWD)/build -create-release-files: - ./scripts/make-release.sh - -release: build-static create-release-files - -clean: +clean: ## Clean up build artifacts @rm -rf ./bin ./build ./release -build: +build: ## Build garm @echo Building garm ${VERSION} $(shell mkdir -p ./bin) @$(GO) build -ldflags "-s -w -X main.Version=${VERSION}" -tags osusergo,netgo,sqlite_omit_load_extension -o bin/garm ./cmd/garm @$(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 @echo Binaries are available in $(PWD)/bin -test: verify go-test +test: verify go-test ## Run tests -install-lint-deps: - @$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest +##@ Release +create-release-files: + ./scripts/make-release.sh -lint: - @golangci-lint run --timeout=8m --build-tags testing +release: build-static create-release-files ## Create a release -go-test: - @$(GO) test -race -mod=vendor -tags testing -v $(TEST_ARGS) -timeout=15m -parallel=4 -count=1 ./... +##@ Lint / Verify +.PHONY: lint +lint: golangci-lint $(GOLANGCI_LINT) ## Run linting. + $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS) -fmt: - @$(GO) fmt $$(go list ./...) - -fmtcheck: - @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 +.PHONY: lint-fix +lint-fix: golangci-lint $(GOLANGCI_LINT) ## Lint the codebase and run auto-fixers if supported by the linte + GOLANGCI_LINT_EXTRA_ARGS=--fix $(MAKE) lint verify-vendor: ## verify if all the go.mod/go.sum files are up-to-date $(eval TMPDIR := $(shell mktemp -d)) @@ -59,4 +60,32 @@ verify-vendor: ## verify if all the go.mod/go.sum files are up-to-date @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 @rm -rf ${TMPDIR} -verify: verify-vendor lint fmtcheck +verify: verify-vendor lint fmtcheck ## Run all verify-* targets + +##@ Development + +go-test: ## Run tests + @$(GO) test -race -mod=vendor -tags testing -v $(TEST_ARGS) -timeout=15m -parallel=4 -count=1 ./... + +fmt: ## Run go fmt against code. + @$(GO) fmt $$(go list ./...) + + +##@ Build Dependencies + +## Location to install dependencies to +LOCALBIN ?= $(shell pwd)/bin +$(LOCALBIN): + mkdir -p $(LOCALBIN) + +## Tool Binaries +GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint + +## Tool Versions +GOLANGCI_LINT_VERSION ?= v1.55.2 + +.PHONY: golangci-lint +golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. If wrong version is installed, it will be overwritten. +$(GOLANGCI_LINT): $(LOCALBIN) + test -s $(LOCALBIN)/golangci-lint && $(LOCALBIN)/golangci-lint --version | grep -q $(GOLANGCI_LINT_VERSION) || \ + GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)