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