Sfoglia il codice sorgente

feat: new command line options

Hintay 3 mesi fa
parent
commit
4239a89d66
4 ha cambiato i file con 53 aggiunte e 8 eliminazioni
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 43 0
      internal/cmd/main.go
  4. 7 8
      main.go

+ 1 - 0
go.mod

@@ -37,6 +37,7 @@ require (
 	github.com/tufanbarisyildirim/gonginx v0.0.0-20250120210832-12a9c7ae0c8a
 	github.com/uozi-tech/cosy v1.14.3
 	github.com/uozi-tech/cosy-driver-sqlite v0.2.0
+	github.com/urfave/cli/v3 v3.0.0-beta1
 	go.uber.org/zap v1.27.0
 	golang.org/x/crypto v0.32.0
 	golang.org/x/net v0.34.0

+ 2 - 0
go.sum

@@ -1782,6 +1782,8 @@ github.com/uozi-tech/cosy-driver-postgres v0.2.1/go.mod h1:eAy1A89yHbAEfjkhNAifa
 github.com/uozi-tech/cosy-driver-sqlite v0.2.0 h1:eTpIMyGoFUK4JcaiKfJHD5AyiM6vtCwN98c7Bz5n25o=
 github.com/uozi-tech/cosy-driver-sqlite v0.2.0/go.mod h1:87a6mzn5IuEtIR4z7U4Ey8eKLGfNEOSkv7kPQlbNQgM=
 github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
+github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg=
+github.com/urfave/cli/v3 v3.0.0-beta1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y=
 github.com/vinyldns/go-vinyldns v0.9.16 h1:GZJStDkcCk1F1AcRc64LuuMh+ENL8pHA0CVd4ulRMcQ=
 github.com/vinyldns/go-vinyldns v0.9.16/go.mod h1:5qIJOdmzAnatKjurI+Tl4uTus7GJKJxb+zitufjHs3Q=
 github.com/volcengine/volc-sdk-golang v1.0.194 h1:3o0INQzdtYJWvdGrtX02booCqPL5TsWSq2W1Ur7Bzlo=

+ 43 - 0
internal/cmd/main.go

@@ -0,0 +1,43 @@
+package cmd
+
+import (
+	"context"
+	"log"
+	"os"
+
+	"github.com/urfave/cli/v3"
+)
+
+func NewAppCmd() *cli.Command {
+	serve := false
+
+	cmd := &cli.Command{
+		Name:  "nginx-ui",
+		Usage: "Yet another Nginx Web UI",
+		Commands: []*cli.Command{
+			{
+				Name:  "serve",
+				Usage: "Start the Nginx-UI server",
+				Action: func(ctx context.Context, command *cli.Command) error {
+					serve = true
+					return nil
+				},
+			},
+		},
+		Flags: []cli.Flag{
+			&cli.StringFlag{
+				Name:  "config",
+				Value: "app.ini",
+				Usage: "configuration file path",
+			},
+		},
+		DefaultCommand: "serve",
+	}
+
+	if err := cmd.Run(context.Background(), os.Args); err != nil {
+		log.Fatal(err)
+	} else if !serve {
+		os.Exit(0)
+	}
+	return cmd
+}

+ 7 - 8
main.go

@@ -1,22 +1,23 @@
 package main
 
 import (
-	"flag"
+	"errors"
 	"fmt"
+	"net/http"
+	"time"
+
+	"github.com/0xJacky/Nginx-UI/internal/cmd"
 	"github.com/0xJacky/Nginx-UI/internal/kernel"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/router"
 	"github.com/0xJacky/Nginx-UI/settings"
 	"github.com/gin-gonic/gin"
 	"github.com/jpillora/overseer"
-	"errors"
 	"github.com/uozi-tech/cosy"
 	cKernel "github.com/uozi-tech/cosy/kernel"
 	"github.com/uozi-tech/cosy/logger"
 	cRouter "github.com/uozi-tech/cosy/router"
 	cSettings "github.com/uozi-tech/cosy/settings"
-	"net/http"
-	"time"
 )
 
 func Program(confPath string) func(state overseer.State) {
@@ -58,12 +59,10 @@ func Program(confPath string) func(state overseer.State) {
 }
 
 func main() {
-	var confPath string
-	flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file")
-	flag.Parse()
+	appCmd := cmd.NewAppCmd()
 
+	confPath := appCmd.String("config")
 	settings.Init(confPath)
-
 	overseer.Run(overseer.Config{
 		Program:          Program(confPath),
 		Address:          fmt.Sprintf("%s:%d", cSettings.ServerSettings.Host, cSettings.ServerSettings.Port),