Enhances version info retrieval
Improves the `version` command to automatically extract build details (version, commit, date) from Go's `debug.ReadBuildInfo` when `ldflags` are not provided. This allows users installing via `go install` to receive accurate version information without requiring a Makefile or explicit build flags. Updates the README to reflect this new capability.
This commit is contained in:
@@ -152,6 +152,8 @@ The Makefile automatically injects version information from git tags:
|
|||||||
- `Commit`: Short commit hash
|
- `Commit`: Short commit hash
|
||||||
- `Date`: Build timestamp
|
- `Date`: Build timestamp
|
||||||
|
|
||||||
|
**Note:** Version information is automatically included when installing via `go install` (no Makefile needed). The tool reads version info from Go's build metadata.
|
||||||
|
|
||||||
Check the version after installing:
|
Check the version after installing:
|
||||||
```bash
|
```bash
|
||||||
gitlocal version
|
gitlocal version
|
||||||
|
|||||||
@@ -2,12 +2,14 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime/debug"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Version is set via -ldflags during build
|
// Version is set via -ldflags during build (for make install)
|
||||||
|
// Falls back to module version from go install
|
||||||
Version = "dev"
|
Version = "dev"
|
||||||
// Commit is set via -ldflags during build
|
// Commit is set via -ldflags during build
|
||||||
Commit = "unknown"
|
Commit = "unknown"
|
||||||
@@ -20,9 +22,34 @@ var versionCmd = &cobra.Command{
|
|||||||
Short: "Print version information",
|
Short: "Print version information",
|
||||||
Long: `Display the version, commit hash, and build date of gitlocal.`,
|
Long: `Display the version, commit hash, and build date of gitlocal.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Printf("gitlocal version %s\n", Version)
|
version := Version
|
||||||
fmt.Printf(" Commit: %s\n", Commit)
|
commit := Commit
|
||||||
fmt.Printf(" Built: %s\n", Date)
|
date := Date
|
||||||
|
|
||||||
|
// If installed via 'go install', read version from build info
|
||||||
|
if version == "dev" {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("gitlocal version %s\n", version)
|
||||||
|
fmt.Printf(" Commit: %s\n", commit)
|
||||||
|
fmt.Printf(" Built: %s\n", date)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user