Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a crash case and added an exclude optional parameter #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/lll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var args struct {
SkipList []string `arg:"-s,env,help:list of dirs to skip"`
Vendor bool `arg:"env,help:check files in vendor directory"`
Files bool `arg:"help:read file names from stdin one at each line"`
Exclude string `arg:"env,help:exclude lines with this substring"`
}

func main() {
Expand All @@ -38,7 +39,7 @@ func main() {
if args.Files {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
err := lll.ProcessFile(os.Stdout, s.Text(), args.MaxLength)
err := lll.ProcessFile(os.Stdout, s.Text(), args.MaxLength, args.Exclude)
if err != nil {
fmt.Fprintf(os.Stderr, "Error processing file: %s\n", err)
}
Expand All @@ -49,12 +50,16 @@ func main() {
// Otherwise, walk the inputs recursively
for _, d := range args.Input {
err := filepath.Walk(d, func(p string, i os.FileInfo, e error) error {
if e != nil {
return e
}

skip, ret := lll.ShouldSkip(p, i.IsDir(), e, args.SkipList, args.GoOnly)
if skip {
return ret
}

err := lll.ProcessFile(os.Stdout, p, args.MaxLength)
err := lll.ProcessFile(os.Stdout, p, args.MaxLength, args.Exclude)
return err
})

Expand Down
12 changes: 8 additions & 4 deletions lll.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func ShouldSkip(path string, isDir bool, err error,

// 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) error {
func ProcessFile(w io.Writer, path string, maxLength int, exclude string) error {
f, err := os.Open(path)
if err != nil {
return err
Expand All @@ -66,16 +66,20 @@ func ProcessFile(w io.Writer, path string, maxLength int) error {
}
}()

return Process(f, w, path, maxLength)
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) error {
func Process(r io.Reader, w io.Writer, path string, maxLength int, exclude string) error {
l := 1
s := bufio.NewScanner(r)
for s.Scan() {
c := utf8.RuneCountInString(s.Text())
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)
}
Expand Down