Browse Source

feat: add version command

Hintay 2 months ago
parent
commit
f213bdf7d6
5 changed files with 100 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 83 0
      cmd/version/generate.go
  3. 7 0
      internal/cmd/main.go
  4. 7 0
      internal/version/version.go
  5. 2 0
      main.go

+ 1 - 0
.gitignore

@@ -15,3 +15,4 @@ app/.status_hash
 casdoor.pub
 .idea/deployment.xml
 .idea/webServers.xml
+*.gen.go

+ 83 - 0
cmd/version/generate.go

@@ -0,0 +1,83 @@
+package main
+
+import (
+	"encoding/json"
+	"errors"
+	"fmt"
+	"io"
+	"io/fs"
+	"log"
+	"os"
+	"path"
+	"runtime"
+
+	"github.com/0xJacky/Nginx-UI/app"
+)
+
+type VersionInfo struct {
+	Version    string `json:"version"`
+	BuildId    int    `json:"build_id"`
+	TotalBuild int    `json:"total_build"`
+}
+
+func main() {
+	_, file, _, ok := runtime.Caller(0)
+	if !ok {
+		log.Print("Unable to get the current file")
+		return
+	}
+	basePath := path.Join(path.Dir(file), "../../")
+
+	versionFile, err := app.DistFS.Open("dist/version.json")
+	if err != nil {
+		if errors.Is(err, fs.ErrNotExist) {
+			log.Print("\"dist/version.json\" not found, load from src instead")
+			versionFile, err = os.Open(path.Join(basePath, "app/src/version.json"))
+		}
+
+		if err != nil {
+			log.Fatal(err)
+			return
+		}
+	}
+
+	defer func(versionFile fs.File) {
+		err := versionFile.Close()
+		if err != nil {
+			log.Fatal(err)
+		}
+	}(versionFile)
+
+	// Read the version.json file
+	data, err := io.ReadAll(versionFile)
+	if err != nil {
+		log.Fatalf("Failed to read version.json: %v", err)
+	}
+
+	// Parse the JSON data
+	var versionInfo VersionInfo
+	err = json.Unmarshal(data, &versionInfo)
+	if err != nil {
+		log.Fatalf("Failed to parse JSON: %v", err)
+	}
+
+	// Generate the version.gen.go file content
+	genContent := fmt.Sprintf(`// Code generated by cmd/version/generate.go; DO NOT EDIT.
+
+package version
+
+func init() {
+	Version = "%s"
+	BuildId = %d
+	TotalBuild = %d
+}
+`, versionInfo.Version, versionInfo.BuildId, versionInfo.TotalBuild)
+
+	genPath := path.Join(basePath, "internal/version/version.gen.go")
+	err = os.WriteFile(genPath, []byte(genContent), 0644)
+	if err != nil {
+		log.Fatalf("Failed to write version.gen.go: %v", err)
+	}
+
+	fmt.Println("version.gen.go has been generated successfully.")
+}

+ 7 - 0
internal/cmd/main.go

@@ -2,9 +2,11 @@ package cmd
 
 import (
 	"context"
+	"fmt"
 	"log"
 	"os"
 
+	"github.com/0xJacky/Nginx-UI/internal/version"
 	"github.com/urfave/cli/v3"
 )
 
@@ -32,6 +34,11 @@ func NewAppCmd() *cli.Command {
 			},
 		},
 		DefaultCommand: "serve",
+		Version:        version.Version,
+	}
+
+	cli.VersionPrinter = func(cmd *cli.Command) {
+		fmt.Printf("%s (%d)\n", cmd.Root().Version, version.BuildId)
 	}
 
 	if err := cmd.Run(context.Background(), os.Args); err != nil {

+ 7 - 0
internal/version/version.go

@@ -0,0 +1,7 @@
+package version
+
+var (
+	Version    = ""
+	BuildId    = 0
+	TotalBuild = 0
+)

+ 2 - 0
main.go

@@ -20,6 +20,8 @@ import (
 	cSettings "github.com/uozi-tech/cosy/settings"
 )
 
+//go:generate go run cmd/version/generate.go
+
 func Program(confPath string) func(state overseer.State) {
 	return func(state overseer.State) {
 		defer logger.Sync()