gt version: Match bd version output format
Single-line output showing version, build type, branch and commit: gt version 0.1.0 (dev: main@62413d55cbb0) Uses runtime/debug to get VCS info from build, with fallback to git commands at runtime. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,16 +2,21 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"runtime/debug"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/steveyegge/gastown/internal/style"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Version information - set at build time via ldflags
|
// Version information - set at build time via ldflags
|
||||||
var (
|
var (
|
||||||
Version = "0.1.0"
|
Version = "0.1.0"
|
||||||
BuildTime = "unknown"
|
// Build can be set via ldflags at compile time
|
||||||
GitCommit = "unknown"
|
Build = "dev"
|
||||||
|
// Commit and Branch - the git revision the binary was built from (optional ldflag)
|
||||||
|
Commit = ""
|
||||||
|
Branch = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
var versionCmd = &cobra.Command{
|
var versionCmd = &cobra.Command{
|
||||||
@@ -19,13 +24,68 @@ var versionCmd = &cobra.Command{
|
|||||||
GroupID: GroupDiag,
|
GroupID: GroupDiag,
|
||||||
Short: "Print version information",
|
Short: "Print version information",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println(style.Bold.Render("gt") + " - Gas Town CLI")
|
commit := resolveCommitHash()
|
||||||
fmt.Printf("Version: %s\n", Version)
|
branch := resolveBranch()
|
||||||
fmt.Printf("Commit: %s\n", style.Dim.Render(GitCommit))
|
|
||||||
fmt.Printf("Built: %s\n", style.Dim.Render(BuildTime))
|
if commit != "" && branch != "" {
|
||||||
|
fmt.Printf("gt version %s (%s: %s@%s)\n", Version, Build, branch, shortCommit(commit))
|
||||||
|
} else if commit != "" {
|
||||||
|
fmt.Printf("gt version %s (%s: %s)\n", Version, Build, shortCommit(commit))
|
||||||
|
} else {
|
||||||
|
fmt.Printf("gt version %s (%s)\n", Version, Build)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(versionCmd)
|
rootCmd.AddCommand(versionCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resolveCommitHash() string {
|
||||||
|
if Commit != "" {
|
||||||
|
return Commit
|
||||||
|
}
|
||||||
|
|
||||||
|
if info, ok := debug.ReadBuildInfo(); ok {
|
||||||
|
for _, setting := range info.Settings {
|
||||||
|
if setting.Key == "vcs.revision" && setting.Value != "" {
|
||||||
|
return setting.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func shortCommit(hash string) string {
|
||||||
|
if len(hash) > 12 {
|
||||||
|
return hash[:12]
|
||||||
|
}
|
||||||
|
return hash
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveBranch() string {
|
||||||
|
if Branch != "" {
|
||||||
|
return Branch
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to get branch from build info (build-time VCS detection)
|
||||||
|
if info, ok := debug.ReadBuildInfo(); ok {
|
||||||
|
for _, setting := range info.Settings {
|
||||||
|
if setting.Key == "vcs.branch" && setting.Value != "" {
|
||||||
|
return setting.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: try to get branch from git at runtime
|
||||||
|
cmd := exec.Command("git", "symbolic-ref", "--short", "HEAD")
|
||||||
|
cmd.Dir = "."
|
||||||
|
if output, err := cmd.Output(); err == nil {
|
||||||
|
if branch := strings.TrimSpace(string(output)); branch != "" && branch != "HEAD" {
|
||||||
|
return branch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user