From 07eba99f2b4cb8e4b917106e72ab223155d2e683 Mon Sep 17 00:00:00 2001 From: Hintay Date: Wed, 5 Feb 2025 14:25:50 +0900 Subject: [PATCH] feat(cli): new version format with commit hash and go version --- cmd/version/generate.go | 13 ++++++++++++- internal/cmd/main.go | 6 ++---- internal/cmd/version.go | 17 +++++++++++++++++ internal/helper/serial.go | 34 ++++++++++++++++++++++++++++++++++ internal/version/version.go | 8 ++++++++ 5 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 internal/cmd/version.go create mode 100644 internal/helper/serial.go diff --git a/cmd/version/generate.go b/cmd/version/generate.go index 304f16f8..6453975f 100644 --- a/cmd/version/generate.go +++ b/cmd/version/generate.go @@ -8,8 +8,10 @@ import ( "io/fs" "log" "os" + "os/exec" "path" "runtime" + "strings" ) type VersionInfo struct { @@ -59,6 +61,14 @@ func main() { log.Fatalf("Failed to parse JSON: %v", err) } + // get current git commit hash + cmd := exec.Command("git", "-C", basePath, "rev-parse", "HEAD") + commitHash, err := cmd.Output() + if err != nil { + log.Printf("Failed to get git commit hash: %v", err) + commitHash = []byte("") + } + // Generate the version.gen.go file content genContent := fmt.Sprintf(`// Code generated by cmd/version/generate.go; DO NOT EDIT. @@ -68,8 +78,9 @@ func init() { Version = "%s" BuildId = %d TotalBuild = %d + Hash = "%s" } -`, versionInfo.Version, versionInfo.BuildId, versionInfo.TotalBuild) +`, versionInfo.Version, versionInfo.BuildId, versionInfo.TotalBuild, strings.TrimRight(string(commitHash), "\r\n")) genPath := path.Join(basePath, "internal/version/version.gen.go") err = os.WriteFile(genPath, []byte(genContent), 0644) diff --git a/internal/cmd/main.go b/internal/cmd/main.go index 68476e13..c84ae9fc 100644 --- a/internal/cmd/main.go +++ b/internal/cmd/main.go @@ -2,7 +2,6 @@ package cmd import ( "context" - "fmt" "log" "os" @@ -37,9 +36,8 @@ func NewAppCmd() *cli.Command { Version: version.Version, } - cli.VersionPrinter = func(cmd *cli.Command) { - fmt.Printf("%s (%d)\n", cmd.Root().Version, version.BuildId) - } + // Set the version printer + cli.VersionPrinter = VersionPrinter if err := cmd.Run(context.Background(), os.Args); err != nil { log.Fatal(err) diff --git a/internal/cmd/version.go b/internal/cmd/version.go new file mode 100644 index 00000000..73b1b964 --- /dev/null +++ b/internal/cmd/version.go @@ -0,0 +1,17 @@ +package cmd + +import ( + "fmt" + "runtime" + + "github.com/0xJacky/Nginx-UI/internal/helper" + "github.com/0xJacky/Nginx-UI/internal/version" + "github.com/urfave/cli/v3" +) + +func VersionPrinter(cmd *cli.Command) { + fmt.Println(helper.Concat( + cmd.Root().Name, " ", cmd.Root().Version, " ", version.BuildId, "(", version.TotalBuild, ") ", + version.GetShortHash(), " (", runtime.Version(), " ", runtime.GOOS, "/", runtime.GOARCH, ")")) + fmt.Println(cmd.Root().Usage) +} diff --git a/internal/helper/serial.go b/internal/helper/serial.go new file mode 100644 index 00000000..a3440e26 --- /dev/null +++ b/internal/helper/serial.go @@ -0,0 +1,34 @@ +package helper + +import ( + "fmt" + "strings" +) + +func ToString(v interface{}) string { + if v == nil { + return "" + } + + switch value := v.(type) { + case string: + return value + case *string: + return *value + case fmt.Stringer: + return value.String() + case error: + return value.Error() + default: + return fmt.Sprintf("%+v", value) + } +} + +// Concat concatenates all input into a single string. +func Concat(v ...interface{}) string { + builder := strings.Builder{} + for _, value := range v { + builder.WriteString(ToString(value)) + } + return builder.String() +} diff --git a/internal/version/version.go b/internal/version/version.go index 4eecdad1..81266767 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -4,6 +4,7 @@ var ( Version = "" BuildId = 0 TotalBuild = 0 + Hash = "" ) type Info struct { @@ -24,3 +25,10 @@ func GetVersionInfo() *Info { } return versionInfo } + +func GetShortHash() string { + if Hash != "" { + return Hash[:8] + } + return "" +}