Skip to content

Commit

Permalink
Merge pull request #105 from yoheimuta/config_path
Browse files Browse the repository at this point in the history
Add a config_path flag
  • Loading branch information
yoheimuta authored Nov 21, 2019
2 parents 2501123 + 17404c7 commit c99b4b9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ go get -u -v github.com/yoheimuta/protolint/cmd/protolint
protolint lint example.proto example2.proto # file mode, specify multiple specific files
protolint lint . # directory mode, search for all .proto files recursively
protolint . # same as "protolint lint ."
protolint lint -config_path=path/to/your_protolint.yaml . # use path/to/your_protolint.yaml
protolint lint -config_dir_path=path/to . # search path/to for .protolint.yaml
protolint lint -fix . # automatically fix some of the problems reported by some rules
protolint lint -v . # with verbose output to investigate the parsing error
Expand Down Expand Up @@ -261,6 +262,7 @@ The built-in reporter options are:

- plain (default)
- junit
- json
- unix

## Configuring
Expand Down Expand Up @@ -297,6 +299,7 @@ Refer to [_example/config/.protolint.yaml](_example/config/.protolint.yaml) for

protolint will search a current working directory for the config file by default.
And it can search the specified directory with `-config_dir_path` flag.
It can also search the specified file with `--config_path` flag.

## Motivation

Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/protocgenprotolint/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func newFlags(
switch params[0] {
case "":
continue
case "config_path":
if len(params) != 2 {
return nil, fmt.Errorf("config_path should be specified")
}
flags.ConfigDirPath = params[1]
case "config_dir_path":
if len(params) != 2 {
return nil, fmt.Errorf("config_dir_path should be specified")
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/subcmds/lint/cmdLint.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewCmdLint(
return nil, err
}

externalConfig, err := config.GetExternalConfig(flags.ConfigDirPath)
externalConfig, err := config.GetExternalConfig(flags.ConfigPath, flags.ConfigDirPath)
if err != nil {
return nil, err
}
Expand Down
11 changes: 9 additions & 2 deletions internal/cmd/subcmds/lint/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Flags struct {
*flag.FlagSet

FilePaths []string
ConfigPath string
ConfigDirPath string
FixMode bool
Reporter report.Reporter
Expand All @@ -36,11 +37,17 @@ func NewFlags(
var rf reporterFlag
var pf subcmds.PluginFlag

f.StringVar(
&f.ConfigPath,
"config_path",
"",
"path/to/protolint.yaml. Note that if both are set, config_dir_path is ignored.",
)
f.StringVar(
&f.ConfigDirPath,
"config_dir_path",
"",
"path/to/protolint.yaml",
"path/to/the_directory_including_protolint.yaml",
)
f.BoolVar(
&f.FixMode,
Expand All @@ -51,7 +58,7 @@ func NewFlags(
f.Var(
&rf,
"reporter",
`formatter to output results in the specific format. Available reporters are "plain"(default), "junit", and "unix".`,
`formatter to output results in the specific format. Available reporters are "plain"(default), "junit", "json", and "unix".`,
)
f.StringVar(
&f.OutputFilePath,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/subcmds/lint/reporterFlag.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ func GetReporter(value string) (report.Reporter, error) {
if r, ok := rs[value]; ok {
return r, nil
}
return nil, fmt.Errorf(`available reporters are "plain", "junit", and "unix"`)
return nil, fmt.Errorf(`available reporters are "plain", "junit", "json", and "unix"`)
}
7 changes: 6 additions & 1 deletion internal/linter/config/externalConfigProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const (

// GetExternalConfig provides the externalConfig.
func GetExternalConfig(
filePath string,
dirPath string,
) (ExternalConfig, error) {
filePath, err := getExternalConfigPath(dirPath)
filePath, err := getExternalConfigPath(filePath, dirPath)
if err != nil {
return ExternalConfig{}, err
}
Expand All @@ -42,8 +43,12 @@ func GetExternalConfig(
}

func getExternalConfigPath(
filePath string,
dirPath string,
) (string, error) {
if 0 < len(filePath) {
return filePath, nil
}
for _, name := range []string{
externalConfigFileName,
externalConfigFileName2,
Expand Down
2 changes: 1 addition & 1 deletion internal/linter/config/externalConfigProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestGetExternalConfig(t *testing.T) {
} {
test := test
t.Run(test.name, func(t *testing.T) {
got, err := config.GetExternalConfig(test.inputDirPath)
got, err := config.GetExternalConfig("", test.inputDirPath)
if test.wantExistErr {
if err == nil {
t.Errorf("got err nil, but want err")
Expand Down

0 comments on commit c99b4b9

Please sign in to comment.