matchers

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2021 License: Apache-2.0 Imports: 3 Imported by: 1

README

go-matchers

build status

Ruleset helper for ordered include/exclude logic, with builtin support for the stdlib flag package. Used by fdf.

Usage

Parsing with flag package

$ go get github.com/josephvusich/go-matchers
package example

import (
	"flag"
	
	"github.com/josephvusich/go-matchers"
	"github.com/josephvusich/go-matchers/glob"
)

func FlagExample() {
	ruleset := matchers.RuleSet{DefaultInclude: true}

	include, exclude := ruleset.FlagValues(glob.NewMatcher)

	fs := flag.NewFlagSet("test", flag.ExitOnError)
	fs.Var(include, "include", "include matching files")
	fs.Var(exclude, "exclude", "exclude matching files")

	fs.Parse([]string{`--exclude`, `*.bar`, `--include`, `foo.bar`})

	ruleset.Includes("foo.bar")  // true
	ruleset.Includes("fizz.bar") // false
}

Custom Matchers

You can extend to patterns beyond simple globs using the provided matchers.Matcher interface:

type Matcher interface {
	Match(string) bool
	
	// A string representation of this Matcher.
	// glob.Matcher returns the glob pattern.
	String() string
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher interface {
	Match(string) bool
	String() string
}

type MatcherFunc

type MatcherFunc func(input string) (Matcher, error)

type RuleSet

type RuleSet struct {
	DefaultInclude bool
	// contains filtered or unexported fields
}

func (*RuleSet) Add

func (s *RuleSet) Add(r Matcher, include bool)

func (*RuleSet) FlagValues

func (s *RuleSet) FlagValues(f MatcherFunc) (include, exclude flag.Value)

For integration with flag.Var

func (*RuleSet) Includes

func (s *RuleSet) Includes(check string) bool

A later Matcher takes precedence over an earlier one relative to the order added. Default (before matching any rules) is the opposite of the first Include/Exclude type. E.g., a list beginning with Include has an implicit "Exclude all" base rule, and vice versa. Empty RuleSet returns DefaultInclude.

func (*RuleSet) String

func (s *RuleSet) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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