Skip to content

Commit

Permalink
fix: quotemetagp
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Nov 7, 2020
1 parent da875e0 commit f43af23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
14 changes: 5 additions & 9 deletions glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,23 @@ import (

// Options allowed to be passed to Glob.
type Options struct {
fs afero.Fs
quoteMeta bool
fs afero.Fs
}

// WithFs allows to provide another afero.Fs implementation to Glob.
func WithFs(fs afero.Fs) Options {
return Options{fs: fs}
}

// QuoteMetaOnly disable globling and only quotes meta characters instead.
func QuoteMetaOnly() Options {
return Options{quoteMeta: true}
// QuoteMeta returns a string that quotes all glob pattern meta characters
// inside the argument text; For example, QuoteMeta(`{foo*}`) returns `\{foo\*\}`.
func QuoteMeta(pattern string) string {
return glob.QuoteMeta(pattern)
}

// Glob returns all files that match the given pattern in the current directory.
func Glob(pattern string, opts ...Options) ([]string, error) {
var options = compileOptions(opts)

pattern = strings.TrimPrefix(pattern, "./")

var fs = options.fs
Expand Down Expand Up @@ -99,9 +98,6 @@ func compileOptions(opts []Options) Options {
if opt.fs != nil {
options.fs = opt.fs
}
if opt.quoteMeta {
options.quoteMeta = true
}
}
return options
}
Expand Down
16 changes: 14 additions & 2 deletions glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,24 @@ func TestGlob(t *testing.T) {
})
}

func globInMemoryFs(pattern string, files []string) ([]string, error) {
func TestQuoteMeta(t *testing.T) {
matches, err := globInMemoryFs(QuoteMeta("{a,b}/c"), []string{
"a/c",
"b/c",
"{a,b}/c",
})
require.NoError(t, err)
require.Equal(t, []string{
"{a,b}/c",
}, matches)
}

func globInMemoryFs(pattern string, files []string, options ...Options) ([]string, error) {
var fs = afero.NewMemMapFs()
if err := createFiles(fs, files); err != nil {
return []string{}, err
}
return Glob(pattern, WithFs(fs))
return Glob(pattern, append(options, WithFs(fs))...)
}

func createFiles(fs afero.Fs, files []string) error {
Expand Down

0 comments on commit f43af23

Please sign in to comment.