Skip to content

Commit 07eba99

Browse files
committed
feat(cli): new version format with commit hash and go version
1 parent 071c22a commit 07eba99

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed

cmd/version/generate.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"io/fs"
99
"log"
1010
"os"
11+
"os/exec"
1112
"path"
1213
"runtime"
14+
"strings"
1315
)
1416

1517
type VersionInfo struct {
@@ -59,6 +61,14 @@ func main() {
5961
log.Fatalf("Failed to parse JSON: %v", err)
6062
}
6163

64+
// get current git commit hash
65+
cmd := exec.Command("git", "-C", basePath, "rev-parse", "HEAD")
66+
commitHash, err := cmd.Output()
67+
if err != nil {
68+
log.Printf("Failed to get git commit hash: %v", err)
69+
commitHash = []byte("")
70+
}
71+
6272
// Generate the version.gen.go file content
6373
genContent := fmt.Sprintf(`// Code generated by cmd/version/generate.go; DO NOT EDIT.
6474
@@ -68,8 +78,9 @@ func init() {
6878
Version = "%s"
6979
BuildId = %d
7080
TotalBuild = %d
81+
Hash = "%s"
7182
}
72-
`, versionInfo.Version, versionInfo.BuildId, versionInfo.TotalBuild)
83+
`, versionInfo.Version, versionInfo.BuildId, versionInfo.TotalBuild, strings.TrimRight(string(commitHash), "\r\n"))
7384

7485
genPath := path.Join(basePath, "internal/version/version.gen.go")
7586
err = os.WriteFile(genPath, []byte(genContent), 0644)

internal/cmd/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"context"
5-
"fmt"
65
"log"
76
"os"
87

@@ -37,9 +36,8 @@ func NewAppCmd() *cli.Command {
3736
Version: version.Version,
3837
}
3938

40-
cli.VersionPrinter = func(cmd *cli.Command) {
41-
fmt.Printf("%s (%d)\n", cmd.Root().Version, version.BuildId)
42-
}
39+
// Set the version printer
40+
cli.VersionPrinter = VersionPrinter
4341

4442
if err := cmd.Run(context.Background(), os.Args); err != nil {
4543
log.Fatal(err)

internal/cmd/version.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
7+
"github.com/0xJacky/Nginx-UI/internal/helper"
8+
"github.com/0xJacky/Nginx-UI/internal/version"
9+
"github.com/urfave/cli/v3"
10+
)
11+
12+
func VersionPrinter(cmd *cli.Command) {
13+
fmt.Println(helper.Concat(
14+
cmd.Root().Name, " ", cmd.Root().Version, " ", version.BuildId, "(", version.TotalBuild, ") ",
15+
version.GetShortHash(), " (", runtime.Version(), " ", runtime.GOOS, "/", runtime.GOARCH, ")"))
16+
fmt.Println(cmd.Root().Usage)
17+
}

internal/helper/serial.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package helper
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func ToString(v interface{}) string {
9+
if v == nil {
10+
return ""
11+
}
12+
13+
switch value := v.(type) {
14+
case string:
15+
return value
16+
case *string:
17+
return *value
18+
case fmt.Stringer:
19+
return value.String()
20+
case error:
21+
return value.Error()
22+
default:
23+
return fmt.Sprintf("%+v", value)
24+
}
25+
}
26+
27+
// Concat concatenates all input into a single string.
28+
func Concat(v ...interface{}) string {
29+
builder := strings.Builder{}
30+
for _, value := range v {
31+
builder.WriteString(ToString(value))
32+
}
33+
return builder.String()
34+
}

internal/version/version.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var (
44
Version = ""
55
BuildId = 0
66
TotalBuild = 0
7+
Hash = ""
78
)
89

910
type Info struct {
@@ -24,3 +25,10 @@ func GetVersionInfo() *Info {
2425
}
2526
return versionInfo
2627
}
28+
29+
func GetShortHash() string {
30+
if Hash != "" {
31+
return Hash[:8]
32+
}
33+
return ""
34+
}

0 commit comments

Comments
 (0)