Skip to content

Commit d2e7dd5

Browse files
authored
Merge pull request #842 from 0xJacky/feat/cli
feat: new command system
2 parents f7e3c52 + 8a7ed08 commit d2e7dd5

File tree

13 files changed

+192
-53
lines changed

13 files changed

+192
-53
lines changed

.github/workflows/build.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ jobs:
142142
with:
143143
go-version: ^1.23.0
144144

145+
- name: Download app artifacts
146+
uses: actions/download-artifact@v4
147+
with:
148+
name: app-dist
149+
path: app/dist
150+
151+
- name: Generate files
152+
env:
153+
GOOS: linux
154+
GOARCH: amd64
155+
run: go generate
156+
145157
- name: Setup compiler environment
146158
id: info
147159
run: |
@@ -182,12 +194,6 @@ jobs:
182194
echo "CXX=${{ env.ARCH_NAME }}-clang++" >> $GITHUB_ENV
183195
echo "LD_FLAGS=-w" >> $GITHUB_ENV
184196
185-
- name: Download app artifacts
186-
uses: actions/download-artifact@v4
187-
with:
188-
name: app-dist
189-
path: app/dist
190-
191197
- name: Build
192198
run: |
193199
mkdir -p dist

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ app/.env
1414
app/.status_hash
1515
.idea/deployment.xml
1616
.idea/webServers.xml
17+
*.gen.go
1718
.devcontainer/go-path
1819
.devcontainer/data
1920
.devcontainer/casdoor.pem

api/cluster/node.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package cluster
22

33
import (
4+
"net/http"
5+
46
"github.com/0xJacky/Nginx-UI/api"
57
analytic2 "github.com/0xJacky/Nginx-UI/internal/analytic"
68
"github.com/0xJacky/Nginx-UI/internal/upgrader"
9+
"github.com/0xJacky/Nginx-UI/internal/version"
710
"github.com/dustin/go-humanize"
811
"github.com/gin-gonic/gin"
912
"github.com/shirou/gopsutil/v4/cpu"
1013
"github.com/shirou/gopsutil/v4/disk"
11-
"net/http"
1214
)
1315

1416
func GetCurrentNode(c *gin.Context) {
@@ -26,7 +28,7 @@ func GetCurrentNode(c *gin.Context) {
2628
}
2729
cpuInfo, _ := cpu.Info()
2830
memory, _ := analytic2.GetMemoryStat()
29-
ver, _ := upgrader.GetCurrentVersion()
31+
ver := version.GetVersionInfo()
3032
diskUsage, _ := disk.Usage(".")
3133

3234
nodeInfo := analytic2.NodeInfo{

api/system/upgrade.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package system
22

33
import (
4+
"net/http"
5+
"os"
6+
47
"github.com/0xJacky/Nginx-UI/api"
58
"github.com/0xJacky/Nginx-UI/internal/upgrader"
9+
"github.com/0xJacky/Nginx-UI/internal/version"
610
"github.com/0xJacky/Nginx-UI/settings"
711
"github.com/gin-gonic/gin"
812
"github.com/gorilla/websocket"
913
"github.com/uozi-tech/cosy/logger"
10-
"net/http"
11-
"os"
1214
)
1315

1416
func GetRelease(c *gin.Context) {
@@ -32,13 +34,7 @@ func GetRelease(c *gin.Context) {
3234
}
3335

3436
func GetCurrentVersion(c *gin.Context) {
35-
curVer, err := upgrader.GetCurrentVersion()
36-
if err != nil {
37-
api.ErrHandler(c, err)
38-
return
39-
}
40-
41-
c.JSON(http.StatusOK, curVer)
37+
c.JSON(http.StatusOK, version.GetVersionInfo())
4238
}
4339

4440
const (

app/app.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,3 @@ import (
88

99
//go:embed i18n.json dist/* dist/*/* src/language/* src/language/*/*
1010
var DistFS embed.FS
11-
12-
var VersionPath = "dist/version.json"

app/app_unembed.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@ package app
44

55
import "embed"
66

7-
//go:embed i18n.json src/language/* src/language/*/* src/version.json
7+
//go:embed i18n.json src/language/* src/language/*/*
88
var DistFS embed.FS
9-
10-
var VersionPath = "src/version.json"

cmd/version/generate.go

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

go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ require (
1111
github.com/dgraph-io/ristretto/v2 v2.1.0
1212
github.com/dustin/go-humanize v1.0.1
1313
github.com/elliotchance/orderedmap/v3 v3.1.0
14-
github.com/fatih/color v1.18.0
1514
github.com/gin-contrib/static v1.1.3
1615
github.com/gin-gonic/gin v1.10.0
1716
github.com/go-acme/lego/v4 v4.21.0
@@ -31,13 +30,13 @@ require (
3130
github.com/pretty66/websocketproxy v0.0.0-20220507015215-930b3a686308
3231
github.com/samber/lo v1.49.1
3332
github.com/sashabaranov/go-openai v1.36.1
34-
github.com/shirou/gopsutil/v4 v4.25.1
33+
github.com/shirou/gopsutil/v4 v4.24.12
3534
github.com/spf13/cast v1.7.1
3635
github.com/stretchr/testify v1.10.0
3736
github.com/tufanbarisyildirim/gonginx v0.0.0-20250120210832-12a9c7ae0c8a
3837
github.com/uozi-tech/cosy v1.14.3
39-
github.com/uozi-tech/cosy-driver-sqlite v0.2.1
40-
go.uber.org/zap v1.27.0
38+
github.com/uozi-tech/cosy-driver-sqlite v0.2.0
39+
github.com/urfave/cli/v3 v3.0.0-beta1
4140
golang.org/x/crypto v0.32.0
4241
golang.org/x/net v0.34.0
4342
gopkg.in/ini.v1 v1.67.0
@@ -107,6 +106,7 @@ require (
107106
github.com/dnsimple/dnsimple-go v1.7.0 // indirect
108107
github.com/ebitengine/purego v0.8.2 // indirect
109108
github.com/exoscale/egoscale/v3 v3.1.9 // indirect
109+
github.com/fatih/color v1.18.0 // indirect
110110
github.com/fatih/structs v1.1.0 // indirect
111111
github.com/felixge/httpsnoop v1.0.4 // indirect
112112
github.com/fsnotify/fsnotify v1.8.0 // indirect
@@ -253,6 +253,7 @@ require (
253253
go.uber.org/atomic v1.11.0 // indirect
254254
go.uber.org/multierr v1.11.0 // indirect
255255
go.uber.org/ratelimit v0.3.1 // indirect
256+
go.uber.org/zap v1.27.0 // indirect
256257
golang.org/x/arch v0.13.0 // indirect
257258
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c // indirect
258259
golang.org/x/mod v0.22.0 // indirect

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,8 +1658,6 @@ github.com/selectel/go-selvpcclient/v3 v3.2.1 h1:ny6WIAMiHzKxOgOEnwcWE79wIQij1AH
16581658
github.com/selectel/go-selvpcclient/v3 v3.2.1/go.mod h1:3EfSf8aEWyhspOGbvZ6mvnFg7JN5uckxNyBFPGWsXNQ=
16591659
github.com/shirou/gopsutil/v4 v4.24.12 h1:qvePBOk20e0IKA1QXrIIU+jmk+zEiYVVx06WjBRlZo4=
16601660
github.com/shirou/gopsutil/v4 v4.24.12/go.mod h1:DCtMPAad2XceTeIAbGyVfycbYQNBGk2P8cvDi7/VN9o=
1661-
github.com/shirou/gopsutil/v4 v4.25.1 h1:QSWkTc+fu9LTAWfkZwZ6j8MSUk4A2LV7rbH0ZqmLjXs=
1662-
github.com/shirou/gopsutil/v4 v4.25.1/go.mod h1:RoUCUpndaJFtT+2zsZzzmhvbfGoDCJ7nFXKJf8GqJbI=
16631661
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
16641662
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
16651663
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
@@ -1783,9 +1781,9 @@ github.com/uozi-tech/cosy-driver-postgres v0.2.1 h1:OICakGuT+omva6QOJCxTJ5Lfr7CG
17831781
github.com/uozi-tech/cosy-driver-postgres v0.2.1/go.mod h1:eAy1A89yHbAEfjkhNAifaJQk172NqrNoRyRtFcZc9Go=
17841782
github.com/uozi-tech/cosy-driver-sqlite v0.2.0 h1:eTpIMyGoFUK4JcaiKfJHD5AyiM6vtCwN98c7Bz5n25o=
17851783
github.com/uozi-tech/cosy-driver-sqlite v0.2.0/go.mod h1:87a6mzn5IuEtIR4z7U4Ey8eKLGfNEOSkv7kPQlbNQgM=
1786-
github.com/uozi-tech/cosy-driver-sqlite v0.2.1 h1:W+Z4pY25PSJCeReqroG7LIBeffsqotbpHzgqSMqZDIM=
1787-
github.com/uozi-tech/cosy-driver-sqlite v0.2.1/go.mod h1:2ya7Z5P3HzFi1ktfL8gvwaAGx0DDV0bmWxNSNpaLlwo=
17881784
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
1785+
github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg=
1786+
github.com/urfave/cli/v3 v3.0.0-beta1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y=
17891787
github.com/vinyldns/go-vinyldns v0.9.16 h1:GZJStDkcCk1F1AcRc64LuuMh+ENL8pHA0CVd4ulRMcQ=
17901788
github.com/vinyldns/go-vinyldns v0.9.16/go.mod h1:5qIJOdmzAnatKjurI+Tl4uTus7GJKJxb+zitufjHs3Q=
17911789
github.com/volcengine/volc-sdk-golang v1.0.194 h1:3o0INQzdtYJWvdGrtX02booCqPL5TsWSq2W1Ur7Bzlo=

internal/cmd/main.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"github.com/0xJacky/Nginx-UI/internal/version"
10+
"github.com/urfave/cli/v3"
11+
)
12+
13+
func NewAppCmd() *cli.Command {
14+
serve := false
15+
16+
cmd := &cli.Command{
17+
Name: "nginx-ui",
18+
Usage: "Yet another Nginx Web UI",
19+
Commands: []*cli.Command{
20+
{
21+
Name: "serve",
22+
Usage: "Start the Nginx-UI server",
23+
Action: func(ctx context.Context, command *cli.Command) error {
24+
serve = true
25+
return nil
26+
},
27+
},
28+
},
29+
Flags: []cli.Flag{
30+
&cli.StringFlag{
31+
Name: "config",
32+
Value: "app.ini",
33+
Usage: "configuration file path",
34+
},
35+
},
36+
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)
42+
}
43+
44+
if err := cmd.Run(context.Background(), os.Args); err != nil {
45+
log.Fatal(err)
46+
} else if !serve {
47+
os.Exit(0)
48+
}
49+
return cmd
50+
}

0 commit comments

Comments
 (0)