From a8472d228456e21069b7aec0d933b7802434e49c Mon Sep 17 00:00:00 2001 From: bartoszmajsak Date: Thu, 23 Jan 2025 16:54:04 +0100 Subject: [PATCH] fix(cmd): injects version at build time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Couple of past released missed aligning harcoded `main.version` variable with the current tag. This results in confusing output of `gci --version`, where e.g. last release (tagged with `v0.13.5`) prints `0.13.4`. ```shell ❯ go install github.com/daixiang0/gci@v0.13.5 ❯ gci --version gci version 0.13.4 ``` With this change, `main.Version` is calculated at build time and injected. Therefore it will always contain the tag that it was built from (or `devel` if it's built from untagged commit). ```shell ❯ make build ❯ ./dist/gci -v gci version devel ❯ git tag v0.13.13 ❯ make build BIN_OUTPUT: dist/gci ❯ ./dist/gci -v gci version 0.13.13 ``` With the upcoming `go1.24` release this information can be obtained from `runtime/debug.BuildInfo.Main.Version` [1]. This will simplify the process even further. [1] https://github.com/golang/go/issues/50603#issuecomment-2181188811 Signed-off-by: bartoszmajsak --- Makefile | 12 +++++++++++- main.go | 8 ++++---- pkg/section/standard_list.go | 8 +++++++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index c5b8ac7..057f385 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,18 @@ clean: @echo BIN_OUTPUT: ${BIN_OUTPUT} @rm -rf dist cover.out +VERSION ?= $(shell git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") +GIT_TAG := $(shell git describe --tags --abbrev=0 --exact-match > /dev/null 2>&1; echo $$?) + +ifneq ($(GIT_TAG),0) + ifeq ($(origin VERSION),file) + VERSION := devel + endif +endif + +LDFLAGS ?= -w -X main.Version=${VERSION} build: clean - @go build -v -trimpath -o ${BIN_OUTPUT} . + @go build -v -trimpath -ldflags "${LDFLAGS}" -o ${BIN_OUTPUT} . test: clean @go test -v -count=1 -cover ./... diff --git a/main.go b/main.go index 07a2d3e..8597c4e 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,15 @@ package main import ( - "os" - "github.com/daixiang0/gci/cmd/gci" + "os" + "strings" ) -var version = "0.13.4" +var Version = "0.0.0" func main() { - e := gci.NewExecutor(version) + e := gci.NewExecutor(strings.TrimPrefix(Version, "v")) err := e.Execute() if err != nil { diff --git a/pkg/section/standard_list.go b/pkg/section/standard_list.go index 5a2dcdc..b223519 100644 --- a/pkg/section/standard_list.go +++ b/pkg/section/standard_list.go @@ -1,6 +1,6 @@ package section -// Code generated based on go1.23.0 X:boringcrypto,arenas. DO NOT EDIT. +// Code generated based on go1.24rc1 X:boringcrypto,arenas. DO NOT EDIT. var standardPackages = map[string]struct{}{ "archive/tar": {}, @@ -28,13 +28,18 @@ var standardPackages = map[string]struct{}{ "crypto/ecdsa": {}, "crypto/ed25519": {}, "crypto/elliptic": {}, + "crypto/fips140": {}, + "crypto/hkdf": {}, "crypto/hmac": {}, "crypto/md5": {}, + "crypto/mlkem": {}, + "crypto/pbkdf2": {}, "crypto/rand": {}, "crypto/rc4": {}, "crypto/rsa": {}, "crypto/sha1": {}, "crypto/sha256": {}, + "crypto/sha3": {}, "crypto/sha512": {}, "crypto/subtle": {}, "crypto/tls": {}, @@ -172,4 +177,5 @@ var standardPackages = map[string]struct{}{ "unicode/utf8": {}, "unique": {}, "unsafe": {}, + "weak": {}, }