diff --git a/cmd/lll/main.go b/cmd/lll/main.go index f4918d8..d7e1ccb 100644 --- a/cmd/lll/main.go +++ b/cmd/lll/main.go @@ -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() { @@ -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) } @@ -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 }) diff --git a/lll.go b/lll.go index 2714b45..1f9fcb5 100644 --- a/lll.go +++ b/lll.go @@ -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 @@ -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) }