Skip to content

Commit 4944f65

Browse files
authored
feat(version): enhance version command with detailed build information
feat(version): enhance version command with detailed build information
2 parents a7b3ee0 + 343e801 commit 4944f65

2 files changed

Lines changed: 92 additions & 5 deletions

File tree

.goreleaser.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ builds:
66
- env:
77
- CGO_ENABLED=0
88
ldflags:
9+
- -s -w
910
- -X github.com/sgaunet/jwt-cli/cmd.version={{.Version}}
11+
- -X github.com/sgaunet/jwt-cli/cmd.commit={{.Commit}}
12+
- -X github.com/sgaunet/jwt-cli/cmd.buildDate={{.Date}}
1013
goos:
1114
- linux
1215
- darwin

cmd/version.go

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,107 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"os"
7+
"runtime"
8+
"runtime/debug"
59

610
"github.com/spf13/cobra"
711
)
812

9-
var version = "development"
13+
var (
14+
version = "development"
15+
commit = "none"
16+
buildDate = "unknown"
17+
)
18+
19+
const shortHashLength = 8
20+
21+
// VersionInfo holds detailed version and build information.
22+
type VersionInfo struct {
23+
Version string `json:"version"`
24+
Commit string `json:"commit"`
25+
BuildDate string `json:"build_date"`
26+
GoVersion string `json:"go_version"`
27+
Platform string `json:"platform"`
28+
Compiler string `json:"compiler"`
29+
}
30+
31+
// getVersionInfo returns detailed version and build information.
32+
func getVersionInfo() VersionInfo {
33+
info := VersionInfo{
34+
Version: version,
35+
Commit: commit,
36+
BuildDate: buildDate,
37+
GoVersion: runtime.Version(),
38+
Platform: runtime.GOOS + "/" + runtime.GOARCH,
39+
Compiler: runtime.Compiler,
40+
}
41+
42+
// Try to get build info from runtime (useful for local development)
43+
bi, ok := debug.ReadBuildInfo()
44+
if !ok {
45+
return info
46+
}
47+
48+
for _, setting := range bi.Settings {
49+
if setting.Key == "vcs.revision" && info.Commit == "none" {
50+
// Use short hash (first 8 chars)
51+
if len(setting.Value) >= shortHashLength {
52+
info.Commit = setting.Value[:shortHashLength]
53+
} else {
54+
info.Commit = setting.Value
55+
}
56+
}
57+
if setting.Key == "vcs.time" && info.BuildDate == "unknown" {
58+
info.BuildDate = setting.Value
59+
}
60+
}
61+
62+
return info
63+
}
1064

1165
// versionCmd represents the version command.
1266
var versionCmd = &cobra.Command{
1367
Use: "version",
14-
Short: "print version of jwt-cli",
15-
Long: `print version of jwt-cli`,
16-
Run: func(_ *cobra.Command, _ []string) {
17-
fmt.Println(version)
68+
Short: "Print version information",
69+
Long: `Print detailed version and build information for jwt-cli.
70+
71+
Use --json flag for machine-readable output.
72+
Use --short flag for compact output (version only).`,
73+
Run: func(cmd *cobra.Command, _ []string) {
74+
info := getVersionInfo()
75+
76+
jsonOutput, _ := cmd.Flags().GetBool("json")
77+
shortOutput, _ := cmd.Flags().GetBool("short")
78+
79+
if shortOutput {
80+
fmt.Println(info.Version)
81+
return
82+
}
83+
84+
if jsonOutput {
85+
enc := json.NewEncoder(os.Stdout)
86+
enc.SetIndent("", " ")
87+
if err := enc.Encode(info); err != nil {
88+
fmt.Fprintf(os.Stderr, "Error encoding JSON: %v\n", err)
89+
}
90+
return
91+
}
92+
93+
// Default format
94+
fmt.Printf("jwt-cli version: %s\n", info.Version)
95+
fmt.Printf("Git commit: %s\n", info.Commit)
96+
fmt.Printf("Build date: %s\n", info.BuildDate)
97+
fmt.Printf("Go version: %s\n", info.GoVersion)
98+
fmt.Printf("Platform: %s\n", info.Platform)
99+
fmt.Printf("Compiler: %s\n", info.Compiler)
18100
},
19101
}
20102

21103
func init() {
104+
versionCmd.Flags().BoolP("json", "j", false, "output in JSON format")
105+
versionCmd.Flags().BoolP("short", "s", false, "output version only")
22106
rootCmd.AddCommand(versionCmd)
23107
}

0 commit comments

Comments
 (0)