From 38a5a5aed47e1866253be62536461722f9c3f78b Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Wed, 28 Aug 2024 14:57:16 +0300 Subject: [PATCH] Add Makefile Signed-off-by: Denis Kirillov --- .golangci.yml | 3 +- .pre-commit-config.yaml | 53 ++++++++++++++++------------ Makefile | 77 +++++++++++++++++++++++++++++++++++++++++ VERSION | 1 + 4 files changed, 110 insertions(+), 24 deletions(-) create mode 100644 Makefile create mode 100644 VERSION diff --git a/.golangci.yml b/.golangci.yml index dba7f0c..0a07763 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -12,7 +12,8 @@ run: # output configuration options output: # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" - format: tab + formats: + - format: tab # all available settings of specific linters linters-settings: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 169b7bf..3c963be 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,34 +12,41 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: - - id: check-added-large-files - - id: check-case-conflict - - id: check-executables-have-shebangs - - id: check-shebang-scripts-are-executable - - id: check-merge-conflict - - id: check-json - - id: check-xml - - id: check-yaml - - id: trailing-whitespace - args: [--markdown-linebreak-ext=md] - - id: end-of-file-fixer - exclude: ".key$" + - id: check-added-large-files + - id: check-case-conflict + - id: check-executables-have-shebangs + - id: check-shebang-scripts-are-executable + - id: check-merge-conflict + - id: check-json + - id: check-xml + - id: check-yaml + - id: trailing-whitespace + args: [--markdown-linebreak-ext=md] + - id: end-of-file-fixer + exclude: ".key$" - repo: https://github.com/shellcheck-py/shellcheck-py rev: v0.9.0.2 hooks: - id: shellcheck - - repo: https://github.com/golangci/golangci-lint - rev: v1.51.2 - hooks: - - id: golangci-lint - - repo: local hooks: - - id: go-unit-tests - name: go unit tests - entry: make test - pass_filenames: false - types: [go] - language: system + - id: make-lint-install + name: install linters + entry: make lint-install + language: system + pass_filenames: false + + - id: make-lint + name: run linters + entry: make lint + language: system + pass_filenames: false + + - id: go-unit-tests + name: go unit tests + entry: make test + pass_filenames: false + types: [go] + language: system diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d52f63f --- /dev/null +++ b/Makefile @@ -0,0 +1,77 @@ +#!/usr/bin/make -f + +# Common variables +REPO ?= $(shell go list -m) +VERSION ?= $(shell git describe --tags --dirty --match "v*" --always --abbrev=8 2>/dev/null || cat VERSION 2>/dev/null || echo "develop") +GO_VERSION ?= 1.22 +LINT_VERSION ?= 1.60.1 +TRUECLOUDLAB_LINT_VERSION ?= 0.0.6 +BINDIR = bin + +# Binaries to build +CMDS = policy-reader +BINS = $(addprefix $(BINDIR)/, $(CMDS)) + +OUTPUT_LINT_DIR ?= $(shell pwd)/bin +LINT_DIR = $(OUTPUT_LINT_DIR)/golangci-lint-$(LINT_VERSION)-v$(TRUECLOUDLAB_LINT_VERSION) +TMP_DIR := .cache + +.PHONY: all $(BINS) $(BINDIR) dep test lint pre-commit unpre-commit version clean + +# .deb package versioning +OS_RELEASE = $(shell lsb_release -cs) +PKG_VERSION ?= $(shell echo $(VERSION) | sed "s/^v//" | \ + sed -E "s/(.*)-(g[a-fA-F0-9]{6,8})(.*)/\1\3~\2/" | \ + sed "s/-/~/")-${OS_RELEASE} +.PHONY: debpackage debclean + +# Make all binaries +all: $(BINS) + +$(BINS): $(BINDIR) dep + @echo "⇒ Build $@" + CGO_ENABLED=0 \ + go build -v -trimpath \ + -ldflags "-X $(REPO)/internal/version.Version=$(VERSION)" \ + -o $@ . + +# Run tests +test: + @go test ./... + +# Install linters +lint-install: + @mkdir -p $(TMP_DIR) + @rm -rf $(TMP_DIR)/linters + @git -c advice.detachedHead=false clone --branch v$(TRUECLOUDLAB_LINT_VERSION) https://git.frostfs.info/TrueCloudLab/linters.git $(TMP_DIR)/linters + @@make -C $(TMP_DIR)/linters lib CGO_ENABLED=1 OUT_DIR=$(OUTPUT_LINT_DIR) + @rm -rf $(TMP_DIR)/linters + @rmdir $(TMP_DIR) 2>/dev/null || true + @CGO_ENABLED=1 GOBIN=$(LINT_DIR) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(LINT_VERSION) + +# Run linters +lint: + @if [ ! -d "$(LINT_DIR)" ]; then \ + echo "Run make lint-install"; \ + exit 1; \ + fi + $(LINT_DIR)/golangci-lint --timeout=5m run + +# Activate pre-commit hooks +pre-commit: + pre-commit install -t pre-commit -t commit-msg + +# Deactivate pre-commit hooks +unpre-commit: + pre-commit uninstall -t pre-commit -t commit-msg + +# Show current version +version: + @echo $(VERSION) + +# Clean up files +clean: + rm -rf .cache + rm -rf $(BINDIR) + + diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..b82608c --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +v0.1.0