Switches to native Go build info for versioning

Simplifies the version command and Makefile by removing manual ldflags injection. The application now relies entirely on Go's built-in VCS metadata embedding to extract version, commit, and date information, ensuring accurate reporting across all build methods.
This commit is contained in:
2026-04-11 15:06:11 +01:00
parent 6e21a0798a
commit 4155d78440
3 changed files with 37 additions and 63 deletions

View File

@@ -7,42 +7,30 @@ import (
"github.com/spf13/cobra"
)
var (
// Version is set via -ldflags during build (for make install)
// Falls back to module version from go install
Version = "dev"
// Commit is set via -ldflags during build
Commit = "unknown"
// Date is set via -ldflags during build
Date = "unknown"
)
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print version information",
Long: `Display the version, commit hash, and build date of gitlocal.`,
Run: func(cmd *cobra.Command, args []string) {
version := Version
commit := Commit
date := Date
version := "dev"
commit := "unknown"
date := "unknown"
// If installed via 'go install', read version from build info
if version == "dev" {
if info, ok := debug.ReadBuildInfo(); ok {
version = info.Main.Version
// Read version from build info (works for both 'go install' and 'go build')
if info, ok := debug.ReadBuildInfo(); ok {
version = info.Main.Version
// Extract commit and date from build settings
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
if len(setting.Value) > 7 {
commit = setting.Value[:7]
} else {
commit = setting.Value
}
case "vcs.time":
date = setting.Value
// Extract commit and date from build settings
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
if len(setting.Value) > 7 {
commit = setting.Value[:7]
} else {
commit = setting.Value
}
case "vcs.time":
date = setting.Value
}
}
}