Skip to content

Commit 26cbb98

Browse files
authored
build: enhance build metadata and version reporting functionality (#234)
- Expand embedded build-time variables to include Git commit, build time, Go version, operating system, and architecture - Update build process to inject additional version and environment metadata - Improve version command to output detailed build information, support both text and JSON formats, and add colored CLI output - Update Go version requirement to 1.24 in go.mod Signed-off-by: appleboy <[email protected]>
1 parent 8013799 commit 26cbb98

File tree

4 files changed

+98
-7
lines changed

4 files changed

+98
-7
lines changed

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ GO ?= go
22
EXECUTABLE := codegpt
33
GOFILES := $(shell find . -type f -name "*.go")
44
TAGS ?=
5-
LDFLAGS ?= -X 'github.com/appleboy/CodeGPT/version.Version=$(VERSION)' -X 'github.com/appleboy/CodeGPT/version.Commit=$(COMMIT)'
65

76
ifneq ($(shell uname), Darwin)
87
EXTLDFLAGS = -extldflags "-static" $(null)
@@ -17,6 +16,14 @@ else
1716
endif
1817
COMMIT ?= $(shell git rev-parse --short HEAD)
1918

19+
LDFLAGS ?= -X 'github.com/appleboy/CodeGPT/version.Version=$(VERSION)' \
20+
-X 'github.com/appleboy/CodeGPT/version.BuildTime=$(shell date +%Y-%m-%dT%H:%M:%S)' \
21+
-X 'github.com/appleboy/CodeGPT/version.GitCommit=$(shell git rev-parse HEAD)' \
22+
-X 'github.com/appleboy/CodeGPT/version.GitBranch=$(shell git rev-parse --abbrev-ref HEAD)' \
23+
-X 'github.com/appleboy/CodeGPT/version.GoVersion=$(shell $(GO) version | cut -d " " -f 3)' \
24+
-X 'github.com/appleboy/CodeGPT/version.BuildOS=$(shell $(GO) env GOOS)' \
25+
-X 'github.com/appleboy/CodeGPT/version.BuildArch=$(shell $(GO) env GOARCH)'
26+
2027
## build: build the codegpt binary
2128
build: $(EXECUTABLE)
2229

cmd/version.go

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,96 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"os"
57

68
"github.com/appleboy/CodeGPT/version"
9+
10+
"github.com/fatih/color"
711
"github.com/spf13/cobra"
812
)
913

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+
1029
var versionCmd = &cobra.Command{
1130
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)
1543
},
1644
}
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+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/appleboy/CodeGPT
22

3-
go 1.23.0
3+
go 1.24
44

55
require (
66
github.com/appleboy/com v0.3.0

version/version.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package version
22

33
var (
4-
Version string = ""
5-
Commit string = ""
4+
Version string
5+
GitCommit string
6+
BuildTime string
7+
GoVersion string
8+
BuildOS string
9+
BuildArch string
610
)

0 commit comments

Comments
 (0)