1
0

main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //go:generate go run .
  2. package main
  3. import (
  4. "archive/zip"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "runtime"
  12. "github.com/spf13/afero"
  13. "github.com/spf13/afero/zipfs"
  14. "github.com/uozi-tech/cosy/logger"
  15. )
  16. const (
  17. repoURL = "https://github.com/go-acme/lego/archive/refs/heads/master.zip"
  18. configDir = "internal/cert/config"
  19. )
  20. func main() {
  21. logger.Init("release")
  22. _, file, _, ok := runtime.Caller(0)
  23. if !ok {
  24. logger.Error("Unable to get the current file")
  25. return
  26. }
  27. basePath := filepath.Join(filepath.Dir(file), "../../")
  28. zipFile, err := downloadAndExtract()
  29. if err != nil {
  30. logger.Errorf("Error downloading and extracting: %v\n", err)
  31. os.Exit(1)
  32. }
  33. if err := copyTomlFiles(zipFile, basePath); err != nil {
  34. logger.Errorf("Error copying TOML files: %v\n", err)
  35. os.Exit(1)
  36. }
  37. logger.Info("Successfully updated provider config")
  38. }
  39. // downloadAndExtract downloads the lego repository and extracts it
  40. func downloadAndExtract() (string, error) {
  41. // Download the file
  42. logger.Info("Downloading lego repository...")
  43. resp, err := http.Get(repoURL)
  44. if err != nil {
  45. return "", err
  46. }
  47. defer resp.Body.Close()
  48. if resp.StatusCode != http.StatusOK {
  49. return "", fmt.Errorf("bad status: %s", resp.Status)
  50. }
  51. // Create the file
  52. out, err := os.CreateTemp("", "lego-master.zip")
  53. if err != nil {
  54. return "", err
  55. }
  56. defer out.Close()
  57. // Write the body to file
  58. _, err = io.Copy(out, resp.Body)
  59. if err != nil {
  60. return "", err
  61. }
  62. return out.Name(), nil
  63. }
  64. func copyTomlFiles(zipFile, basePath string) error {
  65. // Open the zip file
  66. logger.Info("Extracting files...")
  67. zipReader, err := zip.OpenReader(zipFile)
  68. if err != nil {
  69. return err
  70. }
  71. defer zipReader.Close()
  72. // Extract files
  73. zfs := zipfs.New(&zipReader.Reader)
  74. afero.Walk(zfs, "./lego-master/providers", func(path string, info os.FileInfo, err error) error {
  75. if info.IsDir() {
  76. return nil
  77. }
  78. if !strings.HasSuffix(info.Name(), ".toml") {
  79. return nil
  80. }
  81. if err != nil {
  82. return err
  83. }
  84. data, err := afero.ReadFile(zfs, path)
  85. if err != nil {
  86. return err
  87. }
  88. // Write to the destination file
  89. destPath := filepath.Join(basePath, configDir, info.Name())
  90. if err := os.WriteFile(destPath, data, 0644); err != nil {
  91. return err
  92. }
  93. logger.Infof("Copied: %s", info.Name())
  94. return nil
  95. })
  96. // Clean up zip file
  97. return os.Remove(zipFile)
  98. }