Skip to content

Commit 1342929

Browse files
committed
build: preserve version when build via go install
1 parent 1159e08 commit 1342929

6 files changed

Lines changed: 29 additions & 11 deletions

File tree

.goreleaser.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version: 2
44

55
before:
66
hooks:
7-
- go mod download
7+
- go mod tidy
88

99
builds:
1010
- env:
@@ -16,8 +16,10 @@ builds:
1616
goarch:
1717
- amd64
1818
- arm64
19+
flags:
20+
- -trimpath
1921
ldflags:
20-
- -s -w -X main.buildVersion={{.Version}} -X main.buildCommit={{.ShortCommit}} -X main.buildDate={{.Date}}
22+
- -s -w -X main.buildVersion={{.Version}} -X main.buildCommit={{.ShortCommit}} -X main.buildDate={{.Date}} -X main.builtBy=goreleaser -X main.treeState={{ .IsGitDirty }}
2123

2224
gomod:
2325
# Proxy a module from proxy.golang.org, making the builds verifiable.
@@ -33,7 +35,7 @@ checksum:
3335
archives:
3436
- format_overrides:
3537
- goos: windows
36-
formats: ["zip"]
38+
formats: [zip]
3739

3840
# disable changelog generation entirely, since bump will manage that itself!
3941
changelog:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24
44

55
require (
66
github.com/Masterminds/semver/v3 v3.3.1
7+
github.com/earthboundkid/versioninfo/v2 v2.24.1
78
github.com/go-git/go-git/v5 v5.16.2
89
github.com/google/go-github/v29 v29.0.3
910
github.com/manifoldco/promptui v0.9.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ github.com/cyphar/filepath-securejoin v0.4.1/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGL
2424
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2525
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2626
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
27+
github.com/earthboundkid/versioninfo/v2 v2.24.1 h1:SJTMHaoUx3GzjjnUO1QzP3ZXK6Ee/nbWyCm58eY3oUg=
28+
github.com/earthboundkid/versioninfo/v2 v2.24.1/go.mod h1:VcWEooDEuyUJnMfbdTh0uFN4cfEIg+kHMuWB2CDCLjw=
2729
github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o=
2830
github.com/elazarl/goproxy v1.7.2/go.mod h1:82vkLNir0ALaW14Rc399OTTjyNREgmdL2cVoIbS6XaE=
2931
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=

main.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ import (
1414
"github.com/pkg/browser"
1515
)
1616

17-
// build info set by goreleaser during production builds
18-
var (
19-
buildVersion = "dev"
20-
buildCommit = "none"
21-
buildDate = "unknown"
22-
)
23-
2417
// VerboseLogging sets whether to log debug/timing info to stderr
2518
var VerboseLogging = false
2619

options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func ParseFlags(opts *Options, args []string) (Options, *flag.FlagSet) {
8989
// (FlagSet.Parse returns "ErrHelp if -help or -h were set but not defined")
9090
_ = flags.Parse(args)
9191
if *version {
92-
fmt.Println(buildVersion)
92+
fmt.Println(Version())
9393
os.Exit(0)
9494
}
9595

version.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"cmp"
5+
6+
"github.com/earthboundkid/versioninfo/v2"
7+
)
8+
9+
// build info set by goreleaser during production builds
10+
var (
11+
buildVersion = ""
12+
buildCommit = ""
13+
buildDate = ""
14+
builtBy = ""
15+
treeState = ""
16+
)
17+
18+
func Version() string {
19+
return cmp.Or(buildVersion, versioninfo.Version)
20+
}

0 commit comments

Comments
 (0)