|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
| 6 | + "os" |
| 7 | + "runtime" |
| 8 | + "runtime/debug" |
5 | 9 |
|
6 | 10 | "github.com/spf13/cobra" |
7 | 11 | ) |
8 | 12 |
|
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 | +} |
10 | 64 |
|
11 | 65 | // versionCmd represents the version command. |
12 | 66 | var versionCmd = &cobra.Command{ |
13 | 67 | 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) |
18 | 100 | }, |
19 | 101 | } |
20 | 102 |
|
21 | 103 | func init() { |
| 104 | + versionCmd.Flags().BoolP("json", "j", false, "output in JSON format") |
| 105 | + versionCmd.Flags().BoolP("short", "s", false, "output version only") |
22 | 106 | rootCmd.AddCommand(versionCmd) |
23 | 107 | } |
0 commit comments