Skip to content

Commit

Permalink
cmd/shfmt: refactor directory walking to deduplicate code
Browse files Browse the repository at this point in the history
We used to have the CouldBeScript logic split in two places for no
reason; now it's in a single place under filepath.Walk.

This also lets us unindent the walkPath func, as it no longer needs to
use a closure func.
  • Loading branch information
mvdan committed Sep 29, 2020
1 parent 4f54032 commit 9096347
Showing 1 changed file with 38 additions and 49 deletions.
87 changes: 38 additions & 49 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,26 @@ Utilities:
}
status := 0
for _, path := range flag.Args() {
walk(path, func(err error) {
if err != errChangedWithDiff {
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
switch err := walkPath(path, info); err {
case nil:
case filepath.SkipDir:
return err
case errChangedWithDiff:
status = 1
default:
fmt.Fprintln(os.Stderr, err)
status = 1
}
status = 1
})
return nil
}); err != nil {
// Something went wrong walking the filesystem; stop.
fmt.Fprintln(os.Stderr, err)
return 1
}
}
return status
}
Expand All @@ -221,57 +235,32 @@ func formatStdin(name string) error {

var vcsDir = regexp.MustCompile(`^\.(git|svn|hg)$`)

func walk(path string, onError func(error)) {
info, err := os.Stat(path)
if err != nil {
onError(err)
return
func walkPath(path string, info os.FileInfo) error {
if info.IsDir() && vcsDir.MatchString(info.Name()) {
return filepath.SkipDir
}
if !info.IsDir() {
checkShebang := false
if *find {
conf := fileutil.CouldBeScript(info)
if conf == fileutil.ConfNotScript {
return
}
checkShebang = conf == fileutil.ConfIfShebang
}
if err := formatPath(path, checkShebang); err != nil {
onError(err)
}
return
}
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if useEditorConfig {
props, err := ecQuery.Find(path)
if err != nil {
onError(err)
return nil
}
if info.IsDir() && vcsDir.MatchString(info.Name()) {
return filepath.SkipDir
return err
}
if useEditorConfig {
props, err := ecQuery.Find(path)
if err != nil {
return err
}
if props.Get("ignore") == "true" {
if info.IsDir() {
return filepath.SkipDir
} else {
return nil
}
if props.Get("ignore") == "true" {
if info.IsDir() {
return filepath.SkipDir
} else {
return nil
}
}
conf := fileutil.CouldBeScript(info)
if conf == fileutil.ConfNotScript {
return nil
}
err = formatPath(path, conf == fileutil.ConfIfShebang)
if err != nil && !os.IsNotExist(err) {
onError(err)
}
}
conf := fileutil.CouldBeScript(info)
if conf == fileutil.ConfNotScript {
return nil
})
}
err := formatPath(path, conf == fileutil.ConfIfShebang)
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
}

var ecQuery = editorconfig.Query{
Expand Down

0 comments on commit 9096347

Please sign in to comment.