mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-10 18:05:48 +02:00
chore: use go generate
This commit is contained in:
parent
61ff392c5c
commit
82da0ef05e
20 changed files with 151 additions and 363 deletions
|
@ -1,3 +1,4 @@
|
|||
//go:generate go run . -project . -type ts -output ../../app/src/constants/errors -ignore-dirs .devcontainer,app,.github
|
||||
package main
|
||||
|
||||
import "github.com/uozi-tech/cosy/errdef"
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
// This file is auto-generated by notification generator. DO NOT EDIT.
|
||||
|
||||
const configMap = {
|
||||
}
|
||||
|
||||
export default configMap
|
|
@ -1,3 +1,4 @@
|
|||
//go:generate go run .
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -8,9 +9,12 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
)
|
||||
|
||||
// Structure to hold extracted notifier information
|
||||
|
@ -51,18 +55,26 @@ export default {{.Name | replaceSpaces}}Config
|
|||
var externalNotifierRegex = regexp.MustCompile(`@external_notifier\(([a-zA-Z0-9 _]+)\)`)
|
||||
|
||||
func main() {
|
||||
if err := GenerateExternalNotifiers(); err != nil {
|
||||
logger.Init("release")
|
||||
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
logger.Error("Unable to get the current file")
|
||||
return
|
||||
}
|
||||
basePath := filepath.Join(filepath.Dir(file), "../../")
|
||||
if err := GenerateExternalNotifiers(basePath); err != nil {
|
||||
fmt.Printf("error generating external notifier configs: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateExternalNotifiers generates TypeScript config files for external notifiers
|
||||
func GenerateExternalNotifiers() error {
|
||||
func GenerateExternalNotifiers(root string) error {
|
||||
fmt.Println("Generating external notifier configs...")
|
||||
|
||||
// Notification package path
|
||||
notificationPkgPath := "internal/notification"
|
||||
outputDir := "app/src/views/preference/components/ExternalNotify"
|
||||
notificationPkgPath := filepath.Join(root, "internal/notification")
|
||||
outputDir := filepath.Join(root, "app/src/views/preference/components/ExternalNotify")
|
||||
|
||||
// Create output directory if it doesn't exist
|
||||
if err := os.MkdirAll(outputDir, 0755); err != nil {
|
||||
|
@ -82,7 +94,7 @@ func GenerateExternalNotifiers() error {
|
|||
notifier, found := extractNotifierInfo(file)
|
||||
if found {
|
||||
notifiers = append(notifiers, notifier)
|
||||
fmt.Printf("Found notifier: %s in %s\n", notifier.Name, file)
|
||||
logger.Infof("Found notifier: %s in %s\n", notifier.Name, file)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,7 +110,7 @@ func GenerateExternalNotifiers() error {
|
|||
return fmt.Errorf("error updating index.ts: %w", err)
|
||||
}
|
||||
|
||||
fmt.Println("Generation completed successfully!")
|
||||
logger.Info("Generation completed successfully!")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -110,7 +122,7 @@ func extractNotifierInfo(filePath string) (NotifierInfo, bool) {
|
|||
// Parse the file
|
||||
file, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing file %s: %v\n", filePath, err)
|
||||
logger.Errorf("Error parsing file %s: %v\n", filePath, err)
|
||||
return NotifierInfo{}, false
|
||||
}
|
||||
|
||||
|
@ -232,7 +244,7 @@ func generateTSConfig(notifier NotifierInfo, outputDir string) error {
|
|||
return fmt.Errorf("error executing template: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Generated TypeScript config for %s at %s\n", notifier.Name, outputFile)
|
||||
logger.Infof("Generated TypeScript config for %s at %s\n", notifier.Name, outputFile)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -272,6 +284,6 @@ func updateIndexFile(notifiers []NotifierInfo, outputDir string) error {
|
|||
return fmt.Errorf("error writing index.ts: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Updated index.ts at %s\n", indexPath)
|
||||
logger.Infof("Updated index.ts at %s\n", indexPath)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,24 +1,35 @@
|
|||
//go:generate go run .
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
cSettings "github.com/uozi-tech/cosy/settings"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"log"
|
||||
"path"
|
||||
gormlogger "gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger.Init("release")
|
||||
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
logger.Error("Unable to get the current file")
|
||||
return
|
||||
}
|
||||
basePath := filepath.Join(filepath.Dir(file), "../../")
|
||||
// specify the output directory (default: "./query")
|
||||
// ### if you want to query without context constrain, set mode gen.WithoutContext ###
|
||||
g := gen.NewGenerator(gen.Config{
|
||||
OutPath: "query",
|
||||
OutPath: filepath.Join(basePath, "query"),
|
||||
Mode: gen.WithoutContext | gen.WithDefaultQuery,
|
||||
//if you want the nullable field generation property to be pointer type, set FieldNullable true
|
||||
FieldNullable: true,
|
||||
|
@ -41,17 +52,17 @@ func main() {
|
|||
flag.Parse()
|
||||
|
||||
cSettings.Init(confPath)
|
||||
dbPath := path.Join(path.Dir(confPath), fmt.Sprintf("%s.db", settings.DatabaseSettings.Name))
|
||||
dbPath := filepath.Join(filepath.Dir(confPath), fmt.Sprintf("%s.db", settings.DatabaseSettings.Name))
|
||||
|
||||
var err error
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Info),
|
||||
Logger: gormlogger.Default.LogMode(gormlogger.Info),
|
||||
PrepareStmt: true,
|
||||
DisableForeignKeyConstraintWhenMigrating: true,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
logger.Fatalf("failed to open database: %v", err)
|
||||
}
|
||||
|
||||
g.UseDB(db)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:generate go run .
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -9,6 +10,8 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"runtime"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
"github.com/spf13/afero/zipfs"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
|
@ -16,19 +19,26 @@ import (
|
|||
|
||||
const (
|
||||
repoURL = "https://github.com/go-acme/lego/archive/refs/heads/master.zip"
|
||||
zipFile = "lego-master.zip"
|
||||
configDir = "internal/cert/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger.Init("release")
|
||||
|
||||
if err := downloadAndExtract(); err != nil {
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
logger.Error("Unable to get the current file")
|
||||
return
|
||||
}
|
||||
basePath := filepath.Join(filepath.Dir(file), "../../")
|
||||
|
||||
zipFile, err := downloadAndExtract()
|
||||
if err != nil {
|
||||
logger.Errorf("Error downloading and extracting: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := copyTomlFiles(); err != nil {
|
||||
if err := copyTomlFiles(zipFile, basePath); err != nil {
|
||||
logger.Errorf("Error copying TOML files: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
@ -37,36 +47,36 @@ func main() {
|
|||
}
|
||||
|
||||
// downloadAndExtract downloads the lego repository and extracts it
|
||||
func downloadAndExtract() error {
|
||||
func downloadAndExtract() (string, error) {
|
||||
// Download the file
|
||||
logger.Info("Downloading lego repository...")
|
||||
resp, err := http.Get(repoURL)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("bad status: %s", resp.Status)
|
||||
return "", fmt.Errorf("bad status: %s", resp.Status)
|
||||
}
|
||||
|
||||
// Create the file
|
||||
out, err := os.Create(zipFile)
|
||||
out, err := os.CreateTemp("", "lego-master.zip")
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// Write the body to file
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
return nil
|
||||
return out.Name(), nil
|
||||
}
|
||||
|
||||
func copyTomlFiles() error {
|
||||
func copyTomlFiles(zipFile, basePath string) error {
|
||||
// Open the zip file
|
||||
logger.Info("Extracting files...")
|
||||
zipReader, err := zip.OpenReader(zipFile)
|
||||
|
@ -78,7 +88,6 @@ func copyTomlFiles() error {
|
|||
// Extract files
|
||||
zfs := zipfs.New(&zipReader.Reader)
|
||||
afero.Walk(zfs, "./lego-master/providers", func(path string, info os.FileInfo, err error) error {
|
||||
// Skip directories
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
@ -93,7 +102,7 @@ func copyTomlFiles() error {
|
|||
return err
|
||||
}
|
||||
// Write to the destination file
|
||||
destPath := filepath.Join(configDir, info.Name())
|
||||
destPath := filepath.Join(basePath, configDir, info.Name())
|
||||
if err := os.WriteFile(destPath, data, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
//go:generate go run .
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"path/filepath"
|
||||
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
|
@ -14,15 +18,26 @@ type Directive struct {
|
|||
Links []string `json:"links"`
|
||||
}
|
||||
|
||||
const (
|
||||
targetPath = "internal/nginx/nginx_directives.json"
|
||||
nginxURL = "https://nginx.org/en/docs/dirindex.html"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
log.Println("Usage: go run . <output_file>")
|
||||
logger.Init("release")
|
||||
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
logger.Error("Unable to get the current file")
|
||||
return
|
||||
}
|
||||
outputPath := os.Args[1]
|
||||
basePath := filepath.Join(filepath.Dir(file), "../../")
|
||||
|
||||
outputPath := filepath.Join(basePath, targetPath)
|
||||
// Fetch page content
|
||||
resp, err := http.Get("https://nginx.org/en/docs/dirindex.html")
|
||||
resp, err := http.Get(nginxURL)
|
||||
if err != nil {
|
||||
log.Println("[Error] fetching page:", err)
|
||||
logger.Errorf("fetching page: %v", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
@ -30,7 +45,7 @@ func main() {
|
|||
// Parse HTML
|
||||
doc, err := html.Parse(resp.Body)
|
||||
if err != nil {
|
||||
log.Println("[Error] parsing HTML:", err)
|
||||
logger.Errorf("parsing HTML: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -103,15 +118,15 @@ func main() {
|
|||
// Write results to JSON file
|
||||
jsonData, err := json.MarshalIndent(directives, "", " ")
|
||||
if err != nil {
|
||||
log.Println("[Error] marshaling JSON:", err)
|
||||
logger.Errorf("marshaling JSON: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = os.WriteFile(outputPath, jsonData, 0644)
|
||||
if err != nil {
|
||||
log.Println("[Error] writing file:", err)
|
||||
logger.Errorf("writing file: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("[OK] Successfully parsed %d directives and saved to %s\n", len(directives), outputPath)
|
||||
logger.Infof("Successfully parsed %d directives and saved to %s\n", len(directives), targetPath)
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:generate go run .
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -7,7 +8,10 @@ import (
|
|||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
)
|
||||
|
||||
// Structure for notification function calls
|
||||
|
@ -21,13 +25,20 @@ type NotificationCall struct {
|
|||
// Directories to exclude
|
||||
var excludeDirs = []string{
|
||||
".devcontainer", ".github", ".idea", ".pnpm-store",
|
||||
".vscode", "app", "query", "tmp",
|
||||
".vscode", "app", "query", "tmp", "cmd",
|
||||
}
|
||||
|
||||
// Main function
|
||||
func main() {
|
||||
// Start scanning from the current directory
|
||||
root := "."
|
||||
logger.Init("release")
|
||||
// Start scanning from the project root
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
logger.Error("Unable to get the current file")
|
||||
return
|
||||
}
|
||||
|
||||
root := filepath.Join(filepath.Dir(file), "../../")
|
||||
calls := []NotificationCall{}
|
||||
|
||||
// Scan all Go files
|
||||
|
@ -38,7 +49,7 @@ func main() {
|
|||
|
||||
// Skip excluded directories
|
||||
for _, dir := range excludeDirs {
|
||||
if strings.HasPrefix(path, "./"+dir) || strings.HasPrefix(path, dir+"/") {
|
||||
if strings.Contains(path, dir) {
|
||||
if info.IsDir() {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
|
@ -55,14 +66,14 @@ func main() {
|
|||
})
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error walking the path: %v\n", err)
|
||||
logger.Errorf("Error walking the path: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Generate a single TS file
|
||||
generateSingleTSFile(calls)
|
||||
generateSingleTSFile(root, calls)
|
||||
|
||||
fmt.Printf("Found %d notification calls\n", len(calls))
|
||||
logger.Infof("Found %d notification calls\n", len(calls))
|
||||
}
|
||||
|
||||
// Find notification function calls in Go files
|
||||
|
@ -71,7 +82,7 @@ func findNotificationCalls(filePath string, calls *[]NotificationCall) {
|
|||
fset := token.NewFileSet()
|
||||
node, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing %s: %v\n", filePath, err)
|
||||
logger.Errorf("Error parsing %s: %v\n", filePath, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -165,12 +176,12 @@ func getStringValue(expr ast.Expr) string {
|
|||
}
|
||||
|
||||
// Generate a single TypeScript file
|
||||
func generateSingleTSFile(calls []NotificationCall) {
|
||||
func generateSingleTSFile(root string, calls []NotificationCall) {
|
||||
// Create target directory
|
||||
targetDir := "app/src/components/Notification"
|
||||
targetDir := filepath.Join(root, "app/src/components/Notification")
|
||||
err := os.MkdirAll(targetDir, 0755)
|
||||
if err != nil {
|
||||
fmt.Printf("Error creating directory %s: %v\n", targetDir, err)
|
||||
logger.Errorf("Error creating directory %s: %v\n", targetDir, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -237,9 +248,9 @@ func generateSingleTSFile(calls []NotificationCall) {
|
|||
// Write file
|
||||
err = os.WriteFile(tsFilePath, []byte(content.String()), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing TS file %s: %v\n", tsFilePath, err)
|
||||
logger.Errorf("Error writing TS file %s: %v\n", tsFilePath, err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Generated single TS file: %s with %d notifications\n", tsFilePath, len(calls))
|
||||
logger.Infof("Generated single TS file: %s with %d notifications\n", tsFilePath, len(calls))
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:generate go run .
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -6,12 +7,14 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
)
|
||||
|
||||
type VersionInfo struct {
|
||||
|
@ -21,54 +24,66 @@ type VersionInfo struct {
|
|||
}
|
||||
|
||||
func main() {
|
||||
logger.Init("release")
|
||||
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
log.Print("Unable to get the current file")
|
||||
logger.Error("Unable to get the current file")
|
||||
return
|
||||
}
|
||||
basePath := path.Join(path.Dir(file), "../../")
|
||||
basePath := filepath.Join(filepath.Dir(file), "../../")
|
||||
|
||||
versionFile, err := os.Open(path.Join(basePath, "app/dist/version.json"))
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
log.Print("\"dist/version.json\" not found, load from src instead")
|
||||
logger.Error("\"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)
|
||||
logger.Fatalf("Failed to open version.json: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
defer func(versionFile fs.File) {
|
||||
err := versionFile.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}(versionFile)
|
||||
defer versionFile.Close()
|
||||
|
||||
// Read the version.json file
|
||||
data, err := io.ReadAll(versionFile)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read version.json: %v", err)
|
||||
logger.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)
|
||||
logger.Fatalf("Failed to parse JSON: %v", err)
|
||||
}
|
||||
|
||||
// get current git commit hash
|
||||
commitHash, err := getGitCommitHash(basePath)
|
||||
if err != nil {
|
||||
logger.Fatalf("Failed to get git commit hash: %v", err)
|
||||
}
|
||||
|
||||
err = generateVersionGenGo(basePath, versionInfo, commitHash)
|
||||
if err != nil {
|
||||
logger.Fatalf("Failed to generate version.gen.go: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func getGitCommitHash(basePath string) (string, error) {
|
||||
cmd := exec.Command("git", "-C", basePath, "rev-parse", "HEAD")
|
||||
commitHash, err := cmd.Output()
|
||||
if err != nil {
|
||||
log.Printf("Failed to get git commit hash: %v", err)
|
||||
commitHash = []byte("")
|
||||
return "", err
|
||||
}
|
||||
|
||||
return strings.TrimRight(string(commitHash), "\r\n"), nil
|
||||
}
|
||||
|
||||
func generateVersionGenGo(basePath string, versionInfo VersionInfo, commitHash string) error {
|
||||
// Generate the version.gen.go file content
|
||||
genContent := fmt.Sprintf(`// Code generated by cmd/version/generate.go; DO NOT EDIT.
|
||||
|
||||
|
@ -82,11 +97,12 @@ func init() {
|
|||
}
|
||||
`, versionInfo.Version, versionInfo.BuildId, versionInfo.TotalBuild, strings.TrimRight(string(commitHash), "\r\n"))
|
||||
|
||||
genPath := path.Join(basePath, "internal/version/version.gen.go")
|
||||
err = os.WriteFile(genPath, []byte(genContent), 0644)
|
||||
genPath := filepath.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)
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("version.gen.go has been generated successfully.")
|
||||
logger.Info("version.gen.go has been generated successfully.")
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue