Skip to content

Commit

Permalink
feat: options
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 6, 2020
1 parent 1e164c3 commit da875e0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
42 changes: 37 additions & 5 deletions glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,32 @@ import (
"github.com/spf13/afero"
)

func Glob(pattern string) ([]string, error) {
return GlobWithFs(afero.NewOsFs(), pattern)
// Options allowed to be passed to Glob.
type Options struct {
fs afero.Fs
quoteMeta bool
}

func GlobWithFs(fs afero.Fs, pattern string) ([]string, error) {
var matches []string
// 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}
}

// 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, "./")
matcher, err := glob.Compile(pattern)

var fs = options.fs
var matches []string

matcher, err := glob.Compile(pattern, filepath.Separator)
if err != nil {
return matches, err
}
Expand Down Expand Up @@ -74,6 +91,21 @@ func GlobWithFs(fs afero.Fs, pattern string) ([]string, error) {
})
}

func compileOptions(opts []Options) Options {
var options = Options{
fs: afero.NewOsFs(),
}
for _, opt := range opts {
if opt.fs != nil {
options.fs = opt.fs
}
if opt.quoteMeta {
options.quoteMeta = true
}
}
return options
}

func filesInDirectory(fs afero.Fs, dir string) ([]string, error) {
var files []string

Expand Down
2 changes: 1 addition & 1 deletion glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func globInMemoryFs(pattern string, files []string) ([]string, error) {
if err := createFiles(fs, files); err != nil {
return []string{}, err
}
return GlobWithFs(fs, pattern)
return Glob(pattern, WithFs(fs))
}

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

0 comments on commit da875e0

Please sign in to comment.