package doublestar
import "github.com/bmatcuk/doublestar/v4"
Index ¶
- Variables
- func FilepathGlob(pattern string, opts ...GlobOption) (matches []string, err error)
- func Glob(fsys fs.FS, pattern string, opts ...GlobOption) ([]string, error)
- func GlobWalk(fsys fs.FS, pattern string, fn GlobWalkFunc, opts ...GlobOption) error
- func Match(pattern, name string) (bool, error)
- func PathMatch(pattern, name string) (bool, error)
- func SplitPattern(p string) (base, pattern string)
- func ValidatePathPattern(s string) bool
- func ValidatePattern(s string) bool
- type DirEntryFromFileInfo
- func (d *DirEntryFromFileInfo) Info() (fs.FileInfo, error)
- func (d *DirEntryFromFileInfo) IsDir() bool
- func (d *DirEntryFromFileInfo) Name() string
- func (d *DirEntryFromFileInfo) Type() fs.FileMode
- type DirEntryWithFullPath
- type GlobOption
- type GlobWalkFunc
Variables ¶
var ErrBadPattern = path.ErrBadPattern
ErrBadPattern indicates a pattern was malformed.
ErrPatternNotExist indicates that the pattern passed to Glob, GlobWalk, or FilepathGlob references a path that does not exist.
If returned from GlobWalkFunc, will cause GlobWalk to skip the current directory. In other words, if the current path is a directory, GlobWalk will not recurse into it. Otherwise, GlobWalk will skip the rest of the current directory.
Functions ¶
func FilepathGlob ¶
func FilepathGlob(pattern string, opts ...GlobOption) (matches []string, err error)
FilepathGlob returns the names of all files matching pattern or nil if there is no matching file. The syntax of pattern is the same as in Match(). The pattern may describe hierarchical names such as usr/*/bin/ed.
FilepathGlob ignores file system errors such as I/O errors reading directories by default. The only possible returned error is ErrBadPattern, reporting that the pattern is malformed.
To enable aborting on I/O errors, the WithFailOnIOErrors option can be passed.
Note: FilepathGlob is a convenience function that is meant as a drop-in replacement for `path/filepath.Glob()` for users who don't need the complication of io/fs. Basically, it:
- Runs `filepath.Clean()` and `ToSlash()` on the pattern
- Runs `SplitPattern()` to get a base path and a pattern to Glob
- Creates an FS object from the base path and `Glob()s` on the pattern
- Joins the base path with all of the matches from `Glob()`
Returned paths will use the system's path separator, just like `filepath.Glob()`.
Note: the returned error doublestar.ErrBadPattern is not equal to filepath.ErrBadPattern.
func Glob ¶
Glob returns the names of all files matching pattern or nil if there is no matching file. The syntax of pattern is the same as in Match(). The pattern may describe hierarchical names such as usr/*/bin/ed.
Glob ignores file system errors such as I/O errors reading directories by default. The only possible returned error is ErrBadPattern, reporting that the pattern is malformed.
To enable aborting on I/O errors, the WithFailOnIOErrors option can be passed.
Note: this is meant as a drop-in replacement for io/fs.Glob(). Like io/fs.Glob(), this function assumes that your pattern uses `/` as the path separator even if that's not correct for your OS (like Windows). If you aren't sure if that's the case, you can use filepath.ToSlash() on your pattern before calling Glob().
Like `io/fs.Glob()`, patterns containing `/./`, `/../`, or starting with `/` will return no results and no errors. You can use SplitPattern to divide a pattern into a base path (to initialize an `FS` object) and pattern.
Note: users should _not_ count on the returned error, doublestar.ErrBadPattern, being equal to path.ErrBadPattern.
func GlobWalk ¶
func GlobWalk(fsys fs.FS, pattern string, fn GlobWalkFunc, opts ...GlobOption) error
GlobWalk calls the callback function `fn` for every file matching pattern. The syntax of pattern is the same as in Match() and the behavior is the same as Glob(), with regard to limitations (such as patterns containing `/./`, `/../`, or starting with `/`). The pattern may describe hierarchical names such as usr/*/bin/ed.
GlobWalk may have a small performance benefit over Glob if you do not need a slice of matches because it can avoid allocating memory for the matches. Additionally, GlobWalk gives you access to the `fs.DirEntry` objects for each match, and lets you quit early by returning a non-nil error from your callback function. Like `io/fs.WalkDir`, if your callback returns `SkipDir`, GlobWalk will skip the current directory. This means that if the current path _is_ a directory, GlobWalk will not recurse into it. If the current path is not a directory, the rest of the parent directory will be skipped.
GlobWalk ignores file system errors such as I/O errors reading directories by default. GlobWalk may return ErrBadPattern, reporting that the pattern is malformed.
To enable aborting on I/O errors, the WithFailOnIOErrors option can be passed.
Additionally, if the callback function `fn` returns an error, GlobWalk will exit immediately and return that error.
Like Glob(), this function assumes that your pattern uses `/` as the path separator even if that's not correct for your OS (like Windows). If you aren't sure if that's the case, you can use filepath.ToSlash() on your pattern before calling GlobWalk().
Note: users should _not_ count on the returned error, doublestar.ErrBadPattern, being equal to path.ErrBadPattern.
func Match ¶
Match reports whether name matches the shell pattern. The pattern syntax is:
pattern: { term } term: '*' matches any sequence of non-path-separators '/**/' matches zero or more directories '?' matches any single non-path-separator character '[' [ '^' '!' ] { character-range } ']' character class (must be non-empty) starting with `^` or `!` negates the class '{' { term } [ ',' { term } ... ] '}' alternatives c matches character c (c != '*', '?', '\\', '[') '\\' c matches character c character-range: c matches character c (c != '\\', '-', ']') '\\' c matches character c lo '-' hi matches character c for lo <= c <= hi
Match returns true if `name` matches the file name `pattern`. `name` and `pattern` are split on forward slash (`/`) characters and may be relative or absolute.
Match requires pattern to match all of name, not just a substring. The only possible returned error is ErrBadPattern, when pattern is malformed.
A doublestar (`**`) should appear surrounded by path separators such as `/**/`. A mid-pattern doublestar (`**`) behaves like bash's globstar option: a pattern such as `path/to/**.txt` would return the same results as `path/to/*.txt`. The pattern you're looking for is `path/to/**/*.txt`.
Note: this is meant as a drop-in replacement for path.Match() which always uses '/' as the path separator. If you want to support systems which use a different path separator (such as Windows), what you want is PathMatch(). Alternatively, you can run filepath.ToSlash() on both pattern and name and then use this function.
Note: users should _not_ count on the returned error, doublestar.ErrBadPattern, being equal to path.ErrBadPattern.
func PathMatch ¶
PathMatch returns true if `name` matches the file name `pattern`. The difference between Match and PathMatch is that PathMatch will automatically use your system's path separator to split `name` and `pattern`. On systems where the path separator is `'\'`, escaping will be disabled.
Note: this is meant as a drop-in replacement for filepath.Match(). It assumes that both `pattern` and `name` are using the system's path separator. If you can't be sure of that, use filepath.ToSlash() on both `pattern` and `name`, and then use the Match() function instead.
func SplitPattern ¶
SplitPattern is a utility function. Given a pattern, SplitPattern will return two strings: the first string is everything up to the last slash (`/`) that appears _before_ any unescaped "meta" characters (ie, `*?[{`). The second string is everything after that slash. For example, given the pattern:
../../path/to/meta*/** ^----------- split here
SplitPattern returns "../../path/to" and "meta*/**". This is useful for initializing os.DirFS() to call Glob() because Glob() will silently fail if your pattern includes `/./` or `/../`. For example:
base, pattern := SplitPattern("../../path/to/meta*/**") fsys := os.DirFS(base) matches, err := Glob(fsys, pattern)
If SplitPattern cannot find somewhere to split the pattern (for example, `meta*/**`), it will return "." and the unaltered pattern (`meta*/**` in this example).
Of course, it is your responsibility to decide if the returned base path is "safe" in the context of your application. Perhaps you could use Match() to validate against a list of approved base directories?
func ValidatePathPattern ¶
Like ValidatePattern, only uses your OS path separator. In other words, use ValidatePattern if you would normally use Match() or Glob(). Use ValidatePathPattern if you would normally use PathMatch(). Keep in mind, Glob() requires '/' separators, even if your OS uses something else.
func ValidatePattern ¶
Validate a pattern. Patterns are validated while they run in Match(), PathMatch(), and Glob(), so, you normally wouldn't need to call this. However, there are cases where this might be useful: for example, if your program allows a user to enter a pattern that you'll run at a later time, you might want to validate it.
ValidatePattern assumes your pattern uses '/' as the path separator.
Types ¶
type DirEntryFromFileInfo ¶
type DirEntryFromFileInfo struct {
// contains filtered or unexported fields
}
func (*DirEntryFromFileInfo) Info ¶
func (d *DirEntryFromFileInfo) Info() (fs.FileInfo, error)
func (*DirEntryFromFileInfo) IsDir ¶
func (d *DirEntryFromFileInfo) IsDir() bool
func (*DirEntryFromFileInfo) Name ¶
func (d *DirEntryFromFileInfo) Name() string
func (*DirEntryFromFileInfo) Type ¶
func (d *DirEntryFromFileInfo) Type() fs.FileMode
type DirEntryWithFullPath ¶
type GlobOption ¶
type GlobOption func(*glob)
GlobOption represents a setting that can be passed to Glob, GlobWalk, and FilepathGlob.
func WithFailOnIOErrors ¶
func WithFailOnIOErrors() GlobOption
WithFailOnIOErrors is an option that can be passed to Glob, GlobWalk, or FilepathGlob. If passed, doublestar will abort and return IO errors when encountered. Note that if the glob pattern references a path that does not exist (such as `nonexistent/path/*`), this is _not_ considered an IO error: it is considered a pattern with no matches.
func WithFailOnPatternNotExist ¶
func WithFailOnPatternNotExist() GlobOption
WithFailOnPatternNotExist is an option that can be passed to Glob, GlobWalk, or FilepathGlob. If passed, doublestar will abort and return ErrPatternNotExist if the pattern references a path that does not exist before any meta charcters such as `nonexistent/path/*`. Note that alts (ie, `{...}`) are expanded before this check. In other words, a pattern such as `{a,b}/*` may fail if either `a` or `b` do not exist but `*/{a,b}` will never fail because the star may match nothing.
type GlobWalkFunc ¶
Callback function for GlobWalk(). If the function returns an error, GlobWalk will end immediately and return the same error.
Source Files ¶
doublestar.go glob.go globoptions.go globwalk.go match.go utils.go validate.go
- Version
- v4.4.0
- Published
- Nov 6, 2022
- Platform
- darwin/amd64
- Imports
- 7 packages
- Last checked
- now –
Tools for package owners.