Makefile: add targets install-go-deps check-deps

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
Hubertbits 2025-04-12 00:13:47 +02:00 committed by yxxhero
parent 1715e3e3a3
commit a864f788ab
1 changed files with 60 additions and 2 deletions

View File

@ -1,14 +1,16 @@
ORG ?= $(shell basename $(realpath ..)) ORG ?= $(shell basename $(realpath ..))
PKGS := $(shell go list ./... | grep -v /vendor/) PKGS := $(shell go list ./... | grep -v /vendor/)
INSTALL_DEPS ?= 0
TAG ?= $(shell git describe --tags --abbrev=0 HEAD) TAG ?= $(shell git describe --tags --abbrev=0 HEAD)
LAST = $(shell git describe --tags --abbrev=0 HEAD^) LAST = $(shell git describe --tags --abbrev=0 HEAD^)
BODY = "`git log ${LAST}..HEAD --oneline --decorate` `printf '\n\#\#\# [Build Info](${BUILD_URL})'`" BODY = "`git log ${LAST}..HEAD --oneline --decorate` `printf '\n\#\#\# [Build Info](${BUILD_URL})'`"
DATE_FMT = +"%Y-%m-%dT%H:%M:%S%z" DATE_FMT = +"%Y-%m-%dT%H:%M:%S%z"
ifdef SOURCE_DATE_EPOCH ifdef SOURCE_DATE_EPOCH
BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u "$(DATE_FMT)") BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u "$(DATE_FMT)")
else else
BUILD_DATE ?= $(shell date "$(DATE_FMT)") BUILD_DATE ?= $(shell date "$(DATE_FMT)")
endif endif
@ -20,6 +22,62 @@ GO_BUILD_VERSION_LDFLAGS=\
-X go.szostok.io/version.commitDate=$(shell git log -1 --date=format:"%Y-%m-%dT%H:%M:%S%z" --format=%cd) \ -X go.szostok.io/version.commitDate=$(shell git log -1 --date=format:"%Y-%m-%dT%H:%M:%S%z" --format=%cd) \
-X go.szostok.io/version.dirtyBuild=false -X go.szostok.io/version.dirtyBuild=false
# Define a function to check if a command exists
# Usage: $(call check_command,command_name,installation_instructions)
# Returns 0 if command exists, 1 if not
define check_command
command -v $(1) >/dev/null
endef
# Install all required Go tools
install-go-deps:
@echo "Installing Go dependencies..."
@go install github.com/daixiang0/gci@latest
@go install github.com/tcnksm/ghr@latest
@go install github.com/mitchellh/gox@latest
@echo "Go dependencies installed successfully"
.PHONY: install-go-deps
# Check for all required dependencies
check-deps:
@echo "Checking dependencies..."
@missing_deps="" ; \
missing_go_tools="" ; \
$(call check_command,go,https://golang.org/doc/install) || missing_deps="$$missing_deps\n- go: install from https://golang.org/doc/install" ; \
$(call check_command,gci,) || missing_go_tools="$$missing_go_tools gci" ; \
$(call check_command,ghr,) || missing_go_tools="$$missing_go_tools ghr" ; \
$(call check_command,gox,) || missing_go_tools="$$missing_go_tools gox" ; \
$(call check_command,curl,https://curl.se/docs/install.html) || missing_deps="$$missing_deps\n- curl: install using your system package manager" ; \
$(call check_command,docker,https://docs.docker.com/get-docker/) || missing_deps="$$missing_deps\n- docker: install from https://docs.docker.com/get-docker/" ; \
$(call check_command,git,https://git-scm.com/downloads) || missing_deps="$$missing_deps\n- git: install from https://git-scm.com/downloads" ; \
$(call check_command,bash,) || missing_deps="$$missing_deps\n- bash: install using your system package manager" ; \
if [ -n "$$missing_go_tools" ]; then \
if [ -n "$$missing_deps" ]; then \
printf "\nCannot install Go tools because Go itself is missing.\n" ; \
elif [ -n "$$INSTALL_DEPS" ]; then \
echo "Installing missing Go tools:$$missing_go_tools" ; \
for tool in $$missing_go_tools; do \
case $$tool in \
gci) go install github.com/daixiang0/gci@latest ;; \
ghr) go install github.com/tcnksm/ghr@latest ;; \
gox) go install github.com/mitchellh/gox@latest ;; \
esac ; \
done ; \
echo "Go tools installed successfully" ; \
else \
printf "\nMissing Go tools:$$missing_go_tools\n" ; \
echo "Run 'make install-go-deps' to install them, or run 'make check-deps INSTALL_DEPS=1' to check and install." ; \
missing_deps="$$missing_deps\n- Go tools: run 'make install-go-deps'" ; \
fi ; \
fi ; \
if [ -n "$$missing_deps" ] && [ -z "$$INSTALL_DEPS" ]; then \
printf "\nMissing dependencies:$$missing_deps\n\n" ; \
echo "Please install the missing dependencies and try again." ; \
exit 1 ; \
else \
echo "All dependencies are available" ; \
fi
.PHONY: check-deps
build: build:
go build -ldflags="$(GO_BUILD_VERSION_LDFLAGS)" ${TARGETS} go build -ldflags="$(GO_BUILD_VERSION_LDFLAGS)" ${TARGETS}