Skip to content

Commit 3130bf5

Browse files
committed
try out versioning for go install
1 parent e7b871d commit 3130bf5

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

cmd/dbos/version.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,54 @@
11
package main
22

33
import (
4+
"runtime/debug"
5+
46
"github.com/spf13/cobra"
57
)
68

7-
// Version will be set at build time using ldflags
8-
var Version = "dev"
9+
var (
10+
Version = "dev" // overridden by -ldflags in CI releases
11+
Commit = ""
12+
BuiltAt = ""
13+
)
14+
15+
func init() {
16+
if info, ok := debug.ReadBuildInfo(); ok {
17+
// If built via `go install module/[email protected]`, use the module version.
18+
if Version == "dev" && info.Main.Version != "" && info.Main.Version != "(devel)" {
19+
Version = info.Main.Version
20+
}
21+
for _, s := range info.Settings {
22+
switch s.Key {
23+
case "vcs.revision":
24+
if Commit == "" {
25+
if len(s.Value) >= 7 {
26+
Commit = s.Value[:7]
27+
} else {
28+
Commit = s.Value
29+
}
30+
}
31+
case "vcs.time":
32+
if BuiltAt == "" {
33+
BuiltAt = s.Value
34+
}
35+
case "vcs.modified":
36+
if s.Value == "true" && Commit != "" {
37+
Commit += "-dirty"
38+
}
39+
}
40+
}
41+
}
42+
}
943

1044
var versionCmd = &cobra.Command{
1145
Use: "version",
1246
Short: "Show the version and exit",
1347
RunE: func(cmd *cobra.Command, args []string) error {
1448
if jsonOutput {
1549
return outputJSON(map[string]string{"version": Version})
16-
} else {
17-
logger.Info("DBOS CLI version", "version", Version)
1850
}
51+
logger.Info("DBOS CLI version", "version", Version, "commit", Commit, "built", BuiltAt)
1952
return nil
2053
},
21-
}
54+
}

0 commit comments

Comments
 (0)