Skip to content

Commit 965f815

Browse files
committed
fix(version): plumb goreleaser ldflag into cobra Version
The cobra root command had `Version: "1.0.0"` hardcoded, so `git-context --version` always reported 1.0.0 regardless of which release was installed. .goreleaser.yml already injects the right value via `-X main.version={{.Version}}`, but main.go had no matching `version` variable for the ldflag to land in, and even if it did, cmd/root.go wouldn't see it. Declare `var version = "dev"` in main.go so the ldflag has a target, expose `cmd.SetVersion` to overwrite the cobra Version field at startup, and call it from main(). Local `go install` / `go build` without ldflags continues to report "dev"; goreleaser builds report the real tag. Signed-off-by: Andre Nogueira <aanogueira@protonmail.com>
1 parent 49d844b commit 965f815

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

cmd/root.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ var rootCmd = &cobra.Command{
1616
1717
Switch between different git identities (work, personal, school, etc.) with a single command.
1818
Profiles are stored in ~/.config/git-context/config.yaml`,
19-
Version: "1.0.0",
19+
Version: "dev",
20+
}
21+
22+
// SetVersion overrides the version reported by `git-context --version`.
23+
// Called from main with the value goreleaser injects via ldflag.
24+
func SetVersion(v string) {
25+
if v != "" {
26+
rootCmd.Version = v
27+
}
2028
}
2129

2230
var initCmd = &cobra.Command{

main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ import (
77
"github.com/aanogueira/git-context/cmd"
88
)
99

10+
// version is set at build time via goreleaser's
11+
// `-X main.version={{.Version}}` ldflag. Defaults to "dev" for local
12+
// `go build` / `go install` invocations.
13+
var version = "dev"
14+
1015
func main() {
16+
cmd.SetVersion(version)
17+
1118
if err := cmd.Execute(); err != nil {
1219
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
1320
os.Exit(1)

0 commit comments

Comments
 (0)