filter

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package filter provides file filtering for sync operations.

Filters are used to include or exclude files based on patterns, size, and age. They are inspired by rclone's filtering system.

Basic usage:

f := filter.New(
    filter.Include("*.json"),
    filter.Exclude("*.tmp"),
    filter.MaxSize(100 * 1024 * 1024), // 100 MB
)

if f.Match(fileInfo) {
    // File passes filter
}

Index

Constants

View Source
const (
	KB = 1024
	MB = 1024 * KB
	GB = 1024 * MB
	TB = 1024 * GB
)

Common file size constants for convenience.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileInfo

type FileInfo struct {
	Path    string
	Size    int64
	ModTime time.Time
	IsDir   bool
}

FileInfo contains the information needed for filtering.

type Filter

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

Filter determines whether files should be included in sync operations.

func New

func New(opts ...Option) *Filter

New creates a new Filter with the given options.

func (*Filter) IsEmpty

func (f *Filter) IsEmpty() bool

IsEmpty returns true if the filter has no rules.

func (*Filter) Match

func (f *Filter) Match(fi FileInfo) bool

Match returns true if the file passes the filter.

The filtering logic:

  1. If there are include patterns and the file doesn't match any, exclude it
  2. If the file matches any exclude pattern, exclude it
  3. If the file fails size or age constraints, exclude it
  4. Otherwise, include the file

func (*Filter) MatchPath

func (f *Filter) MatchPath(path string) bool

MatchPath is a convenience method that matches by path only.

type Option

type Option func(*Filter)

Option configures a Filter.

func Exclude

func Exclude(pattern string) Option

Exclude adds an exclude pattern. Files matching any exclude pattern are excluded. Exclude rules take precedence over include rules.

func FromFile

func FromFile(path string) (Option, error)

FromFile loads filter rules from a file. Each line is a pattern. Lines starting with + are includes, lines starting with - are excludes. Empty lines and lines starting with # are ignored.

Example file:

# Include JSON files
+ *.json
# Exclude temp files
- *.tmp
- *.bak

func Include

func Include(pattern string) Option

Include adds an include pattern. Files matching any include pattern are included (unless excluded). Patterns use filepath.Match syntax (*, ?, [...]).

func MaxAge

func MaxAge(d time.Duration) Option

MaxAge sets the maximum file age filter. Files older than this are excluded. Age is calculated as time since modification.

func MaxSize

func MaxSize(size int64) Option

MaxSize sets the maximum file size filter. Files larger than this are excluded.

func MinAge

func MinAge(d time.Duration) Option

MinAge sets the minimum file age filter. Files newer than this are excluded. Age is calculated as time since modification.

func MinSize

func MinSize(size int64) Option

MinSize sets the minimum file size filter. Files smaller than this are excluded.

Jump to

Keyboard shortcuts

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