Created
June 28, 2016 07:55
-
-
Save r10r/d68faeee9cd5f7fbb98037aa2475e48f to your computer and use it in GitHub Desktop.
Simple directory listing with include / exclude pattern support.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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