Skip to content

Commit

Permalink
feat: make MatchDirectories clearer (#15)
Browse files Browse the repository at this point in the history
* feat: make MatchDirectories clearer

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: lint

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: funcs

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Feb 3, 2021
1 parent db715ee commit 7408fde
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
21 changes: 20 additions & 1 deletion glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type FileSystem afero.Fs
type globOptions struct {
fs afero.Fs

// if matchDirectories directly is set to true a matching directory will
// if matchDirectories directly is set to true a matching directory will
// be treated just like a matching file. If set to false, a matching directory
// will auto-match all files inside instead of the directory itself.
matchDirectoriesDirectly bool
Expand All @@ -38,9 +38,28 @@ func WithFs(fs FileSystem) OptFunc {
}
}

// MatchDirectoryIncludesContents makes a match on a directory match all
// files inside it as well.
//
// This is the default behavior.
//
// Also check MatchDirectoryAsFile.
func MatchDirectoryIncludesContents(opts *globOptions) {
opts.matchDirectoriesDirectly = false
}

// MatchDirectoryAsFile makes a match on a directory match its name only.
//
// Also check MatchDirectoryIncludesContents.
func MatchDirectoryAsFile(opts *globOptions) {
opts.matchDirectoriesDirectly = true
}

// MatchDirectories determines weather a matching directory should
// result in only the folder name itself being returned (true) or
// in all files inside that folder being returned (false).
//
// Deprecated: use MatchDirectoryIncludesContents and MatchDirectoryAsFile instead.
func MatchDirectories(v bool) OptFunc {
return func(opts *globOptions) {
opts.matchDirectoriesDirectly = v
Expand Down
6 changes: 3 additions & 3 deletions glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func TestGlob(t *testing.T) { // nolint:funlen

t.Run("match directories directly", func(t *testing.T) {
t.Parallel()
matches, err := Glob("/a/{b,c}", MatchDirectories(true), WithFs(testFs(t, []string{
matches, err := Glob("/a/{b,c}", MatchDirectoryAsFile, WithFs(testFs(t, []string{
"/a/b/d",
"/a/b/e/f",
"/a/c",
Expand All @@ -268,7 +268,7 @@ func TestGlob(t *testing.T) { // nolint:funlen

t.Run("match empty directory", func(t *testing.T) {
t.Parallel()
matches, err := Glob("/a/{b,c}", MatchDirectories(true), WithFs(testFs(t, []string{
matches, err := Glob("/a/{b,c}", MatchDirectoryAsFile, WithFs(testFs(t, []string{
"/a/b",
}, []string{
"/a/c",
Expand All @@ -282,7 +282,7 @@ func TestGlob(t *testing.T) { // nolint:funlen

t.Run("pattern ending with star and subdir", func(t *testing.T) {
t.Parallel()
matches, err := Glob("a/*", WithFs(testFs(t, []string{
matches, err := Glob("a/*", MatchDirectoryIncludesContents, WithFs(testFs(t, []string{
"./a/1.txt",
"./a/2.txt",
"./a/3.txt",
Expand Down

0 comments on commit 7408fde

Please sign in to comment.