main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. )
  9. func main() {
  10. // Define base directory for map components
  11. baseDir := "app/src/views/nginx_log/dashboard/components"
  12. // Map configurations
  13. maps := []struct {
  14. name string
  15. url string
  16. jsonFile string
  17. }{
  18. {
  19. name: "WorldMapChart",
  20. url: "https://cdn.jsdelivr.net/npm/echarts/map/json/world.json",
  21. jsonFile: "world.json",
  22. },
  23. {
  24. name: "ChinaMapChart",
  25. url: "https://cdn.jsdelivr.net/npm/echarts/map/json/china.json",
  26. jsonFile: "china.json",
  27. },
  28. }
  29. for _, mapConfig := range maps {
  30. // Create directory for the map component
  31. mapDir := filepath.Join(baseDir, mapConfig.name)
  32. if err := os.MkdirAll(mapDir, 0755); err != nil {
  33. fmt.Printf("Failed to create directory %s: %v\n", mapDir, err)
  34. continue
  35. }
  36. // Download JSON data
  37. jsonPath := filepath.Join(mapDir, mapConfig.jsonFile)
  38. if err := downloadFile(mapConfig.url, jsonPath); err != nil {
  39. fmt.Printf("Failed to download %s: %v\n", mapConfig.url, err)
  40. continue
  41. }
  42. fmt.Printf("Downloaded %s to %s\n", mapConfig.url, jsonPath)
  43. }
  44. fmt.Println("Map generator completed successfully!")
  45. }
  46. // downloadFile downloads a file from URL and saves it to the specified path
  47. func downloadFile(url, filepath string) error {
  48. // Get the data
  49. resp, err := http.Get(url)
  50. if err != nil {
  51. return err
  52. }
  53. defer resp.Body.Close()
  54. // Check server response
  55. if resp.StatusCode != http.StatusOK {
  56. return fmt.Errorf("bad status: %s", resp.Status)
  57. }
  58. // Create the file
  59. out, err := os.Create(filepath)
  60. if err != nil {
  61. return err
  62. }
  63. defer out.Close()
  64. // Write the body to file
  65. _, err = io.Copy(out, resp.Body)
  66. return err
  67. }
  68. // moveFile moves a file from src to dst
  69. func moveFile(src, dst string) error {
  70. // Check if source file exists
  71. if _, err := os.Stat(src); os.IsNotExist(err) {
  72. return fmt.Errorf("source file does not exist: %s", src)
  73. }
  74. // Attempt to rename first (fastest if on same filesystem)
  75. if err := os.Rename(src, dst); err == nil {
  76. return nil
  77. }
  78. // If rename fails, copy and delete
  79. return copyAndDelete(src, dst)
  80. }
  81. // copyAndDelete copies a file and then deletes the original
  82. func copyAndDelete(src, dst string) error {
  83. // Open source file
  84. srcFile, err := os.Open(src)
  85. if err != nil {
  86. return err
  87. }
  88. defer srcFile.Close()
  89. // Create destination file
  90. dstFile, err := os.Create(dst)
  91. if err != nil {
  92. return err
  93. }
  94. defer dstFile.Close()
  95. // Copy the content
  96. _, err = io.Copy(dstFile, srcFile)
  97. if err != nil {
  98. return err
  99. }
  100. // Sync to ensure all data is written
  101. if err := dstFile.Sync(); err != nil {
  102. return err
  103. }
  104. // Remove source file
  105. return os.Remove(src)
  106. }