chore: use go generate

This commit is contained in:
Jacky 2025-04-17 14:27:03 +00:00
parent 61ff392c5c
commit 82da0ef05e
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
20 changed files with 151 additions and 363 deletions

View file

@ -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
}