fork of https://github.com/sourcegraph/zoekt
1// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package gitindex
16
17import "github.com/grafana/regexp"
18
19// Filter is a include/exclude filter to be used for repo names.
20type Filter struct {
21 inc, exc *regexp.Regexp
22}
23
24// Include returns true if the name passes the filter.
25func (f *Filter) Include(name string) bool {
26 if f.inc != nil {
27 if !f.inc.MatchString(name) {
28 return false
29 }
30 }
31 if f.exc != nil {
32 if f.exc.MatchString(name) {
33 return false
34 }
35 }
36 return true
37}
38
39// NewFilter creates a new filter.
40func NewFilter(includeRegex, excludeRegex string) (*Filter, error) {
41 f := &Filter{}
42 var err error
43 if includeRegex != "" {
44 f.inc, err = regexp.Compile(includeRegex)
45 if err != nil {
46 return nil, err
47 }
48 }
49 if excludeRegex != "" {
50 f.exc, err = regexp.Compile(excludeRegex)
51 if err != nil {
52 return nil, err
53 }
54 }
55
56 return f, nil
57}