|
| 1 | +// Copyright 2019 Google LLC |
| 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 | +// https://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 | + |
| 15 | +package lint |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/bmatcuk/doublestar/v4" |
| 26 | + "gopkg.in/yaml.v3" |
| 27 | +) |
| 28 | + |
| 29 | +// Configs determine if a rule is enabled or not on a file path. |
| 30 | +type Configs []Config |
| 31 | + |
| 32 | +// Config stores rule configurations for certain files |
| 33 | +// that the file path must match any of the included paths |
| 34 | +// but none of the excluded ones. |
| 35 | +type Config struct { |
| 36 | + // Explicitly specify the input file paths in scope of this config. |
| 37 | + // If omitted, it applies to all input file paths. |
| 38 | + IncludedPaths []string `json:"included_paths" yaml:"included_paths"` |
| 39 | + |
| 40 | + // Explicitly specify the input files paths to exclude from using this |
| 41 | + // config. If omitted, none of the input file paths are excluded. |
| 42 | + ExcludedPaths []string `json:"excluded_paths" yaml:"excluded_paths"` |
| 43 | + |
| 44 | + // The fully-qualifed rule name of a rule to enable as part of this config. |
| 45 | + // Can be one of the following formats: |
| 46 | + // |
| 47 | + // - an individual rule: `core::0203::field-behavior-required` |
| 48 | + // - an entire AIP rule group: `core::0203` |
| 49 | + // - an entire AIP category: `core` |
| 50 | + // - all rules: `all` |
| 51 | + EnabledRules []string `json:"enabled_rules" yaml:"enabled_rules"` |
| 52 | + |
| 53 | + // The fully-qualifed rule name of a rule to disable as part of this config. |
| 54 | + // Can be one of the following formats: |
| 55 | + // |
| 56 | + // - an individual rule: `core::0203::field-behavior-required` |
| 57 | + // - an entire AIP rule group: `core::0203` |
| 58 | + // - an entire AIP category: `core` |
| 59 | + // - all rules: `all` |
| 60 | + DisabledRules []string `json:"disabled_rules" yaml:"disabled_rules"` |
| 61 | +} |
| 62 | + |
| 63 | +// ReadConfigsFromFile reads Configs from a file. |
| 64 | +// It supports JSON(.json) and YAML(.yaml or .yml) files. |
| 65 | +func ReadConfigsFromFile(path string) (Configs, error) { |
| 66 | + var parse func(io.Reader) (Configs, error) |
| 67 | + switch filepath.Ext(path) { |
| 68 | + case ".json": |
| 69 | + parse = ReadConfigsJSON |
| 70 | + case ".yaml", ".yml": |
| 71 | + parse = ReadConfigsYAML |
| 72 | + } |
| 73 | + if parse == nil { |
| 74 | + return nil, fmt.Errorf("reading Configs: unsupported format `%q` with file path `%q`", filepath.Ext(path), path) |
| 75 | + } |
| 76 | + |
| 77 | + f, err := os.Open(path) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("readConfig: %s", err.Error()) |
| 80 | + } |
| 81 | + defer f.Close() |
| 82 | + |
| 83 | + return parse(f) |
| 84 | +} |
| 85 | + |
| 86 | +// ReadConfigsJSON reads Configs from a JSON file. |
| 87 | +func ReadConfigsJSON(f io.Reader) (Configs, error) { |
| 88 | + b, err := io.ReadAll(f) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + var c Configs |
| 93 | + if err := json.Unmarshal(b, &c); err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + return c, nil |
| 97 | +} |
| 98 | + |
| 99 | +// ReadConfigsYAML reads Configs from a YAML(.yml or .yaml) file. |
| 100 | +func ReadConfigsYAML(f io.Reader) (Configs, error) { |
| 101 | + b, err := io.ReadAll(f) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + var c Configs |
| 106 | + if err := yaml.Unmarshal(b, &c); err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + return c, nil |
| 110 | +} |
| 111 | + |
| 112 | +// IsRuleEnabled returns true if a rule is enabled by the configs. |
| 113 | +func (configs Configs) IsRuleEnabled(rule string, path string) bool { |
| 114 | + // Enabled by default if the rule does not belong to one of the default |
| 115 | + // disabled groups. Otherwise, needs to be explicitly enabled. |
| 116 | + enabled := !matchRule(rule, defaultDisabledRules...) |
| 117 | + for _, c := range configs { |
| 118 | + if c.matchPath(path) { |
| 119 | + if matchRule(rule, c.DisabledRules...) { |
| 120 | + enabled = false |
| 121 | + } |
| 122 | + if matchRule(rule, c.EnabledRules...) { |
| 123 | + enabled = true |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return enabled |
| 129 | +} |
| 130 | + |
| 131 | +func (c Config) matchPath(path string) bool { |
| 132 | + if matchPath(path, c.ExcludedPaths...) { |
| 133 | + return false |
| 134 | + } |
| 135 | + return len(c.IncludedPaths) == 0 || matchPath(path, c.IncludedPaths...) |
| 136 | +} |
| 137 | + |
| 138 | +func matchPath(path string, pathPatterns ...string) bool { |
| 139 | + path = filepath.ToSlash(path) |
| 140 | + for _, pattern := range pathPatterns { |
| 141 | + pattern = filepath.ToSlash(pattern) |
| 142 | + if matched, _ := doublestar.Match(pattern, path); matched { |
| 143 | + return true |
| 144 | + } |
| 145 | + } |
| 146 | + return false |
| 147 | +} |
| 148 | + |
| 149 | +func matchRule(rule string, rulePrefixes ...string) bool { |
| 150 | + rule = strings.ToLower(rule) |
| 151 | + for _, prefix := range rulePrefixes { |
| 152 | + prefix = strings.ToLower(prefix) |
| 153 | + prefix = strings.TrimSuffix(prefix, nameSeparator) // "core::" -> "core" |
| 154 | + prefix = strings.TrimPrefix(prefix, nameSeparator) // "::http-body" -> "http-body" |
| 155 | + if prefix == "all" || |
| 156 | + prefix == rule || |
| 157 | + strings.HasPrefix(rule, prefix+nameSeparator) || // e.g., "core" matches "core::http-body", but not "core-rules::http-body" |
| 158 | + strings.HasSuffix(rule, nameSeparator+prefix) || // e.g., "http-body" matches "core::http-body", but not "core::google-http-body" |
| 159 | + strings.Contains(rule, nameSeparator+prefix+nameSeparator) { // e.g., "http-body" matches "core::http-body::post", but not "core::google-http-body::post" |
| 160 | + return true |
| 161 | + } |
| 162 | + } |
| 163 | + return false |
| 164 | +} |
0 commit comments