forked from bazel-contrib/bazel-gazelle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
110 lines (90 loc) · 3.06 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Copyright 2018 The Bazel Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package walk
import (
"flag"
"log"
"path"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/bmatcuk/doublestar"
gzflag "github.com/bazelbuild/bazel-gazelle/flag"
)
// TODO(#472): store location information to validate each exclude. They
// may be set in one directory and used in another. Excludes work on
// declared generated files, so we can't just stat.
type walkConfig struct {
excludes []string
ignore bool
follow []string
}
const walkName = "_walk"
func getWalkConfig(c *config.Config) *walkConfig {
return c.Exts[walkName].(*walkConfig)
}
func (wc *walkConfig) isExcluded(rel, base string) bool {
if base == ".git" {
return true
}
f := path.Join(rel, base)
for _, x := range wc.excludes {
matched, err := doublestar.Match(x, f)
if err != nil {
// doublestar.Match returns only one possible error, and only if the
// pattern is not valid. During the configuration of the walker (see
// Configure below), we discard any invalid pattern and thus an error
// here should not be possible.
log.Panicf("error during doublestar.Match. This should not happen, please file an issue https://github.com/bazelbuild/bazel-gazelle/issues/new: %s", err)
}
if matched {
return true
}
}
return false
}
type Configurer struct{}
func (_ *Configurer) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {
wc := &walkConfig{}
c.Exts[walkName] = wc
fs.Var(&gzflag.MultiFlag{Values: &wc.excludes}, "exclude", "pattern that should be ignored (may be repeated)")
}
func (_ *Configurer) CheckFlags(fs *flag.FlagSet, c *config.Config) error { return nil }
func (_ *Configurer) KnownDirectives() []string {
return []string{"exclude", "follow", "ignore"}
}
func (cr *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
wc := getWalkConfig(c)
wcCopy := &walkConfig{}
*wcCopy = *wc
wcCopy.ignore = false
if f != nil {
for _, d := range f.Directives {
switch d.Key {
case "exclude":
if err := checkPathMatchPattern(path.Join(rel, d.Value)); err != nil {
log.Printf("the exclusion pattern is not valid %q: %s", path.Join(rel, d.Value), err)
continue
}
wcCopy.excludes = append(wcCopy.excludes, path.Join(rel, d.Value))
case "follow":
wcCopy.follow = append(wcCopy.follow, path.Join(rel, d.Value))
case "ignore":
wcCopy.ignore = true
}
}
}
c.Exts[walkName] = wcCopy
}
func checkPathMatchPattern(pattern string) error {
_, err := doublestar.Match(pattern, "x")
return err
}