-
Notifications
You must be signed in to change notification settings - Fork 367
/
config.go
64 lines (53 loc) · 1.68 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
package visibility
import (
"flag"
"strings"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
)
const (
_directiveName = "default_visibility"
)
type visConfig struct {
visibilityTargets []string
}
// getVisConfig directly returns the internal configuration struct rather
// than a pointer because we explicitly want pass-by-value symantics so
// configurations down a directory tree don't accidentially update upstream.
func getVisConfig(c *config.Config) visConfig {
cfg := c.Exts[_extName]
if cfg == nil {
return visConfig{}
}
return cfg.(visConfig)
}
// RegisterFlags noops because we only parameterize behavior with a directive.
func (*visibilityExtension) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {}
// CheckFlags noops because no flags are referenced.
func (*visibilityExtension) CheckFlags(fs *flag.FlagSet, c *config.Config) error {
return nil
}
// KnownDirectives returns the only directive this extension operates on.
func (*visibilityExtension) KnownDirectives() []string {
return []string{_directiveName}
}
// Configure identifies the visibility targets from the directive value, if it exists.
//
// To set multiple visibility targets, either multiple directives can be used, or a
// list can be provided with comma-separated values.
func (*visibilityExtension) Configure(c *config.Config, _ string, f *rule.File) {
cfg := getVisConfig(c)
if f == nil {
return
}
for _, d := range f.Directives {
switch d.Key {
case _directiveName:
for _, target := range strings.Split(d.Value, ",") {
cfg.visibilityTargets = append(cfg.visibilityTargets, target)
}
}
}
c.Exts[_extName] = cfg
}
// /Configurator embed