Skip to content

Instantly share code, notes, and snippets.

@r10r
Created June 28, 2016 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r10r/d68faeee9cd5f7fbb98037aa2475e48f to your computer and use it in GitHub Desktop.
Save r10r/d68faeee9cd5f7fbb98037aa2475e48f to your computer and use it in GitHub Desktop.
Simple directory listing with include / exclude pattern support.
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
)
func main() {
var excludePattern, includePattern string
flag.StringVar(&excludePattern, "excludes", ``, "Exclude pattern (regexp).")
flag.StringVar(&includePattern, "includes", `.*`, "Include pattern (regexp).")
flag.Parse()
excludeRegexp := regexp.MustCompile(excludePattern)
includeRegexp := regexp.MustCompile(includePattern)
root := flag.Arg(0)
err := filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
if !excludeRegexp.MatchString(path) {
if includeRegexp.MatchString(path) {
fmt.Println(path)
}
}
return nil
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment