main.go 2.1 KB

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