cache copy.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package middleware
  2. // import (
  3. // "fmt"
  4. // "os"
  5. // "path/filepath"
  6. // "regexp"
  7. // "sort"
  8. // "xg_dba/internal/models"
  9. // "github.com/BurntSushi/toml"
  10. // )
  11. // func InitCache(folderPath string) {
  12. // packageModels := make(map[string]models.Cache)
  13. // err := filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
  14. // if err != nil {
  15. // return err
  16. // }
  17. // if info.IsDir() {
  18. // listenFilePath := filepath.Join(path, "listen.toml")
  19. // var connectInfo models.ConnectInfo
  20. // var systemMetaInfo models.SystemMetaInfo
  21. // if fileExists(listenFilePath) {
  22. // if _, err := parseTomlFile(listenFilePath, &connectInfo); err != nil {
  23. // return err
  24. // }
  25. // }
  26. // latestOsInfoFilePath, err := getLatestOsInfoFile(path)
  27. // if err != nil {
  28. // return err
  29. // }
  30. // if latestOsInfoFilePath != "" {
  31. // if _, err := parseTomlFile(latestOsInfoFilePath, &systemMetaInfo); err != nil {
  32. // return err
  33. // }
  34. // }
  35. // host := connectInfo.Ssh.Host
  36. // if host != "" {
  37. // packageModels[host] = models.Cache{
  38. // ConnectInfo: connectInfo,
  39. // SystemMetaInfo: systemMetaInfo,
  40. // }
  41. // }
  42. // }
  43. // return nil
  44. // })
  45. // if err != nil {
  46. // fmt.Printf("Error walking the path %q: %v\n", folderPath, err)
  47. // return
  48. // }
  49. // for host, cache := range packageModels {
  50. // fmt.Printf("Host: %s, Cache: %+v\n", host, cache)
  51. // }
  52. // fmt.Println("个数为: ", len(packageModels))
  53. // }
  54. // func fileExists(filename string) bool {
  55. // info, err := os.Stat(filename)
  56. // if os.IsNotExist(err) {
  57. // return false
  58. // }
  59. // return !info.IsDir()
  60. // }
  61. // func parseTomlFile(filePath string, v interface{}) (toml.MetaData, error) {
  62. // file, err := os.Open(filePath)
  63. // if err != nil {
  64. // return toml.MetaData{}, err
  65. // }
  66. // defer file.Close()
  67. // decoder := toml.NewDecoder(file)
  68. // metaData, err := decoder.Decode(v)
  69. // if err != nil {
  70. // return toml.MetaData{}, err
  71. // }
  72. // return metaData, nil
  73. // }
  74. // func getLatestOsInfoFile(dirPath string) (string, error) {
  75. // pattern := regexp.MustCompile(`^os_info(?:_\d{8}_\d{6})?\.toml$`)
  76. // files, err := os.ReadDir(dirPath)
  77. // if err != nil {
  78. // return "", err
  79. // }
  80. // var matchingFiles []string
  81. // for _, file := range files {
  82. // if !file.IsDir() && pattern.MatchString(file.Name()) {
  83. // matchingFiles = append(matchingFiles, filepath.Join(dirPath, file.Name()))
  84. // }
  85. // }
  86. // if len(matchingFiles) == 0 {
  87. // return "", nil
  88. // }
  89. // sort.Slice(matchingFiles, func(i, j int) bool {
  90. // return matchingFiles[i] > matchingFiles[j]
  91. // })
  92. // return matchingFiles[0], nil
  93. // }