|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "fmt"
|
| 6 | + "os" |
5 | 7 |
|
6 | 8 | "github.com/appleboy/CodeGPT/version"
|
| 9 | + |
| 10 | + "github.com/fatih/color" |
7 | 11 | "github.com/spf13/cobra"
|
8 | 12 | )
|
9 | 13 |
|
| 14 | +/* |
| 15 | +VersionInfo holds all version-related information for the application. |
| 16 | +*/ |
| 17 | +type VersionInfo struct { |
| 18 | + Version string `json:"version"` // Application version |
| 19 | + GitCommit string `json:"git_commit"` // Git commit SHA |
| 20 | + BuildTime string `json:"build_time"` // Build timestamp |
| 21 | + GoVersion string `json:"go_version"` // Go language version |
| 22 | + BuildOS string `json:"build_os"` // Build operating system |
| 23 | + BuildArch string `json:"build_arch"` // Build architecture |
| 24 | + Platform string `json:"platform"` // Combined OS/Arch string |
| 25 | +} |
| 26 | + |
| 27 | +var outputFormat string |
| 28 | + |
10 | 29 | var versionCmd = &cobra.Command{
|
11 | 30 | Use: "version",
|
12 |
| - Short: "Print the application version and commit information", |
13 |
| - Run: func(cmd *cobra.Command, args []string) { |
14 |
| - fmt.Println("version:", version.Version, "commit:", version.Commit) |
| 31 | + Short: "Display version, commit, build time, and environment details", |
| 32 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 33 | + v := VersionInfo{ |
| 34 | + Version: version.Version, |
| 35 | + GitCommit: version.GitCommit, |
| 36 | + BuildTime: version.BuildTime, |
| 37 | + GoVersion: version.GoVersion, |
| 38 | + BuildOS: version.BuildOS, |
| 39 | + BuildArch: version.BuildArch, |
| 40 | + Platform: fmt.Sprintf("%s/%s", version.BuildOS, version.BuildArch), |
| 41 | + } |
| 42 | + return printVersion(outputFormat, v) |
15 | 43 | },
|
16 | 44 | }
|
| 45 | + |
| 46 | +func init() { |
| 47 | + versionCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Output format: text|json") |
| 48 | +} |
| 49 | + |
| 50 | +/* |
| 51 | +shortCommit returns the first 7 characters of a Git commit SHA (short SHA). |
| 52 | +*/ |
| 53 | +func shortCommit(commit string) string { |
| 54 | + if len(commit) > 7 { |
| 55 | + return commit[:7] |
| 56 | + } |
| 57 | + return commit |
| 58 | +} |
| 59 | + |
| 60 | +/* |
| 61 | +printVersion prints version information in the specified format. |
| 62 | +
|
| 63 | +format: "text" for colored CLI output, "json" for JSON output. |
| 64 | +v: VersionInfo struct containing version data. |
| 65 | +*/ |
| 66 | +func printVersion(format string, v VersionInfo) error { |
| 67 | + // Use short SHA for Git commit |
| 68 | + shortV := v |
| 69 | + shortV.GitCommit = shortCommit(v.GitCommit) |
| 70 | + switch format { |
| 71 | + case "json": |
| 72 | + // Output as pretty-printed JSON |
| 73 | + enc := json.NewEncoder(os.Stdout) |
| 74 | + enc.SetIndent("", " ") |
| 75 | + return enc.Encode(shortV) |
| 76 | + case "text": |
| 77 | + fallthrough |
| 78 | + default: |
| 79 | + // Output as colored CLI text |
| 80 | + blue := color.New(color.FgBlue, color.Bold) |
| 81 | + // Each row contains a label and its value |
| 82 | + rows := [][2]string{ |
| 83 | + {"Version:", shortV.Version}, |
| 84 | + {"Git Commit:", shortV.GitCommit}, |
| 85 | + {"Build Time:", shortV.BuildTime}, |
| 86 | + {"Go Version:", shortV.GoVersion}, |
| 87 | + {"OS/Arch:", fmt.Sprintf("%s/%s", shortV.BuildOS, shortV.BuildArch)}, |
| 88 | + } |
| 89 | + // Print each row with colored label and default value color |
| 90 | + for _, row := range rows { |
| 91 | + blue.Print(row[0]) |
| 92 | + fmt.Printf(" %s\n", row[1]) |
| 93 | + } |
| 94 | + return nil |
| 95 | + } |
| 96 | +} |
0 commit comments