-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
161 lines (148 loc) · 3.3 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
153
154
155
156
157
158
159
160
161
package main
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
)
var (
format = flag.String("format", "", "format (github-actions or empty)")
level = flag.String("level", "warning", "GitHub Action level (notice, warning, or error)")
)
func printTyposInFile(tf *TypoFinder, path string) (int, error) {
data, err := os.ReadFile(path)
if err != nil {
return 0, err
}
if !strings.HasPrefix(http.DetectContentType(data), "text/") {
return 0, nil
}
s := bufio.NewScanner(bytes.NewReader(data))
max := 1024 * 1024
s.Buffer(make([]byte, max), max)
lineNumber := 0
n := 0
for s.Scan() {
lineNumber++
if typos := tf.FindTypos(s.Text()); len(typos) > 0 {
n += len(typos)
for _, typo := range typos {
switch *format {
case "github-actions":
if _, err := fmt.Printf("::%s file=%s,line=%d,col=%d::%s: typo of %s\n", *level, path, lineNumber, typo.Index+1, typo.S, tf.word); err != nil {
return n, err
}
default:
if _, err := fmt.Printf("%s:%d:%d: %s\n", path, lineNumber, typo.Index+1, typo.S); err != nil {
return n, err
}
}
}
}
}
switch err := s.Err(); {
case errors.Is(err, bufio.ErrTooLong):
// Ignore too long errors because it means that a line was longer than
// 1MB, so the file is likely not human-readable.
return n, nil
default:
return n, err
}
}
func printTyposInStdin(tf *TypoFinder) (int, error) {
s := bufio.NewScanner(os.Stdin)
max := 1024 * 1024
s.Buffer(make([]byte, max), max)
lineNumber := 0
n := 0
for s.Scan() {
lineNumber++
if typos := tf.FindTypos(s.Text()); len(typos) > 0 {
n += len(typos)
for _, typo := range typos {
switch *format {
case "github-actions":
if _, err := fmt.Printf("::%s line=%d,col=%d::%s: typo of %s\n", *level, lineNumber, typo.Index+1, typo.S, tf.word); err != nil {
return n, err
}
default:
if _, err := fmt.Printf("%d:%d: %s\n", lineNumber, typo.Index+1, typo.S); err != nil {
return n, err
}
}
}
}
}
return n, s.Err()
}
func run() (bool, error) {
flag.Parse()
if len(flag.Args()) < 1 {
fmt.Printf("usage: %s word [path...]\n", filepath.Base(os.Args[0]))
return false, nil
}
tf, err := NewTypoFinder(flag.Arg(0))
if err != nil {
return false, err
}
if flag.NArg() == 1 {
n, err := printTyposInStdin(tf)
if err != nil {
return false, err
}
return n == 0, nil
}
total := 0
fsys := os.DirFS(".")
for _, arg := range flag.Args()[1:] {
switch info, err := os.Stat(arg); {
case err != nil:
return false, err
case info.IsDir():
if err := fs.WalkDir(fsys, arg, func(path string, d fs.DirEntry, err error) error {
switch {
case err != nil:
return err
case d.IsDir():
switch {
case filepath.Base(path) == ".git":
return fs.SkipDir
default:
return nil
}
case d.Type() == 0:
n, err := printTyposInFile(tf, path)
total += n
if err != nil {
return err
}
}
return nil
}); err != nil {
return false, err
}
case info.Mode().IsRegular():
n, err := printTyposInFile(tf, arg)
total += n
if err != nil {
return false, err
}
}
}
return total == 0, nil
}
func main() {
ok, err := run()
if err != nil {
fmt.Println(err)
}
if !ok || err != nil {
os.Exit(1)
}
}