-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
main.go
152 lines (139 loc) · 3.45 KB
/
main.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/errata-ai/vale/check"
"github.com/errata-ai/vale/core"
"github.com/errata-ai/vale/lint"
"github.com/errata-ai/vale/ui"
"github.com/urfave/cli"
)
// version is set during the release build process.
var version = "master"
func main() {
var glob string
config := core.LoadConfig()
app := cli.NewApp()
app.Name = "vale"
app.Usage = "A command-line linter for prose."
app.Version = version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "glob",
Value: "*",
Usage: `a glob pattern (e.g., --glob='*.{md,txt}')`,
Destination: &glob,
},
cli.StringFlag{
Name: "output",
Value: "CLI",
Usage: `output style ("line" or "JSON")`,
Destination: &config.Output,
},
cli.StringFlag{
Name: "ext",
Value: ".txt",
Usage: `extension to associate with stdin`,
Destination: &config.InExt,
},
cli.BoolFlag{
Name: "no-wrap",
Usage: "don't wrap CLI output",
Destination: &config.Wrap,
},
cli.BoolFlag{
Name: "no-exit",
Usage: "don't return a nonzero exit code on lint errors",
Destination: &config.NoExit,
},
cli.BoolFlag{
Name: "sort",
Usage: "sort files by their name in output",
Destination: &config.Sorted,
},
cli.BoolFlag{
Name: "normalize",
Usage: "replace each path separator with a slash ('/')",
Destination: &config.Normalize,
},
cli.BoolFlag{
Name: "ignore-syntax",
Usage: "lint all files line-by-line",
Destination: &config.Simple,
},
cli.BoolFlag{
Name: "relative",
Usage: "return relative paths",
Destination: &config.Relative,
},
}
app.Commands = []cli.Command{
{
Name: "dump-config",
Aliases: []string{"dc"},
Usage: "Dumps configuration options to stdout and exits",
Action: func(c *cli.Context) error {
fmt.Println(core.DumpConfig(config))
return nil
},
},
{
Name: "new",
Usage: "Generates a template for the given extension point",
Action: func(c *cli.Context) error {
name := c.Args().First()
template := check.GetTemplate(name)
if template != "" {
fmt.Println(template)
} else {
fmt.Printf(
"'%s' not in %v!\n", name, check.GetExtenionPoints())
}
return nil
},
},
}
app.Action = func(c *cli.Context) error {
var linted []*core.File
var err error
var hasAlerts bool
if c.NArg() > 0 || core.Stat() {
linter := lint.Linter{
Config: config, CheckManager: check.NewManager(config)}
if c.NArg() > 0 {
if core.LooksLikeStdin(c.Args()[0]) {
linted, err = linter.LintString(c.Args()[0])
} else {
linted, err = linter.Lint(c.Args(), glob)
}
} else {
stdin, _ := ioutil.ReadAll(os.Stdin)
linted, err = linter.LintString(string(stdin))
}
// How should we style the output?
if config.Output == "line" {
hasAlerts = ui.PrintLineAlerts(linted, config.Relative)
} else if config.Output == "JSON" {
hasAlerts = ui.PrintJSONAlerts(linted)
} else {
hasAlerts = ui.PrintVerboseAlerts(linted, config.Wrap)
}
// Should return a nonzero vale on errors?
if err == nil && hasAlerts && !config.NoExit {
err = errors.New("")
}
return err
}
cli.ShowAppHelp(c)
return nil
}
core.ExeDir, _ = filepath.Abs(filepath.Dir(os.Args[0]))
if app.Run(os.Args) != nil {
os.Exit(1)
} else {
os.Exit(0)
}
}