github.com/aretext/aretext@v1.3.0/config/ruleset.go (about) 1 package config 2 3 import ( 4 "log" 5 6 "github.com/aretext/aretext/file" 7 ) 8 9 // Rule is a configuration rule. 10 // Each rule contains a glob pattern matching the path of the current file. 11 // If the rule matches the current file, its configuration will be applied. 12 type Rule struct { 13 Name string `json:"name"` 14 Pattern string `json:"pattern"` 15 Config map[string]any `json:"config"` 16 } 17 18 // RuleSet is a set of configuration rules. 19 // If multiple rules match a file path, they are applied in order. 20 type RuleSet []Rule 21 22 // Rules that match the file path are applied in order to produce the configuration. 23 func (rs RuleSet) ConfigForPath(path string) Config { 24 c := make(map[string]any, 0) 25 for _, rule := range rs { 26 if file.GlobMatch(rule.Pattern, path) { 27 log.Printf("Applying config rule %q with pattern %q for path %q\n", rule.Name, rule.Pattern, path) 28 c = MergeRecursive(c, rule.Config).(map[string]any) 29 } 30 } 31 log.Printf("Resolved config for path %q: %#v\n", path, c) 32 return ConfigFromUntypedMap(c) 33 } 34 35 // Validate checks whether every rule in the set has a valid configuration. 36 func (rs RuleSet) Validate() error { 37 for _, r := range rs { 38 c := ConfigFromUntypedMap(r.Config) 39 err := c.Validate() 40 if err != nil { 41 return err 42 } 43 } 44 return nil 45 }