Add version flag

This commit is contained in:
timoxa0 2024-06-21 18:15:23 +05:00
parent dfd5cee2fc
commit 4a28684c48
2 changed files with 12 additions and 8 deletions

View file

@ -1,8 +1,10 @@
# Define variables # Define variables
NAME = lon-tool NAME = lon-tool
GOFLAGS ?= -ldflags="-s -w"
SRC ?= main.go SRC ?= main.go
BIN_DIR ?= ./bin BIN_DIR ?= ./bin
GIT_VERSION := $(shell git describe --abbrev=4 --dirty --always --tags)
GOFLAGS ?= -ldflags="-s -w -X lon-tool/cmd.version=$(GIT_VERSION)"
WINDOWS_GOFLAGS ?= -ldflags="-extldflags=-static -s -w -X lon-tool/cmd.version=$(GIT_VERSION)"
# macOS settings # macOS settings
MACOS_BIN = $(BIN_DIR)/$(NAME)_mac_amd64 MACOS_BIN = $(BIN_DIR)/$(NAME)_mac_amd64
@ -29,24 +31,24 @@ LINUX_CGO_LDFLAGS = -L/usr/local/x86_64-linux-gnu/lib
all: macos windows linux all: macos windows linux
macos: macos:
echo "Building macos bin"
@export CGO_ENABLED=1 GOARCH=amd64 GOOS=darwin CC=$(MACOS_CC) CXX=$(MACOS_CXX) && \ @export CGO_ENABLED=1 GOARCH=amd64 GOOS=darwin CC=$(MACOS_CC) CXX=$(MACOS_CXX) && \
echo "Building macos bin" && \
go build $(GOFLAGS) -o $(MACOS_BIN) $(SRC) && \ go build $(GOFLAGS) -o $(MACOS_BIN) $(SRC) && \
echo "- saved to $(MACOS_BIN)" echo "- saved to $(MACOS_BIN)"
windows: windows:
echo "Building windows bin"
@export CGO_ENABLED=1 GOARCH=amd64 GOOS=windows CC=$(WINDOWS_CC) CXX=$(WINDOWS_CXX) \ @export CGO_ENABLED=1 GOARCH=amd64 GOOS=windows CC=$(WINDOWS_CC) CXX=$(WINDOWS_CXX) \
PKG_CONFIG_PATH=$(WINDOWS_PKG_CONFIG_PATH) CGO_CFLAGS=$(WINDOWS_CGO_CFLAGS) \ PKG_CONFIG_PATH=$(WINDOWS_PKG_CONFIG_PATH) CGO_CFLAGS=$(WINDOWS_CGO_CFLAGS) \
CGO_LDFLAGS=$(WINDOWS_CGO_LDFLAGS) && \ CGO_LDFLAGS=$(WINDOWS_CGO_LDFLAGS) && \
echo "Building windows bin" && \ go build $(WINDOWS_GOFLAGS) -o $(WINDOWS_BIN) $(SRC) && \
go build $(GOFLAGS) -o $(WINDOWS_BIN) -ldflags="-extldflags=-static" $(SRC) && \
echo "- saved to $(WINDOWS_BIN)" echo "- saved to $(WINDOWS_BIN)"
linux: linux:
echo "Building linux bin"
@export CGO_ENABLED=1 GOARCH=amd64 GOOS=linux CC=$(LINUX_CC) CXX=$(LINUX_CXX) \ @export CGO_ENABLED=1 GOARCH=amd64 GOOS=linux CC=$(LINUX_CC) CXX=$(LINUX_CXX) \
PKG_CONFIG_PATH=$(LINUX_PKG_CONFIG_PATH) CGO_CFLAGS=$(LINUX_CGO_CFLAGS) \ PKG_CONFIG_PATH=$(LINUX_PKG_CONFIG_PATH) CGO_CFLAGS=$(LINUX_CGO_CFLAGS) \
CGO_LDFLAGS=$(LINUX_CGO_LDFLAGS) && \ CGO_LDFLAGS=$(LINUX_CGO_LDFLAGS) && \
echo "Building linux bin" && \
go build $(GOFLAGS) -o $(LINUX_BIN) $(SRC) && \ go build $(GOFLAGS) -o $(LINUX_BIN) $(SRC) && \
echo "- saved to $(LINUX_BIN)" echo "- saved to $(LINUX_BIN)"

View file

@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var version string
var logger pterm.Logger var logger pterm.Logger
var pbar pterm.ProgressbarPrinter var pbar pterm.ProgressbarPrinter
var spinner pterm.SpinnerPrinter var spinner pterm.SpinnerPrinter
@ -34,6 +35,7 @@ var rootCmd = &cobra.Command{
Use: "lon-tool", Use: "lon-tool",
Short: "A tool for installing linux on nabu and managing linux images", Short: "A tool for installing linux on nabu and managing linux images",
Long: "A tool for installing linux on nabu and managing linux images", Long: "A tool for installing linux on nabu and managing linux images",
Version: version,
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
if verbose { if verbose {
logger = *pterm.DefaultLogger.WithLevel(pterm.LogLevelDebug).WithTime(false) logger = *pterm.DefaultLogger.WithLevel(pterm.LogLevelDebug).WithTime(false)