Skip to content

Commit f8e7977

Browse files
authored
s2: Add block support to commandline tools (#413)
* Add block support to commandline tools * Allow better glob support.
1 parent 501979b commit f8e7977

File tree

6 files changed

+482
-21
lines changed

6 files changed

+482
-21
lines changed

s2/cmd/internal/filepathx/LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2016 The filepathx Authors
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# filepathx
2+
3+
> A small `filepath` extension library that supports double star globbling.
4+
5+
## Documentation
6+
7+
GoDoc: <https://pkg.go.dev/github.com/yargevad/filepathx>
8+
9+
## Install
10+
11+
```bash
12+
go get github.com/yargevad/filepathx
13+
```
14+
15+
## Usage Example
16+
17+
You can use `a/**/*.*` to match everything under the `a` directory
18+
that contains a dot, like so:
19+
20+
```go
21+
package main
22+
23+
import (
24+
"fmt"
25+
"os"
26+
27+
"github.com/yargevad/filepathx"
28+
)
29+
30+
func main() {
31+
if 2 != len(os.Args) {
32+
fmt.Println(len(os.Args), os.Args)
33+
fmt.Fprintf(os.Stderr, "Usage: go build example/find/*.go; ./find <pattern>\n")
34+
os.Exit(1)
35+
return
36+
}
37+
pattern := os.Args[1]
38+
39+
matches, err := filepathx.Glob(pattern)
40+
if err != nil {
41+
panic(err)
42+
}
43+
44+
for _, match := range matches {
45+
fmt.Printf("MATCH: [%v]\n", match)
46+
}
47+
}
48+
```
49+
50+
Given this directory structure:
51+
52+
```bash
53+
find a
54+
```
55+
56+
```txt
57+
a
58+
a/b
59+
a/b/c.d
60+
a/b/c.d/e.f
61+
```
62+
63+
This will be the output:
64+
65+
```bash
66+
go build example/find/*.go
67+
./find 'a/**/*.*'
68+
```
69+
70+
```txt
71+
MATCH: [a/b/c.d]
72+
MATCH: [a/b/c.d/e.f]
73+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Package filepathx adds double-star globbing support to the Glob function from the core path/filepath package.
2+
// You might recognize "**" recursive globs from things like your .gitignore file, and zsh.
3+
// The "**" glob represents a recursive wildcard matching zero-or-more directory levels deep.
4+
package filepathx
5+
6+
import (
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
)
11+
12+
// Globs represents one filepath glob, with its elements joined by "**".
13+
type Globs []string
14+
15+
// Glob adds double-star support to the core path/filepath Glob function.
16+
// It's useful when your globs might have double-stars, but you're not sure.
17+
func Glob(pattern string) ([]string, error) {
18+
if !strings.Contains(pattern, "**") {
19+
// passthru to core package if no double-star
20+
return filepath.Glob(pattern)
21+
}
22+
return Globs(strings.Split(pattern, "**")).Expand()
23+
}
24+
25+
// Expand finds matches for the provided Globs.
26+
func (globs Globs) Expand() ([]string, error) {
27+
var matches = []string{""} // accumulate here
28+
for _, glob := range globs {
29+
var hits []string
30+
var hitMap = map[string]bool{}
31+
for _, match := range matches {
32+
paths, err := filepath.Glob(match + glob)
33+
if err != nil {
34+
return nil, err
35+
}
36+
for _, path := range paths {
37+
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
38+
if err != nil {
39+
return err
40+
}
41+
// save deduped match from current iteration
42+
if _, ok := hitMap[path]; !ok {
43+
hits = append(hits, path)
44+
hitMap[path] = true
45+
}
46+
return nil
47+
})
48+
if err != nil {
49+
return nil, err
50+
}
51+
}
52+
}
53+
matches = hits
54+
}
55+
56+
// fix up return value for nil input
57+
if globs == nil && len(matches) > 0 && matches[0] == "" {
58+
matches = matches[1:]
59+
}
60+
61+
return matches, nil
62+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package filepathx
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestGlob_ZeroDoubleStars_oneMatch(t *testing.T) {
10+
// test passthru to vanilla path/filepath
11+
path := "./a/b/c.d/e.f"
12+
err := os.MkdirAll(path, 0755)
13+
if err != nil {
14+
t.Fatalf("os.MkdirAll: %s", err)
15+
}
16+
matches, err := Glob("./*/*/*.d")
17+
if err != nil {
18+
t.Fatalf("Glob: %s", err)
19+
}
20+
if len(matches) != 1 {
21+
t.Fatalf("got %d matches, expected 1", len(matches))
22+
}
23+
expected := strings.Join([]string{"a", "b", "c.d"}, string(os.PathSeparator))
24+
if matches[0] != expected {
25+
t.Fatalf("matched [%s], expected [%s]", matches[0], expected)
26+
}
27+
}
28+
29+
func TestGlob_OneDoubleStar_oneMatch(t *testing.T) {
30+
// test a single double-star
31+
path := "./a/b/c.d/e.f"
32+
err := os.MkdirAll(path, 0755)
33+
if err != nil {
34+
t.Fatalf("os.MkdirAll: %s", err)
35+
}
36+
matches, err := Glob("./**/*.f")
37+
if err != nil {
38+
t.Fatalf("Glob: %s", err)
39+
}
40+
if len(matches) != 1 {
41+
t.Fatalf("got %d matches, expected 1", len(matches))
42+
}
43+
expected := strings.Join([]string{"a", "b", "c.d", "e.f"}, string(os.PathSeparator))
44+
if matches[0] != expected {
45+
t.Fatalf("matched [%s], expected [%s]", matches[0], expected)
46+
}
47+
}
48+
49+
func TestGlob_OneDoubleStar_twoMatches(t *testing.T) {
50+
// test a single double-star
51+
path := "./a/b/c.d/e.f"
52+
err := os.MkdirAll(path, 0755)
53+
if err != nil {
54+
t.Fatalf("os.MkdirAll: %s", err)
55+
}
56+
matches, err := Glob("./a/**/*.*")
57+
if err != nil {
58+
t.Fatalf("Glob: %s", err)
59+
}
60+
if len(matches) != 2 {
61+
t.Fatalf("got %d matches, expected 2", len(matches))
62+
}
63+
expected := []string{
64+
strings.Join([]string{"a", "b", "c.d"}, string(os.PathSeparator)),
65+
strings.Join([]string{"a", "b", "c.d", "e.f"}, string(os.PathSeparator)),
66+
}
67+
68+
for i, match := range matches {
69+
if match != expected[i] {
70+
t.Fatalf("matched [%s], expected [%s]", match, expected[i])
71+
}
72+
}
73+
}
74+
75+
func TestGlob_TwoDoubleStars_oneMatch(t *testing.T) {
76+
// test two double-stars
77+
path := "./a/b/c.d/e.f"
78+
err := os.MkdirAll(path, 0755)
79+
if err != nil {
80+
t.Fatalf("os.MkdirAll: %s", err)
81+
}
82+
matches, err := Glob("./**/b/**/*.f")
83+
if err != nil {
84+
t.Fatalf("Glob: %s", err)
85+
}
86+
if len(matches) != 1 {
87+
t.Fatalf("got %d matches, expected 1", len(matches))
88+
}
89+
expected := strings.Join([]string{"a", "b", "c.d", "e.f"}, string(os.PathSeparator))
90+
91+
if matches[0] != expected {
92+
t.Fatalf("matched [%s], expected [%s]", matches[0], expected)
93+
}
94+
}
95+
96+
func TestExpand_DirectCall_emptySlice(t *testing.T) {
97+
var empty []string
98+
matches, err := Globs(empty).Expand()
99+
if err != nil {
100+
t.Fatalf("Glob: %s", err)
101+
}
102+
if len(matches) != 0 {
103+
t.Fatalf("got %d matches, expected 0", len(matches))
104+
}
105+
}

0 commit comments

Comments
 (0)