|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +/** |
| 9 | +* 解析redis服务信息 |
| 10 | + */ |
| 11 | +func ParseInfo(infoString string) (result map[string]map[string]interface{}) { |
| 12 | + result = map[string]map[string]interface{}{} |
| 13 | + lines := strings.Split(infoString, "\n") |
| 14 | + section := "default" |
| 15 | + for i := range lines { |
| 16 | + |
| 17 | + if strings.TrimSpace(lines[i]) == "" { |
| 18 | + continue |
| 19 | + } |
| 20 | + |
| 21 | + if strings.HasPrefix(lines[i], "#") { |
| 22 | + sections := strings.Fields(lines[i]) |
| 23 | + if len(sections) > 1 { |
| 24 | + section = sections[1] |
| 25 | + } |
| 26 | + continue |
| 27 | + } |
| 28 | + |
| 29 | + items := strings.Split(lines[i], ":") |
| 30 | + if len(items) < 1 { |
| 31 | + continue |
| 32 | + } |
| 33 | + if strings.Contains(items[1], ",") { |
| 34 | + valueItems := strings.Split(items[1], ",") |
| 35 | + subMap := make(map[string]interface{}, len(valueItems)) |
| 36 | + for i := range valueItems { |
| 37 | + subValueitems := strings.Split(valueItems[i], "=") |
| 38 | + if len(subValueitems) < 1 { |
| 39 | + continue |
| 40 | + } |
| 41 | + floatValue, err := strconv.ParseFloat(strings.TrimSpace(subValueitems[1]), 10) |
| 42 | + if err == nil { |
| 43 | + subMap[subValueitems[0]] = floatValue |
| 44 | + } else { |
| 45 | + subMap[subValueitems[0]] = strings.TrimSpace(subValueitems[1]) |
| 46 | + } |
| 47 | + } |
| 48 | + if _, ok := result[section]; ok { |
| 49 | + result[section][items[0]] = subMap |
| 50 | + } else { |
| 51 | + result[section] = map[string]interface{}{ |
| 52 | + items[0]: subMap, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + } else { |
| 57 | + floatValue, err := strconv.ParseFloat(strings.TrimSpace(items[1]), 10) |
| 58 | + var vlaue interface{} = strings.TrimSpace(items[1]) |
| 59 | + if err == nil { |
| 60 | + vlaue = floatValue |
| 61 | + } |
| 62 | + if _, ok := result[section]; ok { |
| 63 | + result[section][items[0]] = vlaue |
| 64 | + } else { |
| 65 | + result[section] = map[string]interface{}{ |
| 66 | + items[0]: vlaue, |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return |
| 72 | +} |
0 commit comments