Skip to content

Commit

Permalink
Merge pull request #1 from goreleaser/options
Browse files Browse the repository at this point in the history
feat: options
  • Loading branch information
caarlos0 authored Nov 7, 2020
2 parents 1e164c3 + f43af23 commit c55480a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
38 changes: 33 additions & 5 deletions glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,31 @@ 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
}

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}
}

// 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, "./")
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 +90,18 @@ 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
}
}
return options
}

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

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 GlobWithFs(fs, pattern)
return Glob(pattern, append(options, WithFs(fs))...)
}

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

0 comments on commit c55480a

Please sign in to comment.