mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
feat: add version command
This commit is contained in:
parent
4239a89d66
commit
f213bdf7d6
5 changed files with 100 additions and 0 deletions
83
cmd/version/generate.go
Normal file
83
cmd/version/generate.go
Normal file
|
@ -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.")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue