matcher

package module
v0.0.0-...-81ff99e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2022 License: MIT Imports: 7 Imported by: 1

README

matcher

matcher is similar to path.Match, but:

  • Supports globstar/doublestar (**).
  • Provides a fast Glob function.
  • Supports combining matchers.

Examples

Match
package main

import "github.com/saracen/matcher"

func main() {
    matched, err := matcher.Match("hello/**/world", "hello/foo/bar/world")
    if err != nil {
        panic(err)
    }

    if matched {
        // do something
    }
}
Glob
package main

import "github.com/saracen/matcher"

func main() {
    matches, err := matcher.Glob(context.Background(), ".", matcher.New("**/*.go"))
    if err != nil {
        panic(err)
    }

    // do something with the matches
    _ = matches
}
Glob with multiple patterns
package main

import "github.com/saracen/matcher"

func main() {
    matcher := matcher.Multi(
        matcher.New("**/*.go"),
        matcher.New("**/*.txt"))

    matches, err := matcher.Glob(context.Background(), ".", matcher)
    if err != nil {
        panic(err)
    }

    // do something with the matches
    _ = matches
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Glob

func Glob(ctx context.Context, dir string, matcher Matcher, opts ...GlobOption) (map[string]os.FileInfo, error)

Glob returns the pathnames and their associated os.FileInfos of all files matching with the Matcher provided.

Patterns are matched against the path relative to the directory provided and path seperators are converted to '/'. Be aware that the matching performed by this library's Matchers are case sensitive (even on case-insensitive filesystems). Use WithPathTransformer(strings.ToLower) and NewMatcher(strings.ToLower(pattern)) to perform case-insensitive matching.

Glob ignores any permission and I/O errors.

func Match

func Match(pattern, pathname string, opts ...MatchOption) (bool, error)

Match has similar behaviour to path.Match, but supports globstar.

The pattern term '**' in a path portion matches zero or more subdirectories.

The only possible returned error is ErrBadPattern, when the pattern is malformed.

Types

type GlobOption

type GlobOption func(*globOptions) error

GlobOption is an option to configure Glob() behaviour.

func WithPathTransformer

func WithPathTransformer(transformer func(pathname string) string) GlobOption

WithPathTransforms allows a function to transform a path prior to it being matched. A common use-case is WithPathTransformer(strings.ToLower) to ensure paths have their case folded before matching.

The transformer function should be safe for concurrent use.

type MatchOption

type MatchOption func(*matchOptions)

MatchOption is an option to configure Match() behaviour.

func WithMatchFunc

func WithMatchFunc(matcher func(pattern, name string) (matched bool, err error)) MatchOption

WithMatchFunc allows a user provided matcher to be used in place of path.Match for matching path segments. The globstar pattern will always be supported, but paths between directory separators will be matched against the function provided.

type Matcher

type Matcher interface {
	Match(pathname string) (Result, error)
}

Matcher is an interface used for matching a path against a pattern.

func Multi

func Multi(matches ...Matcher) Matcher

Multi returns a new Matcher that matches against many matchers.

func New

func New(pattern string, opts ...MatchOption) Matcher

New returns a new Matcher.

The Matcher returned uses the same rules as Match, but returns a result of either NotMatched, Matched or Follow.

Follow hints to the caller that whilst the pattern wasn't matched, path traversal might yield matches. This allows for more efficient globbing, preventing path traversal where a match is impossible.

type Result

type Result int
const (
	NotMatched Result = iota
	Matched
	Follow
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL