/
command.go
224 lines (180 loc) · 4.44 KB
/
command.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
216
217
218
219
220
221
222
223
224
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/jdkato/twine/nlp/tag"
"github.com/pterm/pterm"
"github.com/errata-ai/vale/v2/internal/check"
"github.com/errata-ai/vale/v2/internal/core"
"github.com/errata-ai/vale/v2/internal/lint"
"github.com/errata-ai/vale/v2/internal/nlp"
)
// TaggedWord is a word with an NLP context.
type TaggedWord struct {
Token tag.Token
Line int
Span []int
}
// CompiledRule is a rule's compiled regex.
type CompiledRule struct {
Pattern string
}
var commandInfo = map[string]string{
"ls-config": "Print the current configuration to stdout.",
"ls-metrics": "Print the given file's internal metrics to stdout.",
"sync": "Download and install external configuration sources.",
"fix": "Attempt to automatically fix the given alert.",
}
// Actions are the available CLI commands.
var Actions = map[string]func(args []string, flags *core.CLIFlags) error{
"ls-config": printConfig,
"ls-metrics": printMetrics,
"dc": printConfig,
"tag": runTag,
"sync": sync,
"fix": fix,
// private
"compile": compileRule,
"run": runRule,
}
func fix(args []string, flags *core.CLIFlags) error {
if len(args) != 1 {
return core.NewE100("fix", errors.New("one argument expected"))
}
alert := args[0]
if core.FileExists(args[0]) {
b, err := os.ReadFile(args[0])
if err != nil {
return err
}
alert = string(b)
}
cfg, err := core.ReadPipeline(flags, false)
if err != nil {
return err
}
resp, err := lint.ParseAlert(alert, cfg)
if err != nil {
return err
}
return printJSON(resp)
}
func sync(_ []string, flags *core.CLIFlags) error {
cfg, err := core.ReadPipeline(flags, true)
if err != nil {
return err
} else if err = initPath(cfg); err != nil {
return err
}
pkgs, err := core.GetPackages(cfg.Flags.Path)
if err != nil {
return err
}
p, err := pterm.DefaultProgressbar.WithTotal(
len(pkgs)).WithTitle("Downloading packages").Start()
if err != nil {
return err
}
for idx, pkg := range pkgs {
if err = readPkg(pkg, cfg.StylesPath, idx); err != nil {
return err
}
name := fileNameWithoutExt(pkg)
pterm.Success.Println("Downloaded package '" + name + "'")
p.Increment()
}
return nil
}
func printConfig(_ []string, flags *core.CLIFlags) error {
cfg, err := core.ReadPipeline(flags, false)
if err != nil {
return err
}
fmt.Println(cfg.String())
return nil
}
func printMetrics(args []string, _ *core.CLIFlags) error {
if len(args) != 1 {
return core.NewE100("ls-metrics", errors.New("one argument expected"))
} else if !core.FileExists(args[0]) {
return errors.New("file not found")
}
cfg, err := core.NewConfig(&core.CLIFlags{})
if err != nil {
return err
}
cfg.MinAlertLevel = 0
cfg.GBaseStyles = []string{"Vale"}
cfg.Flags.InExt = ".txt" // default value
linter, err := lint.NewLinter(cfg)
if err != nil {
return err
}
linted, err := linter.Lint([]string{args[0]}, "*")
if err != nil {
return err
}
computed, _ := linted[0].ComputeMetrics()
return printJSON(computed)
}
func runTag(args []string, _ *core.CLIFlags) error {
if len(args) != 3 {
return core.NewE100("tag", errors.New("three arguments expected"))
}
text, err := os.ReadFile(args[0])
if err != nil {
return err
}
out := core.TextToContext(
string(text), &nlp.Info{Lang: args[1], Endpoint: args[2]})
return printJSON(out)
}
func compileRule(args []string, _ *core.CLIFlags) error {
if len(args) != 1 {
return core.NewE100("compile", errors.New("one argument expected"))
}
cfg, err := core.NewConfig(&core.CLIFlags{})
if err != nil {
return err
}
path := args[0]
name := filepath.Base(path)
mgr, err := check.NewManager(cfg)
if err != nil {
return err
}
err = mgr.AddRuleFromFile(name, path)
if err != nil {
return err
}
rule := CompiledRule{Pattern: mgr.Rules()[name].Pattern()}
return printJSON(rule)
}
func runRule(args []string, _ *core.CLIFlags) error {
if len(args) != 2 {
return core.NewE100("run", errors.New("two arguments expected"))
}
cfg, err := core.NewConfig(&core.CLIFlags{})
if err != nil {
return err
}
cfg.MinAlertLevel = 0
cfg.GBaseStyles = []string{"Test"}
cfg.Flags.InExt = ".txt" // default value
linter, err := lint.NewLinter(cfg)
if err != nil {
return err
}
err = linter.Manager.AddRuleFromFile("Test.Rule", args[0])
if err != nil {
return err
}
linted, err := linter.Lint([]string{args[1]}, "*")
if err != nil {
return err
}
PrintJSONAlerts(linted)
return nil
}