|
| 1 | +package configuration |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/markusressel/fan2go/internal/ui" |
| 10 | +) |
| 11 | + |
| 12 | +// WarnUnknownKeys checks unused configuration keys, determines if they are sections, |
| 13 | +// finds suggestions for typos, and prints warnings. |
| 14 | +func WarnUnknownKeys(cfg interface{}, unusedKeys []string, getValue func(string) interface{}) { |
| 15 | + re := regexp.MustCompile(`\[(\d+)\]`) |
| 16 | + for _, unused := range unusedKeys { |
| 17 | + key := strings.ToLower(unused) |
| 18 | + viperKey := re.ReplaceAllString(key, ".$1") |
| 19 | + val := getValue(viperKey) |
| 20 | + |
| 21 | + isSection := false |
| 22 | + if val != nil { |
| 23 | + kind := reflect.TypeOf(val).Kind() |
| 24 | + if kind == reflect.Map || kind == reflect.Slice { |
| 25 | + isSection = true |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + suggestion := "" |
| 30 | + pathParts := strings.Split(viperKey, ".") |
| 31 | + if len(pathParts) > 0 { |
| 32 | + unknownPart := pathParts[len(pathParts)-1] |
| 33 | + parentPath := pathParts[:len(pathParts)-1] |
| 34 | + |
| 35 | + parentType := getParentType(parentPath, reflect.TypeOf(cfg)) |
| 36 | + validKeys := getValidKeys(parentType) |
| 37 | + |
| 38 | + closest := getClosestMatch(unknownPart, validKeys) |
| 39 | + if closest != "" { |
| 40 | + suggestion = fmt.Sprintf(" - did you mean '%s'?", closest) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if isSection { |
| 45 | + ui.Warning("Unknown configuration section (and its contents): %s%s", key, suggestion) |
| 46 | + } else { |
| 47 | + ui.Warning("Unknown configuration key: %s%s", key, suggestion) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func levenshtein(a, b string) int { |
| 53 | + d := make([][]int, len(a)+1) |
| 54 | + for i := range d { |
| 55 | + d[i] = make([]int, len(b)+1) |
| 56 | + d[i][0] = i |
| 57 | + } |
| 58 | + for j := range d[0] { |
| 59 | + d[0][j] = j |
| 60 | + } |
| 61 | + for j := 1; j <= len(b); j++ { |
| 62 | + for i := 1; i <= len(a); i++ { |
| 63 | + if a[i-1] == b[j-1] { |
| 64 | + d[i][j] = d[i-1][j-1] |
| 65 | + } else { |
| 66 | + min := d[i-1][j] + 1 |
| 67 | + if d[i][j-1]+1 < min { |
| 68 | + min = d[i][j-1] + 1 |
| 69 | + } |
| 70 | + if d[i-1][j-1]+1 < min { |
| 71 | + min = d[i-1][j-1] + 1 |
| 72 | + } |
| 73 | + d[i][j] = min |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + return d[len(a)][len(b)] |
| 78 | +} |
| 79 | + |
| 80 | +func getClosestMatch(target string, options []string) string { |
| 81 | + closest := "" |
| 82 | + minDist := -1 |
| 83 | + for _, opt := range options { |
| 84 | + dist := levenshtein(target, opt) |
| 85 | + if minDist == -1 || dist < minDist { |
| 86 | + minDist = dist |
| 87 | + closest = opt |
| 88 | + } |
| 89 | + } |
| 90 | + if minDist != -1 && minDist <= 3 && len(target) > 2 { |
| 91 | + return closest |
| 92 | + } |
| 93 | + return "" |
| 94 | +} |
| 95 | + |
| 96 | +func getValidKeys(parentType reflect.Type) []string { |
| 97 | + var keys []string |
| 98 | + if parentType == nil { |
| 99 | + return keys |
| 100 | + } |
| 101 | + for parentType.Kind() == reflect.Pointer { |
| 102 | + parentType = parentType.Elem() |
| 103 | + } |
| 104 | + if parentType.Kind() != reflect.Struct { |
| 105 | + return keys |
| 106 | + } |
| 107 | + for i := 0; i < parentType.NumField(); i++ { |
| 108 | + field := parentType.Field(i) |
| 109 | + if field.PkgPath != "" { // unexported |
| 110 | + continue |
| 111 | + } |
| 112 | + |
| 113 | + name := "" |
| 114 | + for _, tagKey := range []string{"mapstructure", "json"} { |
| 115 | + tag := field.Tag.Get(tagKey) |
| 116 | + if tag != "" && tag != "-" { |
| 117 | + name = strings.Split(tag, ",")[0] |
| 118 | + break |
| 119 | + } |
| 120 | + } |
| 121 | + if name == "" { |
| 122 | + name = strings.ToLower(field.Name) |
| 123 | + } |
| 124 | + keys = append(keys, name) |
| 125 | + } |
| 126 | + return keys |
| 127 | +} |
| 128 | + |
| 129 | +func getParentType(path []string, currentType reflect.Type) reflect.Type { |
| 130 | + for currentType.Kind() == reflect.Pointer { |
| 131 | + currentType = currentType.Elem() |
| 132 | + } |
| 133 | + if len(path) == 0 { |
| 134 | + return currentType |
| 135 | + } |
| 136 | + part := path[0] |
| 137 | + |
| 138 | + switch currentType.Kind() { |
| 139 | + case reflect.Struct: |
| 140 | + for i := 0; i < currentType.NumField(); i++ { |
| 141 | + field := currentType.Field(i) |
| 142 | + name := strings.ToLower(field.Name) |
| 143 | + for _, tagKey := range []string{"mapstructure", "json"} { |
| 144 | + tag := field.Tag.Get(tagKey) |
| 145 | + if tag != "" && tag != "-" { |
| 146 | + tagOpts := strings.Split(tag, ",") |
| 147 | + if strings.ToLower(tagOpts[0]) == part { |
| 148 | + name = strings.ToLower(tagOpts[0]) |
| 149 | + break |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + if name == part { |
| 154 | + return getParentType(path[1:], field.Type) |
| 155 | + } |
| 156 | + } |
| 157 | + return nil |
| 158 | + case reflect.Slice, reflect.Array, reflect.Map: |
| 159 | + return getParentType(path[1:], currentType.Elem()) |
| 160 | + } |
| 161 | + return nil |
| 162 | +} |
0 commit comments