diff --git a/README.md b/README.md index 82fcdd5..e94e50f 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,9 @@ supplied to `go-bindata`. Refer to the `testdata/out` directory for various output examples from the assets in `testdata/in`. Each example uses different command line options. +To ignore files, pass in regexes using -ignore, for example: + + $ go-bindata -ignore=\\.gitignore data/... ### Accessing an asset diff --git a/config.go b/config.go index cbc82f0..d2aaae0 100644 --- a/config.go +++ b/config.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" ) // InputConfig defines options on a asset directory to be convert. @@ -120,6 +121,13 @@ type Config struct { // sub directories. This defaults to false, so only files in the // input directory itself are read. Recursive bool + + // Ignores any filenames matching the regex pattern specified, e.g. + // path/to/file.ext will ignore only that file, or \\.gitignore + // will match any .gitignore file. + // + // This parameter can be provided multiple times. + Ignore []*regexp.Regexp } // NewConfig returns a default configuration struct. @@ -131,6 +139,7 @@ func NewConfig() *Config { c.Debug = false c.Recursive = false c.Output = "./bindata.go" + c.Ignore = make([]*regexp.Regexp, 0) return c } diff --git a/convert.go b/convert.go index c9a67e8..0d4dd38 100644 --- a/convert.go +++ b/convert.go @@ -28,7 +28,7 @@ func Translate(c *Config) error { // Locate all the assets. for _, input := range c.Input { - err = findFiles(input.Path, c.Prefix, input.Recursive, &toc) + err = findFiles(input.Path, c.Prefix, input.Recursive, &toc, c.Ignore) if err != nil { return err } @@ -78,7 +78,7 @@ func Translate(c *Config) error { // findFiles recursively finds all the file paths in the given directory tree. // They are added to the given map as keys. Values will be safe function names // for each file, which will be used when generating the output code. -func findFiles(dir, prefix string, recursive bool, toc *[]Asset) error { +func findFiles(dir, prefix string, recursive bool, toc *[]Asset, ignore []*regexp.Regexp) error { if len(prefix) > 0 { dir, _ = filepath.Abs(dir) prefix, _ = filepath.Abs(prefix) @@ -102,9 +102,20 @@ func findFiles(dir, prefix string, recursive bool, toc *[]Asset) error { asset.Path = filepath.Join(dir, file.Name()) asset.Name = filepath.ToSlash(asset.Path) + ignoring := false + for _, re := range ignore { + if re.MatchString(asset.Path) { + ignoring = true + break + } + } + if ignoring { + continue + } + if file.IsDir() { if recursive { - findFiles(asset.Path, prefix, recursive, toc) + findFiles(asset.Path, prefix, recursive, toc, ignore) } continue } diff --git a/go-bindata/AppendSliceValue.go b/go-bindata/AppendSliceValue.go new file mode 100644 index 0000000..f5da495 --- /dev/null +++ b/go-bindata/AppendSliceValue.go @@ -0,0 +1,22 @@ +package main + +import "strings" + +// borrowed from https://github.com/hashicorp/serf/blob/master/command/agent/flag_slice_value.go + +// AppendSliceValue implements the flag.Value interface and allows multiple +// calls to the same variable to append a list. +type AppendSliceValue []string + +func (s *AppendSliceValue) String() string { + return strings.Join(*s, ",") +} + +func (s *AppendSliceValue) Set(value string) error { + if *s == nil { + *s = make([]string, 0, 1) + } + + *s = append(*s, value) + return nil +} diff --git a/go-bindata/main.go b/go-bindata/main.go index 5a16398..2b93d33 100644 --- a/go-bindata/main.go +++ b/go-bindata/main.go @@ -10,6 +10,7 @@ import ( "github.com/jteeuwen/go-bindata" "os" "path/filepath" + "regexp" "strings" ) @@ -46,8 +47,18 @@ func parseArgs() *bindata.Config { flag.BoolVar(&c.NoCompress, "nocompress", c.NoCompress, "Assets will *not* be GZIP compressed when this flag is specified.") flag.StringVar(&c.Output, "o", c.Output, "Optional name of the output file to be generated.") flag.BoolVar(&version, "version", false, "Displays version information.") + + ignore := make([]string, 0) + flag.Var((*AppendSliceValue)(&ignore), "ignore", "Regex pattern to ignore") + flag.Parse() + patterns := make([]*regexp.Regexp, 0) + for _, pattern := range ignore { + patterns = append(patterns, regexp.MustCompile(pattern)) + } + c.Ignore = patterns + if version { fmt.Printf("%s\n", Version()) os.Exit(0)