Skip to content

Commit

Permalink
Use go-makefile-maker
Browse files Browse the repository at this point in the history
  • Loading branch information
talal committed Nov 19, 2020
1 parent 7014fb3 commit f42f1de
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 47 deletions.
107 changes: 60 additions & 47 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,68 +1,81 @@
PKG = github.com/sapcc/absent-metrics-operator
PREFIX := /usr
################################################################################
# This file is AUTOGENERATED with <https://github.com/sapcc/go-makefile-maker> #
# Edit Makefile.maker.yaml instead. #
################################################################################

GOOS = $(shell go env GOOS)
GOARCH = $(shell go env GOARCH)
KUBEBUILDER_RELEASE_VERSION = $(shell cat test/.kubebuilder-version)
default: build-all

VERSION := $(shell git describe --abbrev=7)
COMMIT_HASH := $(shell git rev-parse --verify HEAD)
BUILD_DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%S%Z")

GO := GOBIN=$(CURDIR)/build go
GO_BUILDFLAGS :=
GO_LDFLAGS := -s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT_HASH) -X main.date=$(BUILD_DATE)
GOOS = $(shell go env GOOS)
GOARCH = $(shell go env GOARCH)
KUBEBUILDER_RELEASE_VERSION = $(shell cat test/.kubebuilder-version)

test/bin:
@mkdir $@
# Download kubebuilder and extract it to tmp
curl -L https://go.kubebuilder.io/dl/$(KUBEBUILDER_RELEASE_VERSION)/$(GOOS)/$(GOARCH) | tar -xz -C /tmp/
# Move to test/bin
mv /tmp/kubebuilder*/bin/* test/bin/

# attach `make test/bin` as a dependency to the autogenerated `make build/cover.out`
build/cover.out: test/bin

build/release-info: CHANGELOG.md
@mkdir -p build
go run $(GO_BUILDFLAGS) tools/releaseinfo/main.go $< $(shell git describe --tags --abbrev=0) > $@

all: FORCE build/absent-metrics-operator
build-all: build/absent-metrics-operator

build/absent-metrics-operator: FORCE | build
$(GO) install $(GO_BUILDFLAGS) -ldflags '$(GO_LDFLAGS)' '$(PKG)'
GO_BUILDFLAGS = -mod vendor
GO_LDFLAGS = -X main.version=$(VERSION) -X main.commit=$(COMMIT_HASH) -X main.date=$(BUILD_DATE)
GO_TESTENV =

build/absent-metrics-operator: FORCE
go build $(GO_BUILDFLAGS) -ldflags '-s -w $(GO_LDFLAGS)' -o build/absent-metrics-operator .

DESTDIR =
ifeq ($(shell uname -s),Darwin)
PREFIX = /usr/local
else
PREFIX = /usr
endif

install: FORCE build/absent-metrics-operator
install -D -m 0755 build/absent-metrics-operator "$(DESTDIR)$(PREFIX)/bin/absent-metrics-operator"

# Run all checks
check: FORCE build/absent-metrics-operator lint build/cover.html
@printf "\e[1;32m>> All checks successful\e[0m\n"
# which packages to test with "go test"
GO_TESTPKGS := $(shell go list -f '{{if .TestGoFiles}}{{.ImportPath}}{{end}}' ./...)
# which packages to measure coverage for
GO_COVERPKGS := $(shell go list ./...)
# to get around weird Makefile syntax restrictions, we need variables containing a space and comma
space := $(null) $(null)
comma := ,

lint: FORCE
@printf "\e[1;34m>> golangci-lint\e[0m\n"
check: build-all static-check build/cover.html FORCE
@printf "\e[1;32m>> All checks successful.\e[0m\n"

static-check: FORCE
@command -v golangci-lint >/dev/null 2>&1 || { echo >&2 "Error: golangci-lint is not installed. See: https://golangci-lint.run/usage/install/"; exit 1; }
golangci-lint run

# Run unit tests
test: FORCE | test/bin
@printf "\e[1;34m>> go test\e[0m\n"
$(GO) test $(GO_BUILDFLAGS) -ldflags '$(GO_LDFLAGS)' $(PKG)/test

# Test with coverage
test-coverage: FORCE build/cover.out
build/cover.out: FORCE | build test/bin
@printf "\e[1;34m>> go test with coverage\e[0m\n"
$(GO) test $(GO_BUILDFLAGS) -ldflags '$(GO_LDFLAGS)' -failfast -race -covermode=atomic -coverpkg=$(PKG)/internal/controller -coverprofile=$@ $(PKG)/test
build/cover.html: build/cover.out
$(GO) tool cover -html $< -o $@
@printf "\e[1;36m>> golangci-lint\e[0m\n"
@golangci-lint run

build/release-info: CHANGELOG.md | build
$(GO) run $(GO_BUILDFLAGS) tools/releaseinfo.go $< $(shell git describe --tags --abbrev=0) > $@
build/cover.out: FORCE
@printf "\e[1;36m>> go test\e[0m\n"
@env $(GO_TESTENV) go test $(GO_BUILDFLAGS) -ldflags '-s -w $(GO_LDFLAGS)' -p 1 -coverprofile=$@ -covermode=count -coverpkg=$(subst $(space),$(comma),$(GO_COVERPKGS)) $(GO_TESTPKGS)

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

# Download the kubebuilder control plane binaries
test/bin:
mkdir $@
# Download kubebuilder and extract it to tmp
curl -L https://go.kubebuilder.io/dl/$(KUBEBUILDER_RELEASE_VERSION)/$(GOOS)/$(GOARCH) | tar -xz -C /tmp/
# Move to test/bin
mv /tmp/kubebuilder*/bin/* test/bin/
vendor: FORCE
go mod tidy
go mod vendor
go mod verify

clean: FORCE
rm -rf -- build test/bin

vendor: FORCE
$(GO) mod tidy -v
$(GO) mod vendor
$(GO) mod verify
git clean -dxf build

.PHONY: FORCE
36 changes: 36 additions & 0 deletions Makefile.maker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
binaries:
- name: absent-metrics-operator
fromPackage: .
installTo: bin/

vendoring:
enabled: true

staticCheck:
golangciLint: true

variables:
GO_LDFLAGS: "-X main.version=$(VERSION) -X main.commit=$(COMMIT_HASH) -X main.date=$(BUILD_DATE)"

verbatim: |
VERSION := $(shell git describe --abbrev=7)
COMMIT_HASH := $(shell git rev-parse --verify HEAD)
BUILD_DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%S%Z")
GOOS = $(shell go env GOOS)
GOARCH = $(shell go env GOARCH)
KUBEBUILDER_RELEASE_VERSION = $(shell cat test/.kubebuilder-version)
test/bin:
@mkdir $@
# Download kubebuilder and extract it to tmp
curl -L https://go.kubebuilder.io/dl/$(KUBEBUILDER_RELEASE_VERSION)/$(GOOS)/$(GOARCH) | tar -xz -C /tmp/
# Move to test/bin
mv /tmp/kubebuilder*/bin/* test/bin/
# attach `make test/bin` as a dependency to the autogenerated `make build/cover.out`
build/cover.out: test/bin
build/release-info: CHANGELOG.md
@mkdir -p build
go run $(GO_BUILDFLAGS) tools/releaseinfo/main.go $< $(shell git describe --tags --abbrev=0) > $@

0 comments on commit f42f1de

Please sign in to comment.