Skip to content

Commit e134b7c

Browse files
typelessshuLhan
authored andcommitted
Add '-include' regex patterns (#14)
1 parent 45f8101 commit e134b7c

File tree

5 files changed

+55
-16
lines changed

5 files changed

+55
-16
lines changed

config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ type Config struct {
139139
// will match any .gitignore file.
140140
//
141141
// This parameter can be provided multiple times.
142-
Ignore []*regexp.Regexp
142+
Ignore []*regexp.Regexp
143+
Include []*regexp.Regexp
143144

144145
// MD5Checksum is a flag that, when set to true, indicates to calculate
145146
// MD5 checksums for files.
@@ -155,6 +156,7 @@ func NewConfig() *Config {
155156
c.Debug = false
156157
c.Output = "./bindata.go"
157158
c.Ignore = make([]*regexp.Regexp, 0)
159+
c.Include = make([]*regexp.Regexp, 0)
158160
return c
159161
}
160162

convert.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func Translate(c *Config) error {
3131
var visitedPaths = make(map[string]bool)
3232
// Locate all the assets.
3333
for _, input := range c.Input {
34-
err = findFiles(input.Path, c.Prefix, input.Recursive, &toc, c.Ignore, knownFuncs, visitedPaths)
34+
err = findFiles(input.Path, c.Prefix, input.Recursive, &toc, c.Ignore, c.Include, knownFuncs, visitedPaths)
3535
if err != nil {
3636
return err
3737
}
@@ -125,6 +125,7 @@ func findFiles(
125125
recursive bool,
126126
toc *[]Asset,
127127
ignore []*regexp.Regexp,
128+
include []*regexp.Regexp,
128129
knownFuncs map[string]int,
129130
visitedPaths map[string]bool,
130131
) (err error) {
@@ -185,6 +186,15 @@ func findFiles(
185186
break
186187
}
187188
}
189+
190+
for _, re := range include {
191+
if re.MatchString(asset.Path) {
192+
ignoring = false
193+
break
194+
}
195+
ignoring = true
196+
}
197+
188198
if ignoring {
189199
continue
190200
}
@@ -193,7 +203,7 @@ func findFiles(
193203
if recursive {
194204
recursivePath := filepath.Join(dir, file.Name())
195205
visitedPaths[asset.Path] = true
196-
findFiles(recursivePath, prefix, recursive, toc, ignore, knownFuncs, visitedPaths)
206+
findFiles(recursivePath, prefix, recursive, toc, ignore, include, knownFuncs, visitedPaths)
197207
}
198208
continue
199209
}
@@ -212,7 +222,7 @@ func findFiles(
212222

213223
if _, ok := visitedPaths[linkPath]; !ok {
214224
visitedPaths[linkPath] = true
215-
findFiles(asset.Path, prefix, recursive, toc, ignore, knownFuncs, visitedPaths)
225+
findFiles(asset.Path, prefix, recursive, toc, ignore, include, knownFuncs, visitedPaths)
216226
}
217227
continue
218228
}

convert_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestFindFiles(t *testing.T) {
2121
var visitedPaths = make(map[string]bool)
2222
prefix := regexp.MustCompile("testdata/dupname")
2323

24-
err := findFiles("testdata/dupname", prefix, true, &toc, []*regexp.Regexp{}, knownFuncs, visitedPaths)
24+
err := findFiles("testdata/dupname", prefix, true, &toc, []*regexp.Regexp{}, []*regexp.Regexp{}, knownFuncs, visitedPaths)
2525
if err != nil {
2626
t.Errorf("expected to be no error: %+v", err)
2727
}
@@ -38,7 +38,7 @@ func TestFindFilesWithSymlinks(t *testing.T) {
3838
var visitedPaths = make(map[string]bool)
3939
prefix := regexp.MustCompile("testdata/symlinkSrc")
4040

41-
err := findFiles("testdata/symlinkSrc", prefix, true, &tocSrc, []*regexp.Regexp{}, knownFuncs, visitedPaths)
41+
err := findFiles("testdata/symlinkSrc", prefix, true, &tocSrc, []*regexp.Regexp{}, []*regexp.Regexp{}, knownFuncs, visitedPaths)
4242
if err != nil {
4343
t.Errorf("expected to be no error: %+v", err)
4444
}
@@ -47,7 +47,7 @@ func TestFindFilesWithSymlinks(t *testing.T) {
4747
visitedPaths = make(map[string]bool)
4848
prefix = regexp.MustCompile("testdata/symlinkParent")
4949

50-
err = findFiles("testdata/symlinkParent", prefix, true, &tocTarget, []*regexp.Regexp{}, knownFuncs, visitedPaths)
50+
err = findFiles("testdata/symlinkParent", prefix, true, &tocTarget, []*regexp.Regexp{}, []*regexp.Regexp{}, knownFuncs, visitedPaths)
5151
if err != nil {
5252
t.Errorf("expected to be no error: %+v", err)
5353
}
@@ -72,7 +72,7 @@ func TestFindFilesWithRecursiveSymlinks(t *testing.T) {
7272
var visitedPaths = make(map[string]bool)
7373
prefix := regexp.MustCompile("testdata/symlinkRecursiveParent")
7474

75-
err := findFiles("testdata/symlinkRecursiveParent", prefix, true, &toc, []*regexp.Regexp{}, knownFuncs, visitedPaths)
75+
err := findFiles("testdata/symlinkRecursiveParent", prefix, true, &toc, []*regexp.Regexp{}, []*regexp.Regexp{}, knownFuncs, visitedPaths)
7676
if err != nil {
7777
t.Errorf("expected to be no error: %+v", err)
7878
}
@@ -89,7 +89,7 @@ func TestFindFilesWithSymlinkedFile(t *testing.T) {
8989
var visitedPaths = make(map[string]bool)
9090
prefix := regexp.MustCompile("testdata/symlinkFile")
9191

92-
err := findFiles("testdata/symlinkFile", prefix, true, &toc, []*regexp.Regexp{}, knownFuncs, visitedPaths)
92+
err := findFiles("testdata/symlinkFile", prefix, true, &toc, []*regexp.Regexp{}, []*regexp.Regexp{}, knownFuncs, visitedPaths)
9393
if err != nil {
9494
t.Errorf("expected to be no error: %+v", err)
9595
}

go-bindata/main.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ var (
3939

4040
// List of error messages.
4141
var (
42-
ErrInvalidIgnoreRegex = errors.New("Invalid -ignore regex pattern")
43-
ErrInvalidPrefixRegex = errors.New("Invalid -prefix regex pattern")
44-
ErrNoInput = errors.New("Missing <input directories>")
42+
ErrInvalidIgnoreRegex = errors.New("Invalid -ignore regex pattern")
43+
ErrInvalidIncludeRegex = errors.New("Invalid -include regex pattern")
44+
ErrInvalidPrefixRegex = errors.New("Invalid -prefix regex pattern")
45+
ErrNoInput = errors.New("Missing <input directories>")
4546
)
4647

4748
// List of local variables.
4849
var (
4950
argIgnore []string
51+
argInclude []string
5052
argVersion bool
5153
argPrefix string
5254
cfg *bindata.Config
@@ -114,6 +116,7 @@ func initArgs() {
114116
flag.StringVar(&cfg.Tags, "tags", cfg.Tags, "Optional set of build tags to include.")
115117
flag.UintVar(&cfg.Mode, "mode", cfg.Mode, "Optional file mode override for all files.")
116118
flag.Var((*AppendSliceValue)(&argIgnore), "ignore", "Regex pattern to ignore")
119+
flag.Var((*AppendSliceValue)(&argInclude), "include", "Regex pattern to include")
117120
}
118121

119122
//
@@ -151,6 +154,11 @@ func parseArgs() (err error) {
151154
return
152155
}
153156

157+
err = parseInclude()
158+
if err != nil {
159+
return
160+
}
161+
154162
parseOutputPkg()
155163

156164
// Create input configurations.
@@ -190,6 +198,21 @@ func parseIgnore() (err error) {
190198
return
191199
}
192200

201+
func parseInclude() (err error) {
202+
var includeVal *regexp.Regexp
203+
204+
for _, pattern := range argInclude {
205+
includeVal, err = regexp.Compile(pattern)
206+
if err != nil {
207+
return ErrInvalidIncludeRegex
208+
}
209+
210+
cfg.Include = append(cfg.Include, includeVal)
211+
}
212+
213+
return
214+
}
215+
193216
//
194217
// parseOutputPkg will change package name to directory of output, only if
195218
// output flag is set and package flag is not set.

go-bindata/main_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func TestParseArgs(t *testing.T) {
9393
Input: []bindata.InputConfig{{
9494
Path: argInputPath,
9595
}},
96-
Ignore: defConfig.Ignore,
96+
Ignore: defConfig.Ignore,
97+
Include: defConfig.Include,
9798
},
9899
}, {
99100
desc: `With "-pkg ` + argPkg + `"`,
@@ -108,7 +109,8 @@ func TestParseArgs(t *testing.T) {
108109
Input: []bindata.InputConfig{{
109110
Path: argInputPath,
110111
}},
111-
Ignore: defConfig.Ignore,
112+
Ignore: defConfig.Ignore,
113+
Include: defConfig.Include,
112114
},
113115
}, {
114116
desc: `With "-o ` + argOutFile + `" (package name should be "` + argOutPkg + `")`,
@@ -123,7 +125,8 @@ func TestParseArgs(t *testing.T) {
123125
Input: []bindata.InputConfig{{
124126
Path: argInputPath,
125127
}},
126-
Ignore: defConfig.Ignore,
128+
Ignore: defConfig.Ignore,
129+
Include: defConfig.Include,
127130
},
128131
}, {
129132

@@ -140,7 +143,8 @@ func TestParseArgs(t *testing.T) {
140143
Input: []bindata.InputConfig{{
141144
Path: argInputPath,
142145
}},
143-
Ignore: defConfig.Ignore,
146+
Ignore: defConfig.Ignore,
147+
Include: defConfig.Include,
144148
},
145149
}}
146150

0 commit comments

Comments
 (0)