Skip to content

Commit dd3e271

Browse files
committed
added method FilesPathExcludes and DirsPathExcludes
1 parent 66e951b commit dd3e271

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

filepath.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,27 @@ func DeleteFiles(files ...string) (errs []error) {
269269

270270
// DirsPath method returns all directories absolute path from given base path recursively.
271271
func DirsPath(basePath string, recursive bool) (pdirs []string, err error) {
272+
return DirsPathExcludes(basePath, recursive, Excludes{})
273+
}
274+
275+
// DirsPathExcludes method returns all directories absolute path from given base path recursively
276+
// excluding the excludes list.
277+
func DirsPathExcludes(basePath string, recursive bool, excludes Excludes) (pdirs []string, err error) {
278+
if err = excludes.Validate(); err != nil {
279+
return
280+
}
281+
272282
if recursive {
273283
err = Walk(basePath, func(srcPath string, info os.FileInfo, err error) error {
284+
if excludes.Match(filepath.Base(srcPath)) {
285+
if info.IsDir() {
286+
// excluding directory
287+
return filepath.SkipDir
288+
}
289+
// excluding file
290+
return nil
291+
}
292+
274293
if info.IsDir() {
275294
pdirs = append(pdirs, srcPath)
276295
}
@@ -286,7 +305,7 @@ func DirsPath(basePath string, recursive bool) (pdirs []string, err error) {
286305
}
287306

288307
for _, v := range list {
289-
if v.IsDir() {
308+
if v.IsDir() && !excludes.Match(v.Name()) {
290309
pdirs = append(pdirs, filepath.Join(basePath, v.Name()))
291310
}
292311
}
@@ -296,8 +315,27 @@ func DirsPath(basePath string, recursive bool) (pdirs []string, err error) {
296315

297316
// FilesPath method returns all files absolute path from given base path recursively.
298317
func FilesPath(basePath string, recursive bool) (files []string, err error) {
318+
return FilesPathExcludes(basePath, recursive, Excludes{})
319+
}
320+
321+
// FilesPathExcludes method returns all files absolute path from given base path recursively
322+
// excluding the excludes list.
323+
func FilesPathExcludes(basePath string, recursive bool, excludes Excludes) (files []string, err error) {
324+
if err = excludes.Validate(); err != nil {
325+
return
326+
}
327+
299328
if recursive {
300329
err = Walk(basePath, func(srcPath string, info os.FileInfo, err error) error {
330+
if excludes.Match(filepath.Base(srcPath)) {
331+
if info.IsDir() {
332+
// excluding directory
333+
return filepath.SkipDir
334+
}
335+
// excluding file
336+
return nil
337+
}
338+
301339
if !info.IsDir() {
302340
files = append(files, srcPath)
303341
}
@@ -313,7 +351,7 @@ func FilesPath(basePath string, recursive bool) (files []string, err error) {
313351
}
314352

315353
for _, v := range list {
316-
if !v.IsDir() {
354+
if !v.IsDir() && !excludes.Match(v.Name()) {
317355
files = append(files, filepath.Join(basePath, v.Name()))
318356
}
319357
}

filepath_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ func TestStripExt(t *testing.T) {
219219

220220
name3 := StripExt("")
221221
assert.Equal(t, "", name3)
222+
223+
name4 := StripExt("myname")
224+
assert.Equal(t, "myname", name4)
222225
}
223226

224227
func TestDirPaths(t *testing.T) {
@@ -250,6 +253,10 @@ func TestDirPaths(t *testing.T) {
250253
assert.True(t, IsSliceContainsString(dirs, path11))
251254
assert.True(t, IsSliceContainsString(dirs, path12))
252255
assert.False(t, IsSliceContainsString(dirs, join(path22, "not-exists")))
256+
257+
dirs, err = DirsPathExcludes(join(testdataPath, "dirpaths"), true, Excludes{"level1-2", "level2-2"})
258+
assert.FailOnError(t, err, "unable to get directory list")
259+
assert.True(t, len(dirs) == 6)
253260
}
254261

255262
func TestFilesPath(t *testing.T) {
@@ -284,4 +291,8 @@ func TestFilesPath(t *testing.T) {
284291
files, err = FilesPath(path11, false)
285292
assert.Nil(t, err)
286293
assert.True(t, strings.HasSuffix(files[0], "file11.txt"))
294+
295+
files, err = FilesPathExcludes(join(testdataPath, "dirpaths"), true, Excludes{"file12.txt", "file22.txt"})
296+
assert.Nil(t, err)
297+
assert.True(t, len(files) == 3)
287298
}

guid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var (
3535
)
3636

3737
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
38-
// Global methods
38+
// Package methods
3939
//___________________________________
4040

4141
// NewGUID method returns a new Globally Unique identifier (GUID).

io_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ func TestCloseQuietly(t *testing.T) {
1616

1717
assert.FailOnError(t, err, "")
1818

19-
CloseQuietly(file)
19+
CloseQuietly(file, nil)
2020
}

0 commit comments

Comments
 (0)