Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make MatchDirectories clearer #15

Merged
merged 3 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 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,32 @@ 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() OptFunc {
return func(opts *globOptions) {
opts.matchDirectoriesDirectly = false
}
}

// MatchDirectoryAsFile makes a match on a directory match its name only.
//
// Also check MatchDirectoryIncludesContents.
func MatchDirectoryAsFile() OptFunc {
return func(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