diff --git a/go.mod b/go.mod index f266c9a1..8e8d4b10 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/onsi/gomega v1.33.1 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.6.1 - github.com/yargevad/filepathx v1.0.0 go.uber.org/zap v1.24.0 golang.org/x/sync v0.7.0 ) @@ -21,6 +20,7 @@ require github.com/mileusna/useragent v1.2.1 require ( github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d github.com/blang/semver/v4 v4.0.0 + github.com/bmatcuk/doublestar/v4 v4.7.1 github.com/mitchellh/go-wordwrap v1.0.1 ) diff --git a/go.sum b/go.sum index e13283fa..86ee387c 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLj github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= +github.com/bmatcuk/doublestar/v4 v4.7.1 h1:fdDeAqgT47acgwd9bd9HxJRDmc9UAmPpc+2m0CXv75Q= +github.com/bmatcuk/doublestar/v4 v4.7.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/bradleyjkemp/cupaloy v2.3.0+incompatible h1:UafIjBvWQmS9i/xRg+CamMrnLTKNzo+bdmT/oH34c2Y= github.com/bradleyjkemp/cupaloy v2.3.0+incompatible/go.mod h1:Au1Xw1sgaJ5iSFktEhYsS0dbQiS1B0/XMXl+42y9Ilk= github.com/caarlos0/env/v7 v7.1.0 h1:9lzTF5amyQeWHZzuZeKlCb5FWSUxpG1js43mhbY8ozg= @@ -57,8 +59,6 @@ github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUq github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/yargevad/filepathx v1.0.0 h1:SYcT+N3tYGi+NvazubCNlvgIPbzAk7i7y2dwg3I5FYc= -github.com/yargevad/filepathx v1.0.0/go.mod h1:BprfX/gpYNJHJfc35GjRRpVcwWXS89gGulUIU5tK3tA= go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= diff --git a/internal/fs/local.go b/internal/fs/local.go index 46ffe5bc..c4f9639e 100644 --- a/internal/fs/local.go +++ b/internal/fs/local.go @@ -7,7 +7,7 @@ import ( "path/filepath" "sort" - "github.com/yargevad/filepathx" + doublestar "github.com/bmatcuk/doublestar/v4" "github.com/rwx-research/captain-cli/internal/errors" ) @@ -39,8 +39,16 @@ func (l Local) CreateTemp(dir string, pattern string) (File, error) { } func (l Local) Glob(pattern string) ([]string, error) { - filepaths, err := filepathx.Glob(pattern) - return filepaths, errors.WithStack(err) + filepaths, err := doublestar.FilepathGlob(pattern) + if err != nil { + return nil, errors.WithStack(err) + } + + if filepaths == nil { + return []string{}, nil + } + + return filepaths, nil } func (l Local) GlobMany(patterns []string) ([]string, error) { diff --git a/internal/fs/local_test.go b/internal/fs/local_test.go index 2a5f2754..2309f655 100644 --- a/internal/fs/local_test.go +++ b/internal/fs/local_test.go @@ -86,6 +86,47 @@ var _ = Describe("fs.GlobMany", func() { "../../test/fixtures/integration-tests/partition/z.rb", })) }) + + It("supports files with special characters", func() { + fs := fs.Local{} + expandedPaths, err := fs.GlobMany([]string{ + "../../test/fixtures/filenames/**/*.txt", + }) + Expect(err).NotTo(HaveOccurred()) + + Expect(expandedPaths).To(Equal([]string{ + "../../test/fixtures/filenames/nested/$ @=:+{}[]^><~#|.txt", + "../../test/fixtures/filenames/nested/**.txt", + "../../test/fixtures/filenames/nested/*.txt", + "../../test/fixtures/filenames/nested/?.txt", + "../../test/fixtures/filenames/nested/[].txt", + "../../test/fixtures/filenames/nested/\\.txt", + })) + }) + + It("supports escaping *s", func() { + fs := fs.Local{} + expandedPaths, err := fs.GlobMany([]string{ + "../../test/fixtures/filenames/**/\\*.txt", + }) + Expect(err).NotTo(HaveOccurred()) + + Expect(expandedPaths).To(Equal([]string{ + "../../test/fixtures/filenames/nested/*.txt", + })) + }) + + It("supports escaping **s", func() { + fs := fs.Local{} + expandedPaths, err := fs.GlobMany([]string{ + "../../test/fixtures/filenames/**/\\*\\*.txt", + }) + Expect(err).NotTo(HaveOccurred()) + + Expect(expandedPaths).To(Equal([]string{ + "../../test/fixtures/filenames/nested/**.txt", + })) + }) }) var _ = Describe("fs.Stat", func() { diff --git a/test/fixtures/filenames/nested/$ @=:+{}[]^><~#|.txt b/test/fixtures/filenames/nested/$ @=:+{}[]^><~#|.txt new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/filenames/nested/**.txt b/test/fixtures/filenames/nested/**.txt new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/filenames/nested/*.txt b/test/fixtures/filenames/nested/*.txt new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/filenames/nested/?.txt b/test/fixtures/filenames/nested/?.txt new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/filenames/nested/[].txt b/test/fixtures/filenames/nested/[].txt new file mode 100644 index 00000000..e69de29b diff --git "a/test/fixtures/filenames/nested/\\.txt" "b/test/fixtures/filenames/nested/\\.txt" new file mode 100644 index 00000000..e69de29b