Add support for -ignore
This commit is contained in:
parent
212d2a5cdc
commit
7f6fddddd0
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
17
convert.go
17
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
|
||||
}
|
||||
|
|
22
go-bindata/AppendSliceValue.go
Normal file
22
go-bindata/AppendSliceValue.go
Normal 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
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user