github.com/Cloud-Foundations/Dominator@v0.3.4/lib/filter/api.go (about)

     1  package filter
     2  
     3  import (
     4  	"io"
     5  	"regexp"
     6  )
     7  
     8  // A Filter contains a list of regular expressions matching pathnames which
     9  // should be filtered out: excluded when building or not changed when pushing
    10  // images to a sub.
    11  // A Filter with no lines is an empty filter (nothing is excluded, everything is
    12  // changed when pushing).
    13  // A nil *Filter is a sparse filter: when building nothing is excluded. When
    14  // pushing to a sub, all files are pushed but files on the sub which are not in
    15  // the image are not removed from the sub.
    16  type Filter struct {
    17  	FilterLines       []string
    18  	filterExpressions []*regexp.Regexp
    19  	invertMatches     bool
    20  }
    21  
    22  // A MergeableFilter may be used to combine multiple Filters, eliminating
    23  // duplicate match expressions.
    24  type MergeableFilter struct {
    25  	filterLines map[string]struct{}
    26  }
    27  
    28  // Load will load a Filter from a file containing newline separated regular
    29  // expressions.
    30  func Load(filename string) (*Filter, error) {
    31  	return load(filename)
    32  }
    33  
    34  // New will create a Filter from a list of regular expressions, which are
    35  // automatically anchored to the beginning of the string to be matched against.
    36  // If filterLines is of length zero the Filter is an empty Filter.
    37  func New(filterLines []string) (*Filter, error) {
    38  	return newFilter(filterLines)
    39  }
    40  
    41  // Read will read a Filter from a reader containing newline separated regular
    42  // expressions.
    43  func Read(reader io.Reader) (*Filter, error) {
    44  	return read(reader)
    45  }
    46  
    47  // Compile will compile the regular expression strings for later use.
    48  func (filter *Filter) Compile() error {
    49  	return filter.compile()
    50  }
    51  
    52  // Equal will return true if two filters contain the same filter lines.
    53  func (left *Filter) Equal(right *Filter) bool {
    54  	return left.equal(right)
    55  }
    56  
    57  // Match will return true if pathname matches one of the regular expressions.
    58  // The Compile method will be automatically called if it has not been called
    59  // yet.
    60  func (filter *Filter) Match(pathname string) bool {
    61  	return filter.match(pathname)
    62  }
    63  
    64  // RegisterStrings may be used to register the regular expression strings with
    65  // a string de-duper. This can be used for garbage collection.
    66  func (filter *Filter) RegisterStrings(registerFunc func(string)) {
    67  	filter.registerStrings(registerFunc)
    68  }
    69  
    70  // ReplaceStrings may be used to replace the regular expression strings with
    71  // de-duplicated copies.
    72  func (filter *Filter) ReplaceStrings(replaceFunc func(string) string) {
    73  	filter.replaceStrings(replaceFunc)
    74  }
    75  
    76  // ExportFilter will return a Filter from previously merged Filters.
    77  func (mf *MergeableFilter) ExportFilter() *Filter {
    78  	return mf.exportFilter()
    79  }
    80  
    81  // Merge will merge a Filter.
    82  func (mf *MergeableFilter) Merge(filter *Filter) {
    83  	mf.merge(filter)
    84  }
    85  
    86  // Write will write the filter as newline separated regular expressions.
    87  func (filter *Filter) Write(writer io.Writer) error {
    88  	return filter.write(writer)
    89  }