/
main.go
executable file
·132 lines (113 loc) · 2.4 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
package main
import (
"fmt"
"io"
"os"
"github.com/spf13/pflag"
"github.com/errata-ai/vale/v3/internal/core"
"github.com/errata-ai/vale/v3/internal/lint"
)
// version is set during the release build process.
var version = "master"
func stat() bool {
stat, err := os.Stdin.Stat()
if err != nil || (stat.Mode()&os.ModeCharDevice) != 0 {
return false
}
return true
}
func looksLikeStdin(s string) int {
isDir := core.IsDir(s)
if !(core.FileExists(s) || isDir) && s != "" {
return 1
} else if isDir {
return 0
}
return -1
}
func doLint(args []string, l *lint.Linter, glob string) ([]*core.File, error) {
var linted []*core.File
var err error
length := len(args)
if length == 1 && looksLikeStdin(args[0]) == 1 { //nolint:gocritic
// Case 1:
//
// $ vale "some text in a string"
linted, err = l.LintString(args[0])
} else if length > 0 {
// Case 2:
//
// $ vale file1 dir1 file2
input := []string{}
for _, file := range args {
status := looksLikeStdin(file)
if status == 1 {
return linted, core.NewE100(
"doLint",
fmt.Errorf("argument '%s' does not exist", file),
)
}
l.HasDir = status == 0
input = append(input, file)
}
linted, err = l.Lint(input, glob)
} else {
// Case 3:
//
// $ cat file.md | vale
stdin, readErr := io.ReadAll(os.Stdin)
if readErr != nil {
return linted, core.NewE100("doLint", readErr)
}
linted, err = l.LintString(string(stdin))
if err != nil {
return linted, core.NewE100("doLint", err)
}
}
return linted, err
}
func handleError(err error) {
ShowError(err, Flags.Output, os.Stderr)
os.Exit(2)
}
func main() {
pflag.Parse()
args := pflag.Args()
argc := len(args)
if Flags.Version { //nolint:gocritic
fmt.Println("vale version " + version)
os.Exit(0)
} else if Flags.Help {
pflag.Usage()
} else if argc == 0 && !stat() {
PrintIntro()
}
if argc > 0 {
cmd, exists := Actions[args[0]]
if exists {
if err := cmd(args[1:], &Flags); err != nil {
handleError(err)
}
os.Exit(0)
}
}
config, err := core.ReadPipeline(&Flags, false)
if err != nil {
handleError(err)
}
linter, err := lint.NewLinter(config)
if err != nil {
handleError(err)
}
linted, err := doLint(args, linter, Flags.Glob)
if err != nil {
handleError(err)
}
hasErrors, err := PrintAlerts(linted, config)
if err != nil {
handleError(err)
} else if hasErrors && !Flags.NoExit {
os.Exit(1)
}
os.Exit(0)
}