Add support for -ignore

This commit is contained in:
Ian Kent 2014-04-29 23:42:13 +01:00
parent 212d2a5cdc
commit 7f6fddddd0
5 changed files with 59 additions and 3 deletions

View File

@ -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 output examples from the assets in `testdata/in`. Each example uses different
command line options. command line options.
To ignore files, pass in regexes using -ignore, for example:
$ go-bindata -ignore=\\.gitignore data/...
### Accessing an asset ### Accessing an asset

View File

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
) )
// InputConfig defines options on a asset directory to be convert. // 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 // sub directories. This defaults to false, so only files in the
// input directory itself are read. // input directory itself are read.
Recursive bool 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. // NewConfig returns a default configuration struct.
@ -131,6 +139,7 @@ func NewConfig() *Config {
c.Debug = false c.Debug = false
c.Recursive = false c.Recursive = false
c.Output = "./bindata.go" c.Output = "./bindata.go"
c.Ignore = make([]*regexp.Regexp, 0)
return c return c
} }

View File

@ -28,7 +28,7 @@ func Translate(c *Config) error {
// Locate all the assets. // Locate all the assets.
for _, input := range c.Input { 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 { if err != nil {
return err return err
} }
@ -78,7 +78,7 @@ func Translate(c *Config) error {
// findFiles recursively finds all the file paths in the given directory tree. // 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 // 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. // 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 { if len(prefix) > 0 {
dir, _ = filepath.Abs(dir) dir, _ = filepath.Abs(dir)
prefix, _ = filepath.Abs(prefix) 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.Path = filepath.Join(dir, file.Name())
asset.Name = filepath.ToSlash(asset.Path) 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 file.IsDir() {
if recursive { if recursive {
findFiles(asset.Path, prefix, recursive, toc) findFiles(asset.Path, prefix, recursive, toc, ignore)
} }
continue continue
} }

View File

@ -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
}

View File

@ -10,6 +10,7 @@ import (
"github.com/jteeuwen/go-bindata" "github.com/jteeuwen/go-bindata"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strings" "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.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.StringVar(&c.Output, "o", c.Output, "Optional name of the output file to be generated.")
flag.BoolVar(&version, "version", false, "Displays version information.") flag.BoolVar(&version, "version", false, "Displays version information.")
ignore := make([]string, 0)
flag.Var((*AppendSliceValue)(&ignore), "ignore", "Regex pattern to ignore")
flag.Parse() flag.Parse()
patterns := make([]*regexp.Regexp, 0)
for _, pattern := range ignore {
patterns = append(patterns, regexp.MustCompile(pattern))
}
c.Ignore = patterns
if version { if version {
fmt.Printf("%s\n", Version()) fmt.Printf("%s\n", Version())
os.Exit(0) os.Exit(0)