-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
s2: Add block support to commandline tools (#413)
* Add block support to commandline tools * Allow better glob support.
- Loading branch information
Showing
6 changed files
with
482 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2016 The filepathx Authors | ||
|
||
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: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# filepathx | ||
|
||
> A small `filepath` extension library that supports double star globbling. | ||
## Documentation | ||
|
||
GoDoc: <https://pkg.go.dev/github.com/yargevad/filepathx> | ||
|
||
## Install | ||
|
||
```bash | ||
go get github.com/yargevad/filepathx | ||
``` | ||
|
||
## Usage Example | ||
|
||
You can use `a/**/*.*` to match everything under the `a` directory | ||
that contains a dot, like so: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/yargevad/filepathx" | ||
) | ||
|
||
func main() { | ||
if 2 != len(os.Args) { | ||
fmt.Println(len(os.Args), os.Args) | ||
fmt.Fprintf(os.Stderr, "Usage: go build example/find/*.go; ./find <pattern>\n") | ||
os.Exit(1) | ||
return | ||
} | ||
pattern := os.Args[1] | ||
|
||
matches, err := filepathx.Glob(pattern) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, match := range matches { | ||
fmt.Printf("MATCH: [%v]\n", match) | ||
} | ||
} | ||
``` | ||
|
||
Given this directory structure: | ||
|
||
```bash | ||
find a | ||
``` | ||
|
||
```txt | ||
a | ||
a/b | ||
a/b/c.d | ||
a/b/c.d/e.f | ||
``` | ||
|
||
This will be the output: | ||
|
||
```bash | ||
go build example/find/*.go | ||
./find 'a/**/*.*' | ||
``` | ||
|
||
```txt | ||
MATCH: [a/b/c.d] | ||
MATCH: [a/b/c.d/e.f] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Package filepathx adds double-star globbing support to the Glob function from the core path/filepath package. | ||
// You might recognize "**" recursive globs from things like your .gitignore file, and zsh. | ||
// The "**" glob represents a recursive wildcard matching zero-or-more directory levels deep. | ||
package filepathx | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// Globs represents one filepath glob, with its elements joined by "**". | ||
type Globs []string | ||
|
||
// Glob adds double-star support to the core path/filepath Glob function. | ||
// It's useful when your globs might have double-stars, but you're not sure. | ||
func Glob(pattern string) ([]string, error) { | ||
if !strings.Contains(pattern, "**") { | ||
// passthru to core package if no double-star | ||
return filepath.Glob(pattern) | ||
} | ||
return Globs(strings.Split(pattern, "**")).Expand() | ||
} | ||
|
||
// Expand finds matches for the provided Globs. | ||
func (globs Globs) Expand() ([]string, error) { | ||
var matches = []string{""} // accumulate here | ||
for _, glob := range globs { | ||
var hits []string | ||
var hitMap = map[string]bool{} | ||
for _, match := range matches { | ||
paths, err := filepath.Glob(match + glob) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, path := range paths { | ||
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
// save deduped match from current iteration | ||
if _, ok := hitMap[path]; !ok { | ||
hits = append(hits, path) | ||
hitMap[path] = true | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
matches = hits | ||
} | ||
|
||
// fix up return value for nil input | ||
if globs == nil && len(matches) > 0 && matches[0] == "" { | ||
matches = matches[1:] | ||
} | ||
|
||
return matches, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package filepathx | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGlob_ZeroDoubleStars_oneMatch(t *testing.T) { | ||
// test passthru to vanilla path/filepath | ||
path := "./a/b/c.d/e.f" | ||
err := os.MkdirAll(path, 0755) | ||
if err != nil { | ||
t.Fatalf("os.MkdirAll: %s", err) | ||
} | ||
matches, err := Glob("./*/*/*.d") | ||
if err != nil { | ||
t.Fatalf("Glob: %s", err) | ||
} | ||
if len(matches) != 1 { | ||
t.Fatalf("got %d matches, expected 1", len(matches)) | ||
} | ||
expected := strings.Join([]string{"a", "b", "c.d"}, string(os.PathSeparator)) | ||
if matches[0] != expected { | ||
t.Fatalf("matched [%s], expected [%s]", matches[0], expected) | ||
} | ||
} | ||
|
||
func TestGlob_OneDoubleStar_oneMatch(t *testing.T) { | ||
// test a single double-star | ||
path := "./a/b/c.d/e.f" | ||
err := os.MkdirAll(path, 0755) | ||
if err != nil { | ||
t.Fatalf("os.MkdirAll: %s", err) | ||
} | ||
matches, err := Glob("./**/*.f") | ||
if err != nil { | ||
t.Fatalf("Glob: %s", err) | ||
} | ||
if len(matches) != 1 { | ||
t.Fatalf("got %d matches, expected 1", len(matches)) | ||
} | ||
expected := strings.Join([]string{"a", "b", "c.d", "e.f"}, string(os.PathSeparator)) | ||
if matches[0] != expected { | ||
t.Fatalf("matched [%s], expected [%s]", matches[0], expected) | ||
} | ||
} | ||
|
||
func TestGlob_OneDoubleStar_twoMatches(t *testing.T) { | ||
// test a single double-star | ||
path := "./a/b/c.d/e.f" | ||
err := os.MkdirAll(path, 0755) | ||
if err != nil { | ||
t.Fatalf("os.MkdirAll: %s", err) | ||
} | ||
matches, err := Glob("./a/**/*.*") | ||
if err != nil { | ||
t.Fatalf("Glob: %s", err) | ||
} | ||
if len(matches) != 2 { | ||
t.Fatalf("got %d matches, expected 2", len(matches)) | ||
} | ||
expected := []string{ | ||
strings.Join([]string{"a", "b", "c.d"}, string(os.PathSeparator)), | ||
strings.Join([]string{"a", "b", "c.d", "e.f"}, string(os.PathSeparator)), | ||
} | ||
|
||
for i, match := range matches { | ||
if match != expected[i] { | ||
t.Fatalf("matched [%s], expected [%s]", match, expected[i]) | ||
} | ||
} | ||
} | ||
|
||
func TestGlob_TwoDoubleStars_oneMatch(t *testing.T) { | ||
// test two double-stars | ||
path := "./a/b/c.d/e.f" | ||
err := os.MkdirAll(path, 0755) | ||
if err != nil { | ||
t.Fatalf("os.MkdirAll: %s", err) | ||
} | ||
matches, err := Glob("./**/b/**/*.f") | ||
if err != nil { | ||
t.Fatalf("Glob: %s", err) | ||
} | ||
if len(matches) != 1 { | ||
t.Fatalf("got %d matches, expected 1", len(matches)) | ||
} | ||
expected := strings.Join([]string{"a", "b", "c.d", "e.f"}, string(os.PathSeparator)) | ||
|
||
if matches[0] != expected { | ||
t.Fatalf("matched [%s], expected [%s]", matches[0], expected) | ||
} | ||
} | ||
|
||
func TestExpand_DirectCall_emptySlice(t *testing.T) { | ||
var empty []string | ||
matches, err := Globs(empty).Expand() | ||
if err != nil { | ||
t.Fatalf("Glob: %s", err) | ||
} | ||
if len(matches) != 0 { | ||
t.Fatalf("got %d matches, expected 0", len(matches)) | ||
} | ||
} |
Oops, something went wrong.