Skip to content

Commit

Permalink
Replace the abandoned filepathx library with doublestar; ensure glob …
Browse files Browse the repository at this point in the history
…characters in paths do not break globbing (#81)
  • Loading branch information
kylekthompson authored Oct 28, 2024
1 parent 5294685 commit 022939f
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
14 changes: 11 additions & 3 deletions internal/fs/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down
41 changes: 41 additions & 0 deletions internal/fs/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 022939f

Please sign in to comment.