github.com/charlievieth/fastwalk@v1.0.3/examples/fwfind/main.go (about) 1 // fwfind is a an example program that is similar to POSIX find, 2 // but faster and worse (it's an example). 3 package main 4 5 import ( 6 "flag" 7 "fmt" 8 "io/fs" 9 "os" 10 "path/filepath" 11 12 "github.com/charlievieth/fastwalk" 13 ) 14 15 const usageMsg = `Usage: %[1]s [-L] [-name] [PATH...]: 16 17 %[1]s is a poor replacement for the POSIX find utility 18 19 ` 20 21 func main() { 22 flag.Usage = func() { 23 fmt.Fprintf(os.Stdout, usageMsg, filepath.Base(os.Args[0])) 24 flag.PrintDefaults() 25 } 26 pattern := flag.String("name", "", "Pattern to match file names against.") 27 followLinks := flag.Bool("L", false, "Follow symbolic links") 28 flag.Parse() 29 30 // If no paths are provided default to the current directory: "." 31 args := flag.Args() 32 if len(args) == 0 { 33 args = append(args, ".") 34 } 35 36 // Follow links if the "-L" flag is provided 37 conf := fastwalk.Config{ 38 Follow: *followLinks, 39 } 40 41 walkFn := func(path string, d fs.DirEntry, err error) error { 42 if err != nil { 43 fmt.Fprintf(os.Stderr, "%s: %v\n", path, err) 44 return nil // returning the error stops iteration 45 } 46 if *pattern != "" { 47 if ok, err := filepath.Match(*pattern, d.Name()); !ok { 48 // invalid pattern (err != nil) or name does not match 49 return err 50 } 51 } 52 _, err = fmt.Println(path) 53 return err 54 } 55 for _, root := range args { 56 if err := fastwalk.Walk(&conf, root, walkFn); err != nil { 57 fmt.Fprintf(os.Stderr, "%s: %v\n", root, err) 58 os.Exit(1) 59 } 60 } 61 }