|
| 1 | +package aspell |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os/exec" |
| 8 | + "slices" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "check-commit/match" |
| 13 | + |
| 14 | + "github.com/fatih/camelcase" |
| 15 | +) |
| 16 | + |
| 17 | +type Aspell struct { |
| 18 | + Mode mode `yaml:"mode"` |
| 19 | + MinLength int `yaml:"min_length"` |
| 20 | + Ignore []string `yaml:"ignore"` |
| 21 | + AllowedWords []string `yaml:"allowed"` |
| 22 | + HelpText string `yaml:"-"` |
| 23 | +} |
| 24 | + |
| 25 | +var ( |
| 26 | + camelCaseOK = map[string]struct{}{ |
| 27 | + "HAProxy": {}, |
| 28 | + "golang": {}, |
| 29 | + "ascii": {}, |
| 30 | + "api": {}, |
| 31 | + } |
| 32 | + camelCaseNotOK = map[string]struct{}{} |
| 33 | +) |
| 34 | + |
| 35 | +func (a Aspell) checkSingle(data string, allowedWords []string) error { |
| 36 | + var words []string |
| 37 | + var badWords []string |
| 38 | + |
| 39 | + checkRes, err := checkWithAspellExec(data) |
| 40 | + if checkRes != "" { |
| 41 | + words = strings.Split(checkRes, "\n") |
| 42 | + } |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + for _, word := range words { |
| 48 | + wordLower := strings.ToLower(word) |
| 49 | + if len(word) < a.MinLength { |
| 50 | + continue |
| 51 | + } |
| 52 | + if _, ok := camelCaseNotOK[wordLower]; ok { |
| 53 | + badWords = append(badWords, wordLower) |
| 54 | + continue |
| 55 | + } |
| 56 | + if _, ok := camelCaseOK[wordLower]; ok { |
| 57 | + continue |
| 58 | + } |
| 59 | + if slices.Contains(a.AllowedWords, wordLower) || slices.Contains(allowedWords, wordLower) { |
| 60 | + continue |
| 61 | + } |
| 62 | + splitted := camelcase.Split(word) |
| 63 | + if len(splitted) < 2 { |
| 64 | + splitted = strings.FieldsFunc(word, func(r rune) bool { |
| 65 | + return r == '_' || r == '-' |
| 66 | + }) |
| 67 | + } |
| 68 | + if len(splitted) > 1 { |
| 69 | + for _, s := range splitted { |
| 70 | + er := a.checkSingle(s, allowedWords) |
| 71 | + if er != nil { |
| 72 | + camelCaseNotOK[wordLower] = struct{}{} |
| 73 | + badWords = append(badWords, word+":"+s) |
| 74 | + break |
| 75 | + } |
| 76 | + } |
| 77 | + } else { |
| 78 | + camelCaseNotOK[wordLower] = struct{}{} |
| 79 | + badWords = append(badWords, word) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if len(badWords) > 0 { |
| 84 | + m := map[string]struct{}{} |
| 85 | + for _, w := range badWords { |
| 86 | + m[w] = struct{}{} |
| 87 | + } |
| 88 | + badWords = []string{} |
| 89 | + for k := range m { |
| 90 | + badWords = append(badWords, k) |
| 91 | + } |
| 92 | + sort.Strings(badWords) |
| 93 | + return fmt.Errorf("aspell: %s", badWords) |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func (a Aspell) Check(subjects []string, commitsFull []string, content []map[string]string) error { |
| 99 | + var response string |
| 100 | + var checks []string |
| 101 | + switch a.Mode { |
| 102 | + case modeDisabled: |
| 103 | + return nil |
| 104 | + case modeSubject: |
| 105 | + checks = subjects |
| 106 | + case modeCommit: |
| 107 | + checks = commitsFull |
| 108 | + case modeAll: |
| 109 | + for _, file := range content { |
| 110 | + for name, v := range file { |
| 111 | + nextFile := false |
| 112 | + for _, filter := range a.Ignore { |
| 113 | + if match.MatchFilter(name, filter) { |
| 114 | + // log.Println("File", name, "in ignore list") |
| 115 | + nextFile = true |
| 116 | + continue |
| 117 | + } |
| 118 | + } |
| 119 | + if nextFile { |
| 120 | + continue |
| 121 | + } |
| 122 | + var imports []string |
| 123 | + if strings.HasSuffix(name, ".go") { |
| 124 | + imports = match.GetImportWordsFromGoFile(name) |
| 125 | + } |
| 126 | + if err := a.checkSingle(v, imports); err != nil { |
| 127 | + log.Println(name, err.Error()) |
| 128 | + response += fmt.Sprintf("%s\n", err) |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + checks = []string{} |
| 133 | + default: |
| 134 | + checks = subjects |
| 135 | + } |
| 136 | + |
| 137 | + for _, subject := range checks { |
| 138 | + if err := a.checkSingle(subject, []string{}); err != nil { |
| 139 | + response += fmt.Sprintf("%s\n", err) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if len(response) > 0 { |
| 144 | + return fmt.Errorf("%s", response) |
| 145 | + } |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func checkWithAspellExec(subject string) (string, error) { |
| 150 | + cmd := exec.Command("aspell", "--list") |
| 151 | + cmd.Stdin = strings.NewReader(subject) |
| 152 | + |
| 153 | + var stdout, stderr bytes.Buffer |
| 154 | + cmd.Stdout = &stdout |
| 155 | + cmd.Stderr = &stderr |
| 156 | + err := cmd.Run() |
| 157 | + if err != nil { |
| 158 | + log.Printf("aspell error: %s, stderr: %s", err, stderr.String()) |
| 159 | + return "", err |
| 160 | + } |
| 161 | + |
| 162 | + return stdout.String() + stderr.String(), nil |
| 163 | +} |
0 commit comments