|
@@ -8,14 +8,12 @@ import (
|
|
|
"sort"
|
|
|
"xg_dba/internal/models"
|
|
|
|
|
|
- "github.com/BurntSushi/toml"
|
|
|
+ "github.com/mitchellh/mapstructure"
|
|
|
+ "github.com/spf13/viper"
|
|
|
)
|
|
|
|
|
|
-func Cache() {
|
|
|
-
|
|
|
-
|
|
|
- folderPath := "C:\\Program_GT\\Code\\Go\\Work\\xugu\\xg_dba\\config"
|
|
|
-
|
|
|
+
|
|
|
+func InitCache(folderPath string) {
|
|
|
packageModels := make(map[string]models.Cache)
|
|
|
|
|
|
err := filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
|
|
@@ -29,23 +27,27 @@ func Cache() {
|
|
|
var connectInfo models.ConnectInfo
|
|
|
var systemMetaInfo models.SystemMetaInfo
|
|
|
|
|
|
+
|
|
|
if fileExists(listenFilePath) {
|
|
|
- if _, err := parseTomlFile(listenFilePath, &connectInfo); err != nil {
|
|
|
+ if err := parseTomlFile(listenFilePath, &connectInfo); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
latestOsInfoFilePath, err := getLatestOsInfoFile(path)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+
|
|
|
if latestOsInfoFilePath != "" {
|
|
|
- if _, err := parseTomlFile(latestOsInfoFilePath, &systemMetaInfo); err != nil {
|
|
|
+ if err := parseTomlFile(latestOsInfoFilePath, &systemMetaInfo); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
host := connectInfo.Ssh.Host
|
|
|
if host != "" {
|
|
|
packageModels[host] = models.Cache{
|
|
@@ -58,15 +60,17 @@ func Cache() {
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
- fmt.Printf("Error walking the path %q: %v\n", folderPath, err)
|
|
|
+ fmt.Printf("遍历路径 %q 时出错: %v\n", folderPath, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+
|
|
|
for host, cache := range packageModels {
|
|
|
- fmt.Printf("Host: %s, Cache: %+v\n", host, cache)
|
|
|
+ fmt.Printf("主机: %s, 缓存: %+v\n", host, cache)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func fileExists(filename string) bool {
|
|
|
info, err := os.Stat(filename)
|
|
|
if os.IsNotExist(err) {
|
|
@@ -75,21 +79,42 @@ func fileExists(filename string) bool {
|
|
|
return !info.IsDir()
|
|
|
}
|
|
|
|
|
|
-func parseTomlFile(filePath string, v interface{}) (toml.MetaData, error) {
|
|
|
- file, err := os.Open(filePath)
|
|
|
- if err != nil {
|
|
|
- return toml.MetaData{}, err
|
|
|
+
|
|
|
+func parseTomlFile(filePath string, v interface{}) error {
|
|
|
+
|
|
|
+ vp := viper.New()
|
|
|
+ vp.SetConfigFile(filePath)
|
|
|
+ vp.SetConfigType("toml")
|
|
|
+
|
|
|
+ if err := vp.ReadInConfig(); err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
- defer file.Close()
|
|
|
|
|
|
- decoder := toml.NewDecoder(file)
|
|
|
- metaData, err := decoder.Decode(v)
|
|
|
+
|
|
|
+ settings := vp.AllSettings()
|
|
|
+ if err := decodeSettings(settings, v); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func decodeSettings(settings map[string]interface{}, v interface{}) error {
|
|
|
+ decoderConfig := &mapstructure.DecoderConfig{
|
|
|
+ Result: v,
|
|
|
+ TagName: "toml",
|
|
|
+ WeaklyTypedInput: true,
|
|
|
+ }
|
|
|
+ decoder, err := mapstructure.NewDecoder(decoderConfig)
|
|
|
if err != nil {
|
|
|
- return toml.MetaData{}, err
|
|
|
+ return err
|
|
|
}
|
|
|
- return metaData, nil
|
|
|
+ return decoder.Decode(settings)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
func getLatestOsInfoFile(dirPath string) (string, error) {
|
|
|
pattern := regexp.MustCompile(`^os_info(?:_\d{8}_\d{6})?\.toml$`)
|
|
|
files, err := os.ReadDir(dirPath)
|
|
@@ -104,10 +129,12 @@ func getLatestOsInfoFile(dirPath string) (string, error) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
if len(matchingFiles) == 0 {
|
|
|
return "", nil
|
|
|
}
|
|
|
|
|
|
+
|
|
|
sort.Slice(matchingFiles, func(i, j int) bool {
|
|
|
return matchingFiles[i] > matchingFiles[j]
|
|
|
})
|