Documentation
¶
Overview ¶
Package richglob brings Bash-style globbing to your filesystem matching like globstar, extglob, case-insensitive matching, ignore patterns, and .gitignore-aware traversal.
- Match reports whether a single name matches a pattern.
- Glob walks the filesystem and returns matching paths.
- GlobSeq2 walks the filesystem lazily and yields matching paths together with a terminal error when one occurs.
By default, the package follows its own matching rules rather than Bash's pathname expansion rules. Bash-specific behaviors are enabled explicitly with options such as WithGlobStar, WithExtGlob, WithNoCaseGlob, WithDotGlob, and WithGitIgnore.
Index ¶
- Constants
- Variables
- func Glob(pattern string, opts ...Option) ([]string, error)
- func GlobSeq2(pattern string, opts ...Option) iter.Seq2[string, error]
- func Match(pattern, name string, opts ...Option) (bool, error)
- type Option
- func WithDotGlob() Option
- func WithExtGlob() Option
- func WithFailGlob() Option
- func WithGitIgnore() Option
- func WithGlobASCIIRanges() Option
- func WithGlobIgnore(patterns ...string) Option
- func WithGlobSkipDots(enabled bool) Option
- func WithGlobStar() Option
- func WithNoCaseGlob() Option
- func WithNullGlob() Option
- func WithSort(mode SortMode) Option
- type SortMode
Examples ¶
Constants ¶
const Separator = os.PathSeparator
Separator is the operating system-specific path separator used by patterns.
Variables ¶
var ErrBadPattern = errors.New("syntax error in pattern")
ErrBadPattern indicates a pattern was malformed.
var ErrNoMatch = errors.New("no matches found")
ErrNoMatch reports that pathname expansion produced no matches.
Functions ¶
func Glob ¶
Glob returns the filesystem paths that match pattern.
When nothing matches, Glob returns nil, nil by default. Use WithNullGlob to return an empty slice instead, or WithFailGlob to return ErrNoMatch.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"go.dw1.io/richglob"
)
func main() {
tmpDir, err := os.MkdirTemp("", "richglob-example-")
if err != nil {
fmt.Println(err)
return
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
fmt.Println(err)
}
}()
oldWD, err := os.Getwd()
if err != nil {
fmt.Println(err)
return
}
defer func() {
if err := os.Chdir(oldWD); err != nil {
fmt.Println(err)
}
}()
for _, path := range []string{
filepath.Join(tmpDir, "src", "main.go"),
filepath.Join(tmpDir, "src", "pkg", "util.go"),
filepath.Join(tmpDir, "src", "pkg", "util.txt"),
} {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
fmt.Println(err)
return
}
if err := os.WriteFile(path, []byte("package main\n"), 0o644); err != nil {
fmt.Println(err)
return
}
}
if err := os.Chdir(tmpDir); err != nil {
fmt.Println(err)
return
}
matches, err := richglob.Glob(filepath.Join("src", "**", "*.go"), richglob.WithGlobStar())
if err != nil {
fmt.Println(err)
return
}
for _, match := range matches {
fmt.Println(filepath.ToSlash(match))
}
}
Output: src/main.go src/pkg/util.go
func GlobSeq2 ¶
GlobSeq2 walks the filesystem lazily and yields matching paths.
The iterator yields `(path, nil)` pairs for matches. If pattern compilation, ignore-pattern compilation, or failglob processing encounters an error, the iterator yields a final `("", err)` pair and then stops. Matches are yielded in traversal order; unlike Glob, GlobSeq2 does not collect and globally sort results before emission.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"go.dw1.io/richglob"
)
func main() {
tmpDir, err := os.MkdirTemp("", "richglob-example-")
if err != nil {
fmt.Println(err)
return
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
fmt.Println(err)
}
}()
oldWD, err := os.Getwd()
if err != nil {
fmt.Println(err)
return
}
defer func() {
if err := os.Chdir(oldWD); err != nil {
fmt.Println(err)
}
}()
for _, path := range []string{
filepath.Join(tmpDir, "src", "main.go"),
filepath.Join(tmpDir, "src", "pkg", "util.go"),
filepath.Join(tmpDir, "src", "pkg", "util.txt"),
} {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
fmt.Println(err)
return
}
if err := os.WriteFile(path, []byte("package main\n"), 0o644); err != nil {
fmt.Println(err)
return
}
}
if err := os.Chdir(tmpDir); err != nil {
fmt.Println(err)
return
}
for match, err := range richglob.GlobSeq2(filepath.Join("src", "**", "*.go"), richglob.WithGlobStar()) {
if err != nil {
fmt.Println(err)
return
}
fmt.Println(filepath.ToSlash(match))
}
}
Output: src/main.go src/pkg/util.go
func Match ¶
Match reports whether name matches pattern.
Pattern syntax is similar to filepath.Match, with optional extensions enabled through Option values.
Example ¶
package main
import (
"fmt"
"path/filepath"
"go.dw1.io/richglob"
)
func main() {
matched, err := richglob.Match(
filepath.FromSlash("src/**/main.go"),
filepath.FromSlash("src/cmd/tool/main.go"),
richglob.WithGlobStar(),
)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(matched)
}
Output: true
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures Match and Glob behavior.
func WithDotGlob ¶
func WithDotGlob() Option
WithDotGlob includes hidden path entries when Bash pathname rules are active.
func WithExtGlob ¶
func WithExtGlob() Option
WithExtGlob enables Bash extglob operators such as @(a|b) and !(tmp|cache).
func WithFailGlob ¶
func WithFailGlob() Option
WithFailGlob makes Glob return ErrNoMatch when nothing matches.
func WithGitIgnore ¶
func WithGitIgnore() Option
WithGitIgnore loads and applies .gitignore rules while walking directories.
func WithGlobASCIIRanges ¶
func WithGlobASCIIRanges() Option
WithGlobASCIIRanges makes character ranges such as [A-Z] use ASCII ordering.
func WithGlobIgnore ¶
WithGlobIgnore removes matches that also match any of the supplied patterns.
func WithGlobSkipDots ¶
WithGlobSkipDots controls whether "." and ".." are skipped during Bash-style pathname expansion.
func WithGlobStar ¶
func WithGlobStar() Option
WithGlobStar enables "**" to match zero or more directory segments.
func WithNoCaseGlob ¶
func WithNoCaseGlob() Option
WithNoCaseGlob enables case-insensitive matching.
func WithNullGlob ¶
func WithNullGlob() Option
WithNullGlob makes Glob return an empty, non-nil slice when nothing matches.