|
| 1 | +APP_NAME := deploykf |
| 2 | + |
| 3 | +BINDIR := $(CURDIR)/bin |
| 4 | +INSTALL_PATH ?= /usr/local/bin |
| 5 | + |
| 6 | +GIT_COMMIT := $(shell git rev-parse HEAD) |
| 7 | +GIT_TAG := $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null) |
| 8 | +GIT_TREE_STATE := $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean") |
| 9 | + |
| 10 | +ifdef VERSION |
| 11 | + BINARY_VERSION = $(VERSION) |
| 12 | +endif |
| 13 | +ifeq ($(GIT_TAG),) |
| 14 | + GIT_TAG := v0.0.0 |
| 15 | +endif |
| 16 | +BINARY_VERSION ?= ${GIT_TAG} |
| 17 | + |
| 18 | +LDFLAGS := -w -s |
| 19 | +LDFLAGS += -X github.com/deployKF/cli/internal/version.version=${BINARY_VERSION} |
| 20 | +LDFLAGS += -X github.com/deployKF/cli/internal/version.gitCommit=${GIT_COMMIT} |
| 21 | +LDFLAGS += -X github.com/deployKF/cli/internal/version.gitTreeState=${GIT_TREE_STATE} |
| 22 | + |
| 23 | +TARGETS := \ |
| 24 | + linux-amd64 \ |
| 25 | + linux-arm64 \ |
| 26 | + darwin-amd64 \ |
| 27 | + darwin-arm64 \ |
| 28 | + windows-amd64 |
| 29 | + |
| 30 | +# ------------------------------------------------------------------------------ |
| 31 | +# checks |
| 32 | + |
| 33 | +.PHONY: check-golangci-lint |
| 34 | +check-golangci-lint: |
| 35 | +ifeq (, $(shell which golangci-lint)) |
| 36 | + $(error "golangci-lint is not installed. Please install it using the following command: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.52.2") |
| 37 | +endif |
| 38 | + |
| 39 | +# ------------------------------------------------------------------------------ |
| 40 | +# build |
| 41 | + |
| 42 | +.PHONY: build |
| 43 | +build: |
| 44 | + @echo "********** building $(APP_NAME) for $(shell go env GOOS)/$(shell go env GOARCH) **********" |
| 45 | + CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o '$(BINDIR)/$(APP_NAME)' |
| 46 | + |
| 47 | +# ------------------------------------------------------------------------------ |
| 48 | +# build-all |
| 49 | + |
| 50 | +.PHONY: build-all |
| 51 | +build-all: $(addprefix build-, $(TARGETS)) |
| 52 | +build-%: |
| 53 | + $(eval os_arch := $(subst -, ,$*)) |
| 54 | + $(eval GOOS := $(word 1, $(os_arch))) |
| 55 | + $(eval GOARCH := $(word 2, $(os_arch))) |
| 56 | + $(eval EXT := $(if $(filter windows,$(GOOS)),.exe,)) |
| 57 | + |
| 58 | + @echo "********** building $(APP_NAME) for $(GOOS)/$(GOARCH) **********" |
| 59 | + GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o '$(BINDIR)/$(APP_NAME)-$(GOOS)-$(GOARCH)$(EXT)' |
| 60 | + |
| 61 | +# ------------------------------------------------------------------------------ |
| 62 | +# install |
| 63 | + |
| 64 | +.PHONY: install |
| 65 | +install: build |
| 66 | + @echo "********** installing into $(INSTALL_PATH)/$(APP_NAME) **********" |
| 67 | + @install '$(BINDIR)/$(APP_NAME)' '$(INSTALL_PATH)/$(APP_NAME)' |
| 68 | + |
| 69 | +# ------------------------------------------------------------------------------ |
| 70 | +# test |
| 71 | + |
| 72 | +.PHONY: test |
| 73 | +test: |
| 74 | + @echo "********** running tests **********" |
| 75 | + @go test -race -v ./... |
| 76 | + |
| 77 | +# ------------------------------------------------------------------------------ |
| 78 | +# lint |
| 79 | + |
| 80 | +.PHONY: lint |
| 81 | +lint: check-golangci-lint |
| 82 | + @echo "********** running golangci-lint **********" |
| 83 | + @golangci-lint run ./... |
| 84 | + |
| 85 | +# ------------------------------------------------------------------------------ |
| 86 | +# lint-fix |
| 87 | + |
| 88 | +.PHONY: lint-fix |
| 89 | +lint-fix: check-golangci-lint |
| 90 | + @echo "********** running gofmt and goimports **********" |
| 91 | + @find . -name '*.go' -type f -not -path './vendor/*' -exec gofmt -s -w {} \; |
| 92 | + @goimports -local github.com/deployKF/cli -w $(shell find . -type f -name '*.go' -not -path "./vendor/*") |
| 93 | + |
| 94 | + @echo "********** running golangci-lint --fix **********" |
| 95 | + @golangci-lint run --fix ./... |
| 96 | + |
| 97 | +# ------------------------------------------------------------------------------ |
| 98 | +# clean |
| 99 | + |
| 100 | +.PHONY: clean |
| 101 | +clean: |
| 102 | + @echo "********** cleaning up build artifacts **********" |
| 103 | + @rm -rf '$(BINDIR)' |
0 commit comments