filter

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: BSD-2-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package filter implements filters for files similar to filepath.Glob, but in contrast to filepath.Glob a pattern may specify directories.

For a list of valid patterns please see the documentation on filepath.Glob.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrBadString = errors.New("filter.Match: string is empty")

ErrBadString is returned when Match is called with the empty string as the second argument.

Functions

func ChildMatch

func ChildMatch(patternStr, str string) (matched bool, err error)

ChildMatch returns true if children of str can match the pattern. When the pattern is malformed, filepath.ErrBadPattern is returned. The empty pattern matches everything, when str is the empty string ErrBadString is returned.

Pattern can be a combination of patterns suitable for filepath.Match, joined by filepath.Separator.

In addition patterns suitable for filepath.Match, pattern accepts a recursive wildcard '**', which greedily matches an arbitrary number of intermediate directories.

func List

func List(patterns []Pattern, str string) (matched bool, err error)

List returns true if str matches one of the patterns. Empty patterns are ignored.

Example
package main

import (
	"fmt"

	"github.com/josh/restic-api/api/filter"
)

func main() {
	patterns := filter.ParsePatterns([]string{"*.c", "*.go"})
	match, _ := filter.List(patterns, "/home/user/file.go")
	fmt.Printf("match: %v\n", match)
}
Output:

match: true

func ListWithChild

func ListWithChild(patterns []Pattern, str string) (matched bool, childMayMatch bool, err error)

ListWithChild returns true if str matches one of the patterns. Empty patterns are ignored.

func Match

func Match(patternStr, str string) (matched bool, err error)

Match returns true if str matches the pattern. When the pattern is malformed, filepath.ErrBadPattern is returned. The empty pattern matches everything, when str is the empty string ErrBadString is returned.

Pattern can be a combination of patterns suitable for filepath.Match, joined by filepath.Separator.

In addition patterns suitable for filepath.Match, pattern accepts a recursive wildcard '**', which greedily matches an arbitrary number of intermediate directories.

Example
package main

import (
	"fmt"

	"github.com/josh/restic-api/api/filter"
)

func main() {
	match, _ := filter.Match("*.go", "/home/user/file.go")
	fmt.Printf("match: %v\n", match)
}
Output:

match: true
Example (Wildcards)
package main

import (
	"fmt"

	"github.com/josh/restic-api/api/filter"
)

func main() {
	match, _ := filter.Match("/home/[uU]ser/?.go", "/home/user/F.go")
	fmt.Printf("match: %v\n", match)
}
Output:

match: true

func ValidatePatterns

func ValidatePatterns(patterns []string) error

ValidatePatterns validates a slice of patterns. Returns true if all patterns are valid - false otherwise, along with the invalid patterns.

Types

type ExcludePatternOptions added in v0.18.0

type ExcludePatternOptions struct {
	Excludes                []string
	InsensitiveExcludes     []string
	ExcludeFiles            []string
	InsensitiveExcludeFiles []string
}

func (*ExcludePatternOptions) Add added in v0.18.0

func (opts *ExcludePatternOptions) Add(f *pflag.FlagSet)

func (ExcludePatternOptions) CollectPatterns added in v0.18.0

func (opts ExcludePatternOptions) CollectPatterns(warnf func(msg string, args ...interface{})) ([]RejectByNameFunc, error)

func (*ExcludePatternOptions) Empty added in v0.18.0

func (opts *ExcludePatternOptions) Empty() bool

type IncludeByNameFunc added in v0.18.0

type IncludeByNameFunc func(item string) (matched bool, childMayMatch bool)

IncludeByNameFunc is a function that takes a filename that should be included in the restore process and returns whether it should be included.

func IncludeByInsensitivePattern added in v0.18.0

func IncludeByInsensitivePattern(patterns []string, warnf func(msg string, args ...interface{})) IncludeByNameFunc

IncludeByInsensitivePattern returns a IncludeByNameFunc which includes files that match one of the patterns, ignoring the casing of the filenames.

func IncludeByPattern added in v0.18.0

func IncludeByPattern(patterns []string, warnf func(msg string, args ...interface{})) IncludeByNameFunc

IncludeByPattern returns a IncludeByNameFunc which includes files that match one of the patterns.

type IncludePatternOptions added in v0.18.0

type IncludePatternOptions struct {
	Includes                []string
	InsensitiveIncludes     []string
	IncludeFiles            []string
	InsensitiveIncludeFiles []string
}

func (*IncludePatternOptions) Add added in v0.18.0

func (opts *IncludePatternOptions) Add(f *pflag.FlagSet)

func (IncludePatternOptions) CollectPatterns added in v0.18.0

func (opts IncludePatternOptions) CollectPatterns(warnf func(msg string, args ...interface{})) ([]IncludeByNameFunc, error)

type InvalidPatternError

type InvalidPatternError struct {
	InvalidPatterns []string
}

func (*InvalidPatternError) Error

func (e *InvalidPatternError) Error() string

type Pattern

type Pattern struct {
	// contains filtered or unexported fields
}

Pattern represents a preparsed filter pattern

func ParsePatterns

func ParsePatterns(pattern []string) []Pattern

ParsePatterns prepares a list of patterns for use with List.

type RejectByNameFunc added in v0.18.0

type RejectByNameFunc func(path string) bool

RejectByNameFunc is a function that takes a filename of a file that would be included in the backup. The function returns true if it should be excluded (rejected) from the backup.

func RejectByInsensitivePattern added in v0.18.0

func RejectByInsensitivePattern(patterns []string, warnf func(msg string, args ...interface{})) RejectByNameFunc

RejectByInsensitivePattern is like RejectByPattern but case insensitive.

func RejectByPattern added in v0.18.0

func RejectByPattern(patterns []string, warnf func(msg string, args ...interface{})) RejectByNameFunc

RejectByPattern returns a RejectByNameFunc which rejects files that match one of the patterns.

Jump to

Keyboard shortcuts

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