Skip to content

Commit

Permalink
Use fs.WalkDir function
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jul 3, 2021
1 parent 9d02a5a commit 5705e04
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions internal/cmds/lint-whitespace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"io/fs"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strings"

"go.uber.org/multierr"
Expand Down Expand Up @@ -59,38 +57,27 @@ func lintFile(filename string) error {
}

func run() error {
filenames := make(map[string]struct{})
if err := filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
var lintErrs error
if err := fs.WalkDir(os.DirFS("."), ".", func(path string, dirEntry fs.DirEntry, err error) error {
if err != nil {
return err
}
for _, rx := range ignoreRxs {
if rx.MatchString(path) {
if info.IsDir() {
return filepath.SkipDir
if dirEntry.IsDir() {
return fs.SkipDir
}
return nil
}
}
if info.Mode().IsRegular() {
filenames[path] = struct{}{}
if dirEntry.Type().IsRegular() {
lintErrs = multierr.Append(lintErrs, lintFile(path))
}
return nil
}); err != nil {
return err
}

sortedFilenames := make([]string, 0, len(filenames))
for path := range filenames {
sortedFilenames = append(sortedFilenames, path)
}
sort.Strings(sortedFilenames)

var err error
for _, filename := range sortedFilenames {
err = multierr.Append(err, lintFile(filename))
}
return err
return lintErrs
}

func main() {
Expand Down

0 comments on commit 5705e04

Please sign in to comment.