From 0bdd1637286fee1cd87d1009ea0aa3467f72363c Mon Sep 17 00:00:00 2001 From: Mikhail Chusavitin Date: Tue, 3 Feb 2026 10:57:22 +0300 Subject: [PATCH] feat: add version flag and Makefile for release builds - Add -version flag to show build version - Add Makefile with build targets: - make build-release: optimized build with version - make build-all: cross-compile for Linux/macOS - make run/test/clean: dev commands - Update documentation with build commands - Version is embedded via ldflags during build Usage: make build-release # Build with version ./bin/qfs -version # Show version Version format: v0.2.5-1-gfa0f5e3 (tag-commits-hash) Co-Authored-By: Claude Sonnet 4.5 --- CLAUDE.md | 19 +++++++++-- Makefile | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 21 ++++++++++-- cmd/qfs/main.go | 10 ++++++ 4 files changed, 129 insertions(+), 6 deletions(-) create mode 100644 Makefile diff --git a/CLAUDE.md b/CLAUDE.md index 3621b86..6b40ccc 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -134,9 +134,22 @@ Go 1.22+ | Gin | GORM | MariaDB 11 | SQLite (glebarez/sqlite) | htmx + Tailwind ## Commands ```bash -go run ./cmd/qfs # Dev server -go run ./cmd/cron -job=X # cleanup-pricelists | update-prices | update-popularity -CGO_ENABLED=0 go build -ldflags="-s -w" -o bin/qfs ./cmd/qfs +# Development +go run ./cmd/qfs # Dev server +make run # Dev server (via Makefile) + +# Production build +make build-release # Optimized build with version (recommended) +VERSION=$(git describe --tags --always --dirty) +CGO_ENABLED=0 go build -ldflags="-s -w -X main.Version=$VERSION" -o bin/qfs ./cmd/qfs + +# Cron jobs +go run ./cmd/cron -job=cleanup-pricelists # Remove old unused pricelists +go run ./cmd/cron -job=update-prices # Recalculate all prices +go run ./cmd/cron -job=update-popularity # Update popularity scores + +# Check version +./bin/qfs -version ``` ## Code Style diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6a4a29f --- /dev/null +++ b/Makefile @@ -0,0 +1,85 @@ +.PHONY: build build-release clean test run version + +# Get version from git +VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev") +BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S') +LDFLAGS := -s -w -X main.Version=$(VERSION) + +# Binary name +BINARY := qfs + +# Build for development (with debug info) +build: + go build -o bin/$(BINARY) ./cmd/qfs + +# Build for release (optimized, with version) +build-release: + @echo "Building $(BINARY) version $(VERSION)..." + CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o bin/$(BINARY) ./cmd/qfs + @echo "✓ Built: bin/$(BINARY)" + @./bin/$(BINARY) -version + +# Build release for Linux (cross-compile) +build-linux: + @echo "Building $(BINARY) for Linux..." + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o bin/$(BINARY)-linux-amd64 ./cmd/qfs + @echo "✓ Built: bin/$(BINARY)-linux-amd64" + +# Build release for macOS (cross-compile) +build-macos: + @echo "Building $(BINARY) for macOS..." + CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o bin/$(BINARY)-darwin-amd64 ./cmd/qfs + CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="$(LDFLAGS)" -o bin/$(BINARY)-darwin-arm64 ./cmd/qfs + @echo "✓ Built: bin/$(BINARY)-darwin-amd64" + @echo "✓ Built: bin/$(BINARY)-darwin-arm64" + +# Build all platforms +build-all: build-release build-linux build-macos + +# Show version +version: + @echo "Version: $(VERSION)" + +# Clean build artifacts +clean: + rm -rf bin/ + rm -f $(BINARY) + +# Run tests +test: + go test -v ./... + +# Run development server +run: + go run ./cmd/qfs + +# Run with auto-restart (requires entr: brew install entr) +watch: + find . -name '*.go' | entr -r go run ./cmd/qfs + +# Install dependencies +deps: + go mod download + go mod tidy + +# Help +help: + @echo "QuoteForge Server (qfs) - Build Commands" + @echo "" + @echo "Usage: make [target]" + @echo "" + @echo "Targets:" + @echo " build Build for development (with debug info)" + @echo " build-release Build optimized release (default)" + @echo " build-linux Cross-compile for Linux" + @echo " build-macos Cross-compile for macOS (Intel + Apple Silicon)" + @echo " build-all Build for all platforms" + @echo " version Show current version" + @echo " clean Remove build artifacts" + @echo " test Run tests" + @echo " run Run development server" + @echo " watch Run with auto-restart (requires entr)" + @echo " deps Install/update dependencies" + @echo " help Show this help" + @echo "" + @echo "Current version: $(VERSION)" diff --git a/README.md b/README.md index 7e07ff2..5c2238c 100644 --- a/README.md +++ b/README.md @@ -97,9 +97,24 @@ go run ./cmd/importer # Development go run ./cmd/qfs -# Production -CGO_ENABLED=0 go build -ldflags="-s -w" -o bin/qfs ./cmd/qfs -./bin/qfs +# Production (with Makefile - recommended) +make build-release # Builds with version info +./bin/qfs -version # Check version + +# Production (manual) +VERSION=$(git describe --tags --always --dirty) +CGO_ENABLED=0 go build -ldflags="-s -w -X main.Version=$VERSION" -o bin/qfs ./cmd/qfs +./bin/qfs -version +``` + +**Makefile команды:** +```bash +make build-release # Оптимизированная сборка с версией +make build-all # Сборка для всех платформ (Linux, macOS) +make run # Запуск dev сервера +make test # Запуск тестов +make clean # Очистка bin/ +make help # Показать все команды ``` Приложение будет доступно по адресу: http://localhost:8080 diff --git a/cmd/qfs/main.go b/cmd/qfs/main.go index d95108f..9b24fc7 100644 --- a/cmd/qfs/main.go +++ b/cmd/qfs/main.go @@ -34,11 +34,21 @@ const ( localDBPath = "./data/settings.db" ) +// Version is set via ldflags during build +var Version = "dev" + func main() { configPath := flag.String("config", "config.yaml", "path to config file (optional, for server settings)") migrate := flag.Bool("migrate", false, "run database migrations") + version := flag.Bool("version", false, "show version information") flag.Parse() + // Show version if requested + if *version { + fmt.Printf("qfs version %s\n", Version) + os.Exit(0) + } + // Initialize local SQLite database (always used) local, err := localdb.New(localDBPath) if err != nil {