Skip to content

Commit

Permalink
find-fast provides --skip PATTERN to skip entries
Browse files Browse the repository at this point in the history
Ref: #53

Returning the special value `filepath.SkipDir` as an error token is
supported by `filepath.Walk` and by `godirwalk.Walk` to configure the
walk function to not descend further in the directory hierarchy of the
current file system entry.

This change updates the example find-fast program to demonstrate use
of this token error value.
  • Loading branch information
Karrick S. McDermott committed Aug 10, 2020
1 parent 99bb374 commit 1b74140
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion examples/find-fast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"regexp"
"strings"

"github.com/karrick/godirwalk"
"github.com/karrick/golf"
Expand All @@ -19,8 +20,9 @@ import (
var NoColor = os.Getenv("TERM") == "dumb" || !(isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()))

func main() {
optRegex := golf.String("regex", "", "Do not print unless full path matches regex.")
optQuiet := golf.Bool("quiet", false, "Do not print intermediate errors to stderr.")
optRegex := golf.String("regex", "", "Do not print unless full path matches regex.")
optSkip := golf.String("skip", "", "Skip and do not descend into entries with this substring in the pathname")
golf.Parse()

programName, err := os.Executable()
Expand Down Expand Up @@ -54,12 +56,18 @@ func main() {
case nameRE == nil:
// When no name pattern provided, print everything.
options.Callback = func(osPathname string, _ *godirwalk.Dirent) error {
if *optSkip != "" && strings.Contains(osPathname, ".git") {
return filepath.SkipDir
}
_, err := fmt.Println(osPathname)
return err
}
case NoColor:
// Name pattern was provided, but color not permitted.
options.Callback = func(osPathname string, _ *godirwalk.Dirent) error {
if *optSkip != "" && strings.Contains(osPathname, ".git") {
return filepath.SkipDir
}
var err error
if nameRE.FindString(osPathname) != "" {
_, err = fmt.Println(osPathname)
Expand All @@ -71,6 +79,9 @@ func main() {
buf = append(buf, "\033[22m"...) // very first print should set normal intensity

options.Callback = func(osPathname string, _ *godirwalk.Dirent) error {
if *optSkip != "" && strings.Contains(osPathname, ".git") {
return filepath.SkipDir
}
matches := nameRE.FindAllStringSubmatchIndex(osPathname, -1)
if len(matches) == 0 {
return nil // entry does not match pattern
Expand Down

0 comments on commit 1b74140

Please sign in to comment.