Skip to content

Commit

Permalink
switch to go-makefile-maker makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
defo89 committed Apr 16, 2024
1 parent 201a6a3 commit c34be07
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 36 deletions.
7 changes: 7 additions & 0 deletions .license-scan-overrides.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{"name": "github.com/chzyer/logex", "licenceType": "MIT"}
{"name": "github.com/hashicorp/vault/api/auth/approle", "licenceType": "MPL-2.0"}
{"name": "github.com/jpillora/longestcommon", "licenceType": "MIT"}
{"name": "github.com/spdx/tools-golang", "licenceTextOverrideFile": "vendor/github.com/spdx/tools-golang/LICENSE.code"}
{"name": "github.com/xeipuuv/gojsonpointer", "licenceType": "Apache-2.0"}
{"name": "github.com/xeipuuv/gojsonreference", "licenceType": "Apache-2.0"}
{"name": "github.com/xeipuuv/gojsonschema", "licenceType": "Apache-2.0"}
128 changes: 95 additions & 33 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,60 +1,122 @@
IMAGE := keppel.eu-de-1.cloud.sap/ccloud/go-pmtud
DATE := $(shell date +%Y%m%d%H%M%S)
VERSION ?= v$(DATE)
################################################################################
# This file is AUTOGENERATED with <https://github.com/sapcc/go-makefile-maker> #
# Edit Makefile.maker.yaml instead. #
################################################################################

.PHONY: all
MAKEFLAGS=--warn-undefined-variables
# /bin/sh is dash on Debian which does not support all features of ash/bash
# to fix that we use /bin/bash only on Debian to not break Alpine
ifneq (,$(wildcard /etc/os-release)) # check file existence
ifneq ($(shell grep -c debian /etc/os-release),0)
SHELL := /bin/bash
endif
endif

all: build push
default: FORCE
@echo 'There is nothing to build, use `make check` for running the test suite or `make help` for a list of available targets.'

build:
docker build -t $(IMAGE):$(VERSION) .
prepare-static-check: FORCE
@if ! hash golangci-lint 2>/dev/null; then printf "\e[1;36m>> Installing golangci-lint (this may take a while)...\e[0m\n"; go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; fi
@if ! hash go-licence-detector 2>/dev/null; then printf "\e[1;36m>> Installing go-licence-detector...\e[0m\n"; go install go.elastic.co/go-licence-detector@latest; fi
@if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi

push: build
docker push ${IMAGE}:${VERSION}
install-controller-gen: FORCE
@if ! hash controller-gen 2>/dev/null; then printf "\e[1;36m>> Installing controller-gen...\e[0m\n"; go install sigs.k8s.io/controller-tools/cmd/controller-gen@latest; fi

docker-push-mac:
docker buildx build --platform linux/amd64 . -t ${IMAGE}:${VERSION} --push
install-setup-envtest: FORCE
@if ! hash setup-envtest 2>/dev/null; then printf "\e[1;36m>> Installing setup-envtest...\e[0m\n"; go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest; fi

## For https://github.com/sapcc/go-makefile-maker
GO_BUILDFLAGS =
GO_LDFLAGS =
GO_TESTENV =
TESTBIN=$(shell pwd)/testbin

# which packages to test with "go test"
# which packages to test with test runner
GO_TESTPKGS := $(shell go list -f '{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...)
# which packages to measure coverage for
GO_COVERPKGS := $(shell go list ./...)
# to get around weird Makefile syntax restrictions, we need variables containing nothing, a space and comma
null :=
space := $(null) $(null)
comma := ,

.PHONY: license-headers
license-headers: FORCE
@if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi
find * \( -name vendor -type d -prune \) -o \( -name \*.go -exec addlicense -c "SAP SE" -- {} + \)
check: FORCE static-check build/cover.html
@printf "\e[1;32m>> All checks successful.\e[0m\n"

check-dependency-licenses: FORCE prepare-static-check
@printf "\e[1;36m>> go-licence-detector\e[0m\n"
@go list -m -mod=readonly -json all | go-licence-detector -includeIndirect -rules .license-scan-rules.json -overrides .license-scan-overrides.jsonl
generate: install-controller-gen
@printf "\e[1;36m>> controller-gen\e[0m\n"
@controller-gen crd rbac:roleName=go-pmtud paths="./..." output:crd:artifacts:config=crd
@controller-gen object paths=./...

prepare-static-check: FORCE
@if ! hash golangci-lint 2>/dev/null; then printf "\e[1;36m>> Installing golangci-lint (this may take a while)...\e[0m\n"; go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; fi
@if ! hash go-licence-detector 2>/dev/null; then printf "\e[1;36m>> Installing go-licence-detector...\e[0m\n"; go install go.elastic.co/go-licence-detector@latest; fi
@if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi
run-golangci-lint: FORCE prepare-static-check
@printf "\e[1;36m>> golangci-lint\e[0m\n"
@golangci-lint run

build/cover.out: FORCE generate install-setup-envtest | build
@printf "\e[1;36m>> Running tests\e[0m\n"
KUBEBUILDER_ASSETS="$(shell setup-envtest use 1.24 --bin-dir $(TESTBIN) -p path)" go test -shuffle=on -p 1 -coverprofile=$@ $(GO_BUILDFLAGS) -ldflags '-s -w $(GO_LDFLAGS)' -covermode=count -coverpkg=$(subst $(space),$(comma),$(GO_COVERPKGS)) $(GO_TESTPKGS)

build/cover.html: build/cover.out
@printf "\e[1;36m>> go tool cover > build/cover.html\e[0m\n"
@go tool cover -html $< -o $@

static-check: FORCE run-golangci-lint check-dependency-licenses check-license-headers

build:
@mkdir $@

tidy-deps: FORCE
go mod tidy
go mod verify

license-headers: FORCE prepare-static-check
@printf "\e[1;36m>> addlicense\e[0m\n"
@addlicense -c "SAP SE" -- $(patsubst $(shell awk '$$1 == "module" {print $$2}' go.mod)%,.%/*.go,$(shell go list ./...))

check-license-headers: FORCE prepare-static-check
@printf "\e[1;36m>> addlicense --check\e[0m\n"
@addlicense --check -- $(patsubst $(shell awk '$$1 == "module" {print $$2}' go.mod)%,.%/*.go,$(shell go list ./...))

bin/cover.out: FORCE | build
@printf "\e[1;36m>> go test\e[0m\n"
@env $(GO_TESTENV) go test $(GO_BUILDFLAGS) -ldflags '-s -w $(GO_LDFLAGS)' -shuffle=on -p 1 -coverprofile=$@ -covermode=count -coverpkg=$(subst $(space),$(comma),$(GO_COVERPKGS)) $(GO_TESTPKGS)
check-dependency-licenses: FORCE prepare-static-check
@printf "\e[1;36m>> go-licence-detector\e[0m\n"
@go list -m -mod=readonly -json all | go-licence-detector -includeIndirect -rules .license-scan-rules.json -overrides .license-scan-overrides.jsonl

bin/cover.html: bin/cover.out
@printf "\e[1;36m>> go tool cover > bin/cover.html\e[0m\n"
@go tool cover -html $< -o $@
clean: FORCE
git clean -dxf build

vars: FORCE
@printf "DESTDIR=$(DESTDIR)\n"
@printf "GO_BUILDFLAGS=$(GO_BUILDFLAGS)\n"
@printf "GO_COVERPKGS=$(GO_COVERPKGS)\n"
@printf "GO_LDFLAGS=$(GO_LDFLAGS)\n"
@printf "GO_TESTENV=$(GO_TESTENV)\n"
@printf "GO_TESTPKGS=$(GO_TESTPKGS)\n"
@printf "PREFIX=$(PREFIX)\n"
@printf "TESTBIN=$(TESTBIN)\n"
help: FORCE
@printf "\n"
@printf "\e[1mUsage:\e[0m\n"
@printf " make \e[36m<target>\e[0m\n"
@printf "\n"
@printf "\e[1mGeneral\e[0m\n"
@printf " \e[36mvars\e[0m Display values of relevant Makefile variables.\n"
@printf " \e[36mhelp\e[0m Display this help.\n"
@printf "\n"
@printf "\e[1mPrepare\e[0m\n"
@printf " \e[36mprepare-static-check\e[0m Install any tools required by static-check. This is used in CI before dropping privileges, you should probably install all the tools using your package manager\n"
@printf " \e[36minstall-controller-gen\e[0m Install controller-gen required by static-check and build-all. This is used in CI before dropping privileges, you should probably install all the tools using your package manager\n"
@printf " \e[36minstall-setup-envtest\e[0m Install setup-envtest required by check. This is used in CI before dropping privileges, you should probably install all the tools using your package manager\n"
@printf "\n"
@printf "\e[1mTest\e[0m\n"
@printf " \e[36mcheck\e[0m Run the test suite (unit tests and golangci-lint).\n"
@printf " \e[36mgenerate\e[0m Generate code for Kubernetes CRDs and deepcopy.\n"
@printf " \e[36mrun-golangci-lint\e[0m Install and run golangci-lint. Installing is used in CI, but you should probably install golangci-lint using your package manager.\n"
@printf " \e[36mbuild/cover.out\e[0m Run tests and generate coverage report.\n"
@printf " \e[36mbuild/cover.html\e[0m Generate an HTML file with source code annotations from the coverage report.\n"
@printf " \e[36mstatic-check\e[0m Run static code checks\n"
@printf "\n"
@printf "\e[1mDevelopment\e[0m\n"
@printf " \e[36mtidy-deps\e[0m Run go mod tidy and go mod verify.\n"
@printf " \e[36mlicense-headers\e[0m Add license headers to all non-vendored .go files.\n"
@printf " \e[36mcheck-license-headers\e[0m Check license headers in all non-vendored .go files.\n"
@printf " \e[36mcheck-dependency-licenses\e[0m Check all dependency licenses using go-licence-detector.\n"
@printf " \e[36mclean\e[0m Run git clean.\n"

.PHONY: FORCE
3 changes: 0 additions & 3 deletions Makefile.maker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
metadata:
url: https://github.com/sapcc/go-pmtud

makefile:
enabled: false

dockerfile:
enabled: false

Expand Down

0 comments on commit c34be07

Please sign in to comment.