cache.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 Cache() {
  12. // 指定需要查找的根文件夹路径
  13. folderPath := "C:\\Program_GT\\Code\\Go\\Work\\xugu\\xg_dba\\config"
  14. packageModels := make(map[string]models.Cache)
  15. err := filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
  16. if err != nil {
  17. return err
  18. }
  19. if info.IsDir() {
  20. listenFilePath := filepath.Join(path, "listen.toml")
  21. var connectInfo models.ConnectInfo
  22. var systemMetaInfo models.SystemMetaInfo
  23. if fileExists(listenFilePath) {
  24. if _, err := parseTomlFile(listenFilePath, &connectInfo); err != nil {
  25. return err
  26. }
  27. }
  28. latestOsInfoFilePath, err := getLatestOsInfoFile(path)
  29. if err != nil {
  30. return err
  31. }
  32. if latestOsInfoFilePath != "" {
  33. if _, err := parseTomlFile(latestOsInfoFilePath, &systemMetaInfo); err != nil {
  34. return err
  35. }
  36. }
  37. host := connectInfo.Ssh.Host
  38. if host != "" {
  39. packageModels[host] = models.Cache{
  40. ConnectInfo: connectInfo,
  41. SystemMetaInfo: systemMetaInfo,
  42. }
  43. }
  44. }
  45. return nil
  46. })
  47. if err != nil {
  48. fmt.Printf("Error walking the path %q: %v\n", folderPath, err)
  49. return
  50. }
  51. for host, cache := range packageModels {
  52. fmt.Printf("Host: %s, Cache: %+v\n", host, cache)
  53. }
  54. }
  55. func fileExists(filename string) bool {
  56. info, err := os.Stat(filename)
  57. if os.IsNotExist(err) {
  58. return false
  59. }
  60. return !info.IsDir()
  61. }
  62. func parseTomlFile(filePath string, v interface{}) (toml.MetaData, error) {
  63. file, err := os.Open(filePath)
  64. if err != nil {
  65. return toml.MetaData{}, err
  66. }
  67. defer file.Close()
  68. decoder := toml.NewDecoder(file)
  69. metaData, err := decoder.Decode(v)
  70. if err != nil {
  71. return toml.MetaData{}, err
  72. }
  73. return metaData, nil
  74. }
  75. func getLatestOsInfoFile(dirPath string) (string, error) {
  76. pattern := regexp.MustCompile(`^os_info(?:_\d{8}_\d{6})?\.toml$`)
  77. files, err := os.ReadDir(dirPath)
  78. if err != nil {
  79. return "", err
  80. }
  81. var matchingFiles []string
  82. for _, file := range files {
  83. if !file.IsDir() && pattern.MatchString(file.Name()) {
  84. matchingFiles = append(matchingFiles, filepath.Join(dirPath, file.Name()))
  85. }
  86. }
  87. if len(matchingFiles) == 0 {
  88. return "", nil
  89. }
  90. sort.Slice(matchingFiles, func(i, j int) bool {
  91. return matchingFiles[i] > matchingFiles[j]
  92. })
  93. return matchingFiles[0], nil
  94. }