github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/cmd/subcmds/lint/cmdLintConfig.go (about)

     1  package lint
     2  
     3  import (
     4  	"github.com/yoheimuta/protolint/internal/addon/plugin/shared"
     5  	"github.com/yoheimuta/protolint/internal/cmd/subcmds"
     6  	"github.com/yoheimuta/protolint/internal/linter/config"
     7  	"github.com/yoheimuta/protolint/internal/linter/file"
     8  	"github.com/yoheimuta/protolint/internal/linter/report"
     9  	"github.com/yoheimuta/protolint/linter/autodisable"
    10  	"github.com/yoheimuta/protolint/linter/rule"
    11  )
    12  
    13  // CmdLintConfig is a config for lint command.
    14  type CmdLintConfig struct {
    15  	external        config.ExternalConfig
    16  	fixMode         bool
    17  	autoDisableType autodisable.PlacementType
    18  	verbose         bool
    19  	reporters       report.ReportersWithOutput
    20  	plugins         []shared.RuleSet
    21  }
    22  
    23  // NewCmdLintConfig creates a new CmdLintConfig.
    24  func NewCmdLintConfig(
    25  	externalConfig config.ExternalConfig,
    26  	flags Flags,
    27  ) CmdLintConfig {
    28  	output := report.WriteToConsole
    29  	if 0 < len(flags.OutputFilePath) {
    30  		output = flags.OutputFilePath
    31  	}
    32  
    33  	var reporters report.ReportersWithOutput
    34  	reporters = append(reporters, *report.NewReporterWithOutput(flags.Reporter, output))
    35  
    36  	for _, additionalReporter := range flags.AdditionalReporters {
    37  		r := *report.NewReporterWithOutput(additionalReporter.reporter, additionalReporter.targetFile)
    38  		reporters = append(reporters, r)
    39  	}
    40  
    41  	return CmdLintConfig{
    42  		external:        externalConfig,
    43  		fixMode:         flags.FixMode,
    44  		autoDisableType: flags.AutoDisableType,
    45  		verbose:         flags.Verbose,
    46  		reporters:       reporters,
    47  		plugins:         flags.Plugins,
    48  	}
    49  }
    50  
    51  // GenRules generates rules which are applied to the filename path.
    52  func (c CmdLintConfig) GenRules(
    53  	f file.ProtoFile,
    54  ) ([]rule.HasApply, error) {
    55  	allRules, err := subcmds.NewAllRules(c.external.Lint.RulesOption, c.fixMode, c.autoDisableType, c.verbose, c.plugins)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	var defaultRuleIDs []string
    61  	if c.external.Lint.Rules.AllDefault {
    62  		defaultRuleIDs = allRules.IDs()
    63  	} else {
    64  		defaultRuleIDs = allRules.Default().IDs()
    65  	}
    66  
    67  	var hasApplies []rule.HasApply
    68  	for _, r := range allRules {
    69  		if c.external.ShouldSkipRule(r.ID(), f.DisplayPath(), defaultRuleIDs) {
    70  			continue
    71  		}
    72  		hasApplies = append(hasApplies, r)
    73  	}
    74  
    75  	return hasApplies, nil
    76  }