From 5ef5501f536d7328b4f3eac40db06cb59bd3022e Mon Sep 17 00:00:00 2001 From: Viswaijth K S Date: Thu, 2 Oct 2025 19:43:55 +0530 Subject: [PATCH] feat: add dynamic version flag injection --- Makefile | 9 +++++++-- cmd/gitx/main.go | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 887f9b1..7ee442d 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,11 @@ BINARY_NAME=gitx CMD_PATH=./cmd/gitx BUILD_DIR=./build +# Versioning -- NEW SECTION +# Get the latest git tag, or fallback to "dev" if no tags are found. +VERSION := $(shell git describe --tags --abbrev=0 2>/dev/null || echo "dev") +LDFLAGS := -ldflags="-X 'main.version=$(VERSION)'" + # Default target executed when you run `make` all: build @@ -16,7 +21,7 @@ sync: build: sync @echo "Building the application..." @mkdir -p $(BUILD_DIR) - @go build -o $(BUILD_DIR)/$(BINARY_NAME) $(CMD_PATH) + @go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) $(CMD_PATH) @echo "Binary available at $(BUILD_DIR)/$(BINARY_NAME)" # Runs the application @@ -37,7 +42,7 @@ ci: # Installs the binary to /usr/local/bin install: build @echo "Installing $(BINARY_NAME)..." - @go install $(CMD_PATH) + @go install $(LDFLAGS) $(CMD_PATH) @echo "$(BINARY_NAME) installed successfully" # Cleans the build artifacts diff --git a/cmd/gitx/main.go b/cmd/gitx/main.go index 8f1583f..e6ef02b 100644 --- a/cmd/gitx/main.go +++ b/cmd/gitx/main.go @@ -4,13 +4,21 @@ import ( "errors" "fmt" "log" + "os" tea "github.com/charmbracelet/bubbletea" "github.com/gitxtui/gitx/internal/tui" zone "github.com/lrstanley/bubblezone" ) +var version = "dev" + func main() { + if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-v") { + fmt.Printf("gitx version: %s\n", version) + return + } + zone.NewGlobal() defer zone.Close()