github.com/terraform-linters/tflint-plugin-sdk@v0.22.0/tflint/ruleset.go (about)

     1  package tflint
     2  
     3  import (
     4  	"github.com/terraform-linters/tflint-plugin-sdk/hclext"
     5  	"github.com/terraform-linters/tflint-plugin-sdk/logger"
     6  )
     7  
     8  var _ RuleSet = &BuiltinRuleSet{}
     9  
    10  // BuiltinRuleSet is the basis of the ruleset. Plugins can serve this ruleset directly.
    11  // You can serve a custom ruleset by embedding this ruleset if you need special extensions.
    12  type BuiltinRuleSet struct {
    13  	Name       string
    14  	Version    string
    15  	Constraint string
    16  	Rules      []Rule
    17  
    18  	EnabledRules []Rule
    19  }
    20  
    21  // RuleSetName is the name of the ruleset.
    22  // Generally, this is synonymous with the name of the plugin.
    23  func (r *BuiltinRuleSet) RuleSetName() string {
    24  	return r.Name
    25  }
    26  
    27  // RuleSetVersion is the version of the plugin.
    28  func (r *BuiltinRuleSet) RuleSetVersion() string {
    29  	return r.Version
    30  }
    31  
    32  // RuleNames is a list of rule names provided by the plugin.
    33  func (r *BuiltinRuleSet) RuleNames() []string {
    34  	names := make([]string, len(r.Rules))
    35  	for idx, rule := range r.Rules {
    36  		names[idx] = rule.Name()
    37  	}
    38  	return names
    39  }
    40  
    41  // VersionConstraint declares the version of TFLint the plugin will work with.
    42  // Default is no constraint.
    43  func (r *BuiltinRuleSet) VersionConstraint() string {
    44  	return r.Constraint
    45  }
    46  
    47  // ApplyGlobalConfig applies the common config to the ruleset.
    48  // This is not supposed to be overridden from custom rulesets.
    49  // Override the ApplyConfig if you want to apply the plugin's own configuration.
    50  //
    51  // The priority of rule configs is as follows:
    52  //
    53  // 1. --only option
    54  // 2. Rule config declared in each "rule" block
    55  // 3. The `disabled_by_default` declared in global "config" block
    56  func (r *BuiltinRuleSet) ApplyGlobalConfig(config *Config) error {
    57  	r.EnabledRules = []Rule{}
    58  	only := map[string]bool{}
    59  
    60  	if len(config.Only) > 0 {
    61  		logger.Debug("Only mode is enabled. Ignoring default plugin rules")
    62  		for _, rule := range config.Only {
    63  			only[rule] = true
    64  		}
    65  	} else if config.DisabledByDefault {
    66  		logger.Debug("Default plugin rules are disabled by default")
    67  	}
    68  
    69  	for _, rule := range r.Rules {
    70  		enabled := rule.Enabled()
    71  		if len(only) > 0 {
    72  			enabled = only[rule.Name()]
    73  		} else if cfg := config.Rules[rule.Name()]; cfg != nil {
    74  			enabled = cfg.Enabled
    75  		} else if config.DisabledByDefault {
    76  			enabled = false
    77  		}
    78  
    79  		if enabled {
    80  			r.EnabledRules = append(r.EnabledRules, rule)
    81  		}
    82  	}
    83  	return nil
    84  }
    85  
    86  // ConfigSchema returns the ruleset plugin config schema.
    87  // This schema should be a schema inside of "plugin" block.
    88  // Custom rulesets can override this method to return the plugin's own config schema.
    89  func (r *BuiltinRuleSet) ConfigSchema() *hclext.BodySchema {
    90  	return nil
    91  }
    92  
    93  // ApplyConfig applies the configuration to the ruleset.
    94  // Custom rulesets can override this method to reflect the plugin's own configuration.
    95  func (r *BuiltinRuleSet) ApplyConfig(content *hclext.BodyContent) error {
    96  	return nil
    97  }
    98  
    99  // NewRunner returns a new runner based on the original runner.
   100  // Custom rulesets can override this method to inject a custom runner.
   101  func (r *BuiltinRuleSet) NewRunner(runner Runner) (Runner, error) {
   102  	return runner, nil
   103  }
   104  
   105  // BuiltinImpl returns the receiver itself as BuiltinRuleSet.
   106  // This is not supposed to be overridden from custom rulesets.
   107  func (r *BuiltinRuleSet) BuiltinImpl() *BuiltinRuleSet {
   108  	return r
   109  }
   110  
   111  func (r *BuiltinRuleSet) mustEmbedBuiltinRuleSet() {}