Skip to content

Commit f213bdf

Browse files
committed
feat: add version command
1 parent 4239a89 commit f213bdf

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ app/.status_hash
1515
casdoor.pub
1616
.idea/deployment.xml
1717
.idea/webServers.xml
18+
*.gen.go

cmd/version/generate.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"io/fs"
9+
"log"
10+
"os"
11+
"path"
12+
"runtime"
13+
14+
"github.com/0xJacky/Nginx-UI/app"
15+
)
16+
17+
type VersionInfo struct {
18+
Version string `json:"version"`
19+
BuildId int `json:"build_id"`
20+
TotalBuild int `json:"total_build"`
21+
}
22+
23+
func main() {
24+
_, file, _, ok := runtime.Caller(0)
25+
if !ok {
26+
log.Print("Unable to get the current file")
27+
return
28+
}
29+
basePath := path.Join(path.Dir(file), "../../")
30+
31+
versionFile, err := app.DistFS.Open("dist/version.json")
32+
if err != nil {
33+
if errors.Is(err, fs.ErrNotExist) {
34+
log.Print("\"dist/version.json\" not found, load from src instead")
35+
versionFile, err = os.Open(path.Join(basePath, "app/src/version.json"))
36+
}
37+
38+
if err != nil {
39+
log.Fatal(err)
40+
return
41+
}
42+
}
43+
44+
defer func(versionFile fs.File) {
45+
err := versionFile.Close()
46+
if err != nil {
47+
log.Fatal(err)
48+
}
49+
}(versionFile)
50+
51+
// Read the version.json file
52+
data, err := io.ReadAll(versionFile)
53+
if err != nil {
54+
log.Fatalf("Failed to read version.json: %v", err)
55+
}
56+
57+
// Parse the JSON data
58+
var versionInfo VersionInfo
59+
err = json.Unmarshal(data, &versionInfo)
60+
if err != nil {
61+
log.Fatalf("Failed to parse JSON: %v", err)
62+
}
63+
64+
// Generate the version.gen.go file content
65+
genContent := fmt.Sprintf(`// Code generated by cmd/version/generate.go; DO NOT EDIT.
66+
67+
package version
68+
69+
func init() {
70+
Version = "%s"
71+
BuildId = %d
72+
TotalBuild = %d
73+
}
74+
`, versionInfo.Version, versionInfo.BuildId, versionInfo.TotalBuild)
75+
76+
genPath := path.Join(basePath, "internal/version/version.gen.go")
77+
err = os.WriteFile(genPath, []byte(genContent), 0644)
78+
if err != nil {
79+
log.Fatalf("Failed to write version.gen.go: %v", err)
80+
}
81+
82+
fmt.Println("version.gen.go has been generated successfully.")
83+
}

internal/cmd/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package cmd
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"os"
78

9+
"github.com/0xJacky/Nginx-UI/internal/version"
810
"github.com/urfave/cli/v3"
911
)
1012

@@ -32,6 +34,11 @@ func NewAppCmd() *cli.Command {
3234
},
3335
},
3436
DefaultCommand: "serve",
37+
Version: version.Version,
38+
}
39+
40+
cli.VersionPrinter = func(cmd *cli.Command) {
41+
fmt.Printf("%s (%d)\n", cmd.Root().Version, version.BuildId)
3542
}
3643

3744
if err := cmd.Run(context.Background(), os.Args); err != nil {

internal/version/version.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package version
2+
3+
var (
4+
Version = ""
5+
BuildId = 0
6+
TotalBuild = 0
7+
)

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
cSettings "github.com/uozi-tech/cosy/settings"
2121
)
2222

23+
//go:generate go run cmd/version/generate.go
24+
2325
func Program(confPath string) func(state overseer.State) {
2426
return func(state overseer.State) {
2527
defer logger.Sync()

0 commit comments

Comments
 (0)