fix(cli): report the real version for go-install builds (#47)

go install github.com/runbear-io/beardrive/cmd/bdrive@latest skips the
release ldflags, so every module-built binary claimed to be 0.1.0-dev —
useless in beta bug reports. Fall back to the module version Go stamps
into the binary (debug.ReadBuildInfo) when ldflags didn't set one.

Found during the BEA-33 fresh-machine quickstart check.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Snow W. Lee (Sungwon)
2026-07-27 07:11:58 +09:00
committed by GitHub
co-authored by Claude Fable 5
parent 379b19a9ec
commit 11d667a447
+16 -2
View File
@@ -6,6 +6,8 @@ package main
import (
"fmt"
"os"
"runtime/debug"
"strings"
"github.com/spf13/cobra"
@@ -13,8 +15,20 @@ import (
)
// version is set at release time via -ldflags "-X main.version=...".
// `go install …@vX.Y.Z` builds skip ldflags, so fall back to the module
// version Go stamps into the binary.
var version = "0.1.0-dev"
func resolvedVersion() string {
if version != "0.1.0-dev" {
return version
}
if bi, ok := debug.ReadBuildInfo(); ok && bi.Main.Version != "" && bi.Main.Version != "(devel)" {
return strings.TrimPrefix(bi.Main.Version, "v")
}
return version
}
func main() {
root := &cobra.Command{
Use: "bdrive",
@@ -28,7 +42,7 @@ Cloud). Every change is journaled — you can always see which device and
author changed which file, and when. Files are real files on disk, so
everything keeps working offline; changes sync when the remote is reachable.`,
SilenceUsage: true,
Version: version,
Version: resolvedVersion(),
}
root.SetVersionTemplate("beardrive {{.Version}}\n")
root.AddCommand(
@@ -61,7 +75,7 @@ func versionCmd() *cobra.Command {
Use: "version",
Short: "Print the bdrive version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("beardrive", version)
fmt.Println("beardrive", resolvedVersion())
},
}
}