/
action.go
215 lines (175 loc) · 4.96 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package lint
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
"github.com/jdkato/twine/strcase"
"github.com/errata-ai/vale/v3/internal/check"
"github.com/errata-ai/vale/v3/internal/core"
)
// Solution is a potential solution to an alert.
type Solution struct {
Suggestions []string `json:"suggestions"`
Error string `json:"error"`
}
type fixer func(core.Alert, *core.Config) ([]string, error)
var fixers = map[string]fixer{
"suggest": suggest,
"replace": replace,
"remove": remove,
"convert": convert,
"edit": edit,
}
// ParseAlert returns a slice of suggestions for the given Vale alert.
func ParseAlert(s string, cfg *core.Config) (Solution, error) {
body := core.Alert{}
resp := Solution{}
err := json.Unmarshal([]byte(s), &body)
if err != nil {
return Solution{}, err
}
suggestions, err := processAlert(body, cfg)
if err != nil {
resp.Error = err.Error()
}
resp.Suggestions = suggestions
return resp, nil
}
func processAlert(alert core.Alert, cfg *core.Config) ([]string, error) {
action := alert.Action.Name
if f, found := fixers[action]; found {
return f(alert, cfg)
}
return []string{}, errors.New("unknown action")
}
func suggest(alert core.Alert, cfg *core.Config) ([]string, error) {
if len(alert.Action.Params) == 0 {
return []string{}, errors.New("no parameters")
}
name := alert.Action.Params[0]
switch name {
case "spellings":
return spelling(alert, cfg)
default:
return script(name, alert, cfg)
}
}
func script(name string, alert core.Alert, cfg *core.Config) ([]string, error) {
var suggestions = []string{}
file := core.FindConfigAsset(cfg, name, core.ActionDir)
if file == "" {
return suggestions, errors.New("script not found")
}
source, err := os.ReadFile(file)
if err != nil {
return suggestions, err
}
script := tengo.NewScript(source)
script.SetImports(stdlib.GetModuleMap("text", "fmt", "math"))
err = script.Add("match", alert.Match)
if err != nil {
return suggestions, err
}
compiled, err := script.Compile()
if err != nil {
return suggestions, err
}
if err = compiled.Run(); err != nil {
return suggestions, err
}
computed := compiled.Get("suggestions").Array()
for _, s := range computed {
suggestions = append(suggestions, s.(string))
}
return suggestions, nil
}
func spelling(alert core.Alert, cfg *core.Config) ([]string, error) {
var suggestions = []string{}
name := strings.Split(alert.Check, ".")
path := filepath.Join(cfg.StylesPath(), name[0], name[1]+".yml")
mgr, err := check.NewManager(cfg)
if err != nil {
return suggestions, err
}
if !strings.Contains(alert.Check, "Vale.") {
err = mgr.AddRuleFromFile(alert.Check, path)
if err != nil {
return suggestions, err
}
}
rule, ok := mgr.Rules()[alert.Check].(check.Spelling)
if !ok {
return suggestions, errors.New("unknown check")
}
return rule.Suggest(alert.Match), nil
}
func replace(alert core.Alert, _ *core.Config) ([]string, error) {
return alert.Action.Params, nil
}
func remove(_ core.Alert, _ *core.Config) ([]string, error) {
return []string{""}, nil
}
func convert(alert core.Alert, _ *core.Config) ([]string, error) {
match := alert.Match
if alert.Action.Params[0] == "simple" {
match = strcase.Simple(match)
}
return []string{match}, nil
}
func edit(alert core.Alert, _ *core.Config) ([]string, error) {
match := alert.Match
if len(alert.Action.Params) == 0 {
return []string{}, errors.New("no parameters")
}
switch name := alert.Action.Params[0]; name {
case "regex":
if len(alert.Action.Params) != 3 {
return []string{}, errors.New("invalid number of parameters")
}
regex, err := regexp.Compile(alert.Action.Params[1])
if err != nil {
return []string{}, err
}
match = regex.ReplaceAllString(match, alert.Action.Params[2])
case "trim_right":
if len(alert.Action.Params) != 2 {
return []string{}, errors.New("invalid number of parameters")
}
match = strings.TrimRight(match, alert.Action.Params[1])
case "trim_left":
if len(alert.Action.Params) != 2 {
return []string{}, errors.New("invalid number of parameters")
}
match = strings.TrimLeft(match, alert.Action.Params[1])
case "trim":
if len(alert.Action.Params) != 2 {
return []string{}, errors.New("invalid number of parameters")
}
match = strings.Trim(match, alert.Action.Params[1])
case "truncate":
if len(alert.Action.Params) != 2 {
return []string{}, errors.New("invalid number of parameters")
}
match = strings.Split(match, alert.Action.Params[1])[0]
case "split":
if len(alert.Action.Params) != 3 {
return []string{}, errors.New("invalid number of parameters")
}
index, err := strconv.Atoi(alert.Action.Params[2])
if err != nil {
return []string{}, err
}
parts := strings.Split(match, alert.Action.Params[1])
if index >= len(parts) {
return []string{}, errors.New("index out of range")
}
match = parts[index]
}
return []string{strings.TrimSpace(match)}, nil
}