mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-12 10:55:51 +02:00
feat(cli): new version format with commit hash and go version
This commit is contained in:
parent
071c22a881
commit
07eba99f2b
5 changed files with 73 additions and 5 deletions
|
@ -8,8 +8,10 @@ import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VersionInfo struct {
|
type VersionInfo struct {
|
||||||
|
@ -59,6 +61,14 @@ func main() {
|
||||||
log.Fatalf("Failed to parse JSON: %v", err)
|
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
|
// Generate the version.gen.go file content
|
||||||
genContent := fmt.Sprintf(`// Code generated by cmd/version/generate.go; DO NOT EDIT.
|
genContent := fmt.Sprintf(`// Code generated by cmd/version/generate.go; DO NOT EDIT.
|
||||||
|
|
||||||
|
@ -68,8 +78,9 @@ func init() {
|
||||||
Version = "%s"
|
Version = "%s"
|
||||||
BuildId = %d
|
BuildId = %d
|
||||||
TotalBuild = %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")
|
genPath := path.Join(basePath, "internal/version/version.gen.go")
|
||||||
err = os.WriteFile(genPath, []byte(genContent), 0644)
|
err = os.WriteFile(genPath, []byte(genContent), 0644)
|
||||||
|
|
|
@ -2,7 +2,6 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
@ -37,9 +36,8 @@ func NewAppCmd() *cli.Command {
|
||||||
Version: version.Version,
|
Version: version.Version,
|
||||||
}
|
}
|
||||||
|
|
||||||
cli.VersionPrinter = func(cmd *cli.Command) {
|
// Set the version printer
|
||||||
fmt.Printf("%s (%d)\n", cmd.Root().Version, version.BuildId)
|
cli.VersionPrinter = VersionPrinter
|
||||||
}
|
|
||||||
|
|
||||||
if err := cmd.Run(context.Background(), os.Args); err != nil {
|
if err := cmd.Run(context.Background(), os.Args); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
17
internal/cmd/version.go
Normal file
17
internal/cmd/version.go
Normal file
|
@ -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)
|
||||||
|
}
|
34
internal/helper/serial.go
Normal file
34
internal/helper/serial.go
Normal file
|
@ -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()
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ var (
|
||||||
Version = ""
|
Version = ""
|
||||||
BuildId = 0
|
BuildId = 0
|
||||||
TotalBuild = 0
|
TotalBuild = 0
|
||||||
|
Hash = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
type Info struct {
|
type Info struct {
|
||||||
|
@ -24,3 +25,10 @@ func GetVersionInfo() *Info {
|
||||||
}
|
}
|
||||||
return versionInfo
|
return versionInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetShortHash() string {
|
||||||
|
if Hash != "" {
|
||||||
|
return Hash[:8]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue