-
Notifications
You must be signed in to change notification settings - Fork 0
/
lll.go
94 lines (84 loc) · 1.97 KB
/
lll.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
// Package lll provides validation functions regarding line length
package lll
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"unicode/utf8"
)
// ShouldSkip checks the input and determines if the path should be skipped.
// Use the SkipList to quickly skip paths.
// All directories are skipped, only files are processed.
// If GoOnly is supplied check that the file is a go file.
// Otherwise check so the file is a "text file".
func ShouldSkip(path string, isDir bool, err error,
skipList []string, goOnly bool) (bool, error) {
name := filepath.Base(path)
for _, d := range skipList {
if name == d {
if isDir {
return true, filepath.SkipDir
}
return true, nil
}
}
if isDir || err != nil {
return true, nil
}
if goOnly {
if !strings.HasSuffix(path, ".go") {
return true, nil
}
} else {
b, err := ioutil.ReadFile(path)
if err != nil {
return true, err
}
m := http.DetectContentType(b)
if !strings.Contains(m, "text/") {
return true, nil
}
}
return false, nil
}
// ProcessFile checks all lines in the file and writes an error if the line
// length is greater than MaxLength.
func ProcessFile(w io.Writer, path string, maxLength int, exclude string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer func() {
err := f.Close()
if err != nil {
fmt.Printf("Error closing file: %s\n", err)
}
}()
return Process(f, w, path, maxLength, exclude)
}
// Process checks all lines in the reader and writes an error if the line length
// is greater than MaxLength.
func Process(r io.Reader, w io.Writer, path string, maxLength int, exclude string) error {
l := 1
s := bufio.NewScanner(r)
for s.Scan() {
t := s.Text()
if len(exclude) != 0 && strings.Contains(t, exclude) {
continue
}
c := utf8.RuneCountInString(t)
if c > maxLength {
fmt.Fprintf(w, "%s:%d: line is %d characters\n", path, l, c)
}
l++
}
if err := s.Err(); err != nil {
return err
}
return nil
}