Skip to content

Commit 2d3c761

Browse files
fix: inject version via ldflags instead of hardcoded const
Replace `const version = "0.1.0"` with `var version = "dev"` injectable via `-ldflags -X`. Add version injection in Makefile (git describe) and .goreleaser.yaml ({{ .Version }}). Add regression tests for --version passthrough after command name. Closes #17
1 parent b953996 commit 2d3c761

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

.goreleaser.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ gomod:
66
builds:
77
- main: ./cmd/snip
88
binary: snip
9+
ldflags:
10+
- -s -w -X github.com/edouard-claude/snip/internal/cli.version={{ .Version }}
911
env:
1012
- CGO_ENABLED=0
1113
goos:

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
BINARY=snip
44
BUILD_DIR=cmd/snip
5-
LDFLAGS=-ldflags="-s -w"
5+
VERSION=$(shell git describe --tags --always 2>/dev/null | sed 's/^v//' || echo dev)
6+
LDFLAGS=-ldflags="-s -w -X 'github.com/edouard-claude/snip/internal/cli.version=$(VERSION)'"
67

78
build:
89
CGO_ENABLED=0 go build -o $(BINARY) $(LDFLAGS) ./$(BUILD_DIR)

internal/cli/cli.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
"github.com/edouard-claude/snip/internal/tracking"
1515
)
1616

17-
const version = "0.1.0"
17+
// version is set at build time via -ldflags "-X ...". Do not reassign.
18+
var version = "dev"
1819

1920
// Run is the main entry point. Returns exit code.
2021
func Run(args []string) int {

internal/cli/flags_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ func TestParseFlags(t *testing.T) {
4848
wantFlags: Flags{Help: true},
4949
wantArgs: nil,
5050
},
51+
{
52+
name: "version after command is passed through",
53+
args: []string{"bun", "--version"},
54+
wantFlags: Flags{},
55+
wantArgs: []string{"bun", "--version"},
56+
},
57+
{
58+
name: "global flags with version after command",
59+
args: []string{"-v", "go", "--version"},
60+
wantFlags: Flags{Verbose: 1},
61+
wantArgs: []string{"go", "--version"},
62+
},
5163
{
5264
name: "built-in command help is preserved",
5365
args: []string{"proxy", "--help"},

0 commit comments

Comments
 (0)