Skip to content

Commit 595ed07

Browse files
committed
fix: auto-detect version from go module info for go install
1 parent 94ef6f1 commit 595ed07

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

version/version.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
package version
22

3-
// These variables will be set during build time
3+
import "runtime/debug"
4+
5+
// These variables will be set during build time via ldflags.
6+
// When installed via `go install`, they fall back to Go's embedded module info.
47
var (
5-
// Version is the current version of the application
6-
Version = "dev"
7-
// Commit is the git commit SHA at build time
8-
Commit = "none"
9-
// BuildDate is the date when the binary was built
8+
Version = ""
9+
Commit = "none"
1010
BuildDate = "unknown"
1111
)
12+
13+
func init() {
14+
if Version != "" {
15+
return
16+
}
17+
info, ok := debug.ReadBuildInfo()
18+
if ok && info.Main.Version != "" && info.Main.Version != "(devel)" {
19+
Version = info.Main.Version
20+
} else {
21+
Version = "dev"
22+
}
23+
}

0 commit comments

Comments
 (0)