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

     1  package lint
     2  
     3  import (
     4  	"flag"
     5  
     6  	"github.com/yoheimuta/protolint/internal/cmd/subcmds"
     7  	"github.com/yoheimuta/protolint/linter/autodisable"
     8  
     9  	"github.com/yoheimuta/protolint/internal/addon/plugin/shared"
    10  
    11  	"github.com/yoheimuta/protolint/internal/linter/report/reporters"
    12  
    13  	"github.com/yoheimuta/protolint/internal/linter/report"
    14  )
    15  
    16  // Flags represents a set of lint flag parameters.
    17  type Flags struct {
    18  	*flag.FlagSet
    19  
    20  	FilePaths                 []string
    21  	ConfigPath                string
    22  	ConfigDirPath             string
    23  	FixMode                   bool
    24  	Reporter                  report.Reporter
    25  	AutoDisableType           autodisable.PlacementType
    26  	OutputFilePath            string
    27  	Verbose                   bool
    28  	NoErrorOnUnmatchedPattern bool
    29  	Plugins                   []shared.RuleSet
    30  	AdditionalReporters       reporterStreamFlags
    31  }
    32  
    33  // NewFlags creates a new Flags.
    34  func NewFlags(
    35  	args []string,
    36  ) (Flags, error) {
    37  	f := Flags{
    38  		FlagSet:         flag.NewFlagSet("lint", flag.ExitOnError),
    39  		Reporter:        reporters.PlainReporter{},
    40  		AutoDisableType: autodisable.Noop,
    41  	}
    42  	var rf reporterFlag
    43  	var af autoDisableFlag
    44  	var pf subcmds.PluginFlag
    45  	var rfs reporterStreamFlags
    46  
    47  	f.StringVar(
    48  		&f.ConfigPath,
    49  		"config_path",
    50  		"",
    51  		"path/to/protolint.yaml. Note that if both are set, config_dir_path is ignored.",
    52  	)
    53  	f.StringVar(
    54  		&f.ConfigDirPath,
    55  		"config_dir_path",
    56  		"",
    57  		"path/to/the_directory_including_protolint.yaml",
    58  	)
    59  	f.BoolVar(
    60  		&f.FixMode,
    61  		"fix",
    62  		false,
    63  		"mode that the command line automatically fix some of the problems",
    64  	)
    65  	f.Var(
    66  		&rf,
    67  		"reporter",
    68  		`formatter to output results in the specific format. Available reporters are "plain"(default), "junit", "json", "sarif", and "unix".`,
    69  	)
    70  	f.Var(
    71  		&af,
    72  		"auto_disable",
    73  		`mode that the command line automatically disable some of the problems. Available auto_disable are "next" and "this".`,
    74  	)
    75  	f.StringVar(
    76  		&f.OutputFilePath,
    77  		"output_file",
    78  		"",
    79  		"path/to/output.txt",
    80  	)
    81  	f.Var(
    82  		&pf,
    83  		"plugin",
    84  		`plugins to provide custom lint rule set. Note that it's necessary to specify it as path format'`,
    85  	)
    86  	f.BoolVar(
    87  		&f.Verbose,
    88  		"v",
    89  		false,
    90  		"verbose output that includes parsing process details",
    91  	)
    92  	f.BoolVar(
    93  		&f.NoErrorOnUnmatchedPattern,
    94  		"no-error-on-unmatched-pattern",
    95  		false,
    96  		"exits with 0 when no file is matched",
    97  	)
    98  	f.Var(
    99  		&rfs,
   100  		"add-reporter",
   101  		"Adds a reporter to the list of reporters to use. The format should be 'name of reporter':'Path-To_output_file'",
   102  	)
   103  
   104  	_ = f.Parse(args)
   105  	if rf.reporter != nil {
   106  		f.Reporter = rf.reporter
   107  	}
   108  	if len(rfs) > 0 {
   109  		f.AdditionalReporters = rfs
   110  	}
   111  	if af.autoDisableType != 0 {
   112  		f.AutoDisableType = af.autoDisableType
   113  	}
   114  
   115  	plugins, err := pf.BuildPlugins(f.Verbose)
   116  	if err != nil {
   117  		return Flags{}, err
   118  	}
   119  	f.Plugins = plugins
   120  
   121  	f.FilePaths = f.Args()
   122  	return f, nil
   123  }