Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add file-types flag #192

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ optional flags:
Subdirectories to exclude when searching for configuration files
-exclude-file-types string
A comma separated list of file types to ignore
-file-types string
A comma separated list of file types to validate. Mutually exclusive with -exclude-file-types
-reporter string
A string representing report format and optional output file path separated by colon if present.
Usage: --reporter <format>:<optional_file_path>
Expand Down Expand Up @@ -118,6 +120,7 @@ func getFlags() (validatorConfig, error) {
depthPtr = flag.Int("depth", 0, "Depth of recursion for the provided search paths. Set depth to 0 to disable recursive path traversal")
excludeDirsPtr = flag.String("exclude-dirs", "", "Subdirectories to exclude when searching for configuration files")
excludeFileTypesPtr = flag.String("exclude-file-types", "", "A comma separated list of file types to ignore")
fileTypesPtr = flag.String("file-types", "", "A comma separated list of file types to validate. Mutually exclusive with --exclude-file-types")
versionPtr = flag.Bool("version", false, "Version prints the release version of validator")
groupOutputPtr = flag.String("groupby", "", "Group output by filetype, directory, pass-fail. Supported for Standard and JSON reports")
quietPtr = flag.Bool("quiet", false, "If quiet flag is set. It doesn't print any output to stdout.")
Expand Down Expand Up @@ -162,6 +165,10 @@ Supported formats: standard, json, junit (default: "standard")`,
}
}

if err := buildExcludeFileTypesFromFileTypes(excludeFileTypesPtr, fileTypesPtr); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW @kehoecj is there a way to validate the behavior of exclusive exclusion (cannot use filter + exclusion) for types in the tests?

Or the code needs to be refactored a lot?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be possible to validate in testing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I think it should be part of the PR

return validatorConfig{}, err
}

err = validateGroupByConf(groupOutputPtr)
if err != nil {
return validatorConfig{}, err
Expand All @@ -181,6 +188,17 @@ Supported formats: standard, json, junit (default: "standard")`,
return config, nil
}

func buildExcludeFileTypesFromFileTypes(excludeFileTypesPtr, fileTypesPtr *string) error {
if excludeFileTypesPtr != nil && fileTypesPtr != nil && *excludeFileTypesPtr != "" && *fileTypesPtr != "" {
return errors.New("Cannot use exclude-file-types and file-types together")
}

if fileTypesPtr == nil || *fileTypesPtr == "" {
return nil
}
return flag.Set("exclude-file-types", getExcludeFileTypesFromFileTypes(fileTypesPtr))
}

func validateReporterConf(conf map[string]string, groupBy *string) error {
acceptedReportTypes := map[string]bool{"standard": true, "json": true, "junit": true, "sarif": true}
groupOutputReportTypes := map[string]bool{"standard": true, "json": true}
Expand Down Expand Up @@ -279,6 +297,7 @@ func applyDefaultFlagsFromEnv() error {
"depth": "CFV_DEPTH",
"exclude-dirs": "CFV_EXCLUDE_DIRS",
"exclude-file-types": "CFV_EXCLUDE_FILE_TYPES",
"file-types": "CFV_FILE_TYPES",
"reporter": "CFV_REPORTER",
"groupby": "CFV_GROUPBY",
"quiet": "CFV_QUIET",
Expand Down Expand Up @@ -307,6 +326,22 @@ func setFlagFromEnvIfNotSet(flagName string, envVar string) error {
return nil
}

// Build exclude-file-type list from file-type values
func getExcludeFileTypesFromFileTypes(fileTypesPtr *string) string {
validTypes := make([]string, 0, len(filetype.FileTypes))
includeTypes := strings.Split(*fileTypesPtr, ",")

for _, t := range filetype.FileTypes {
validTypes = append(validTypes, t.Name)
}

excludeFileTypes := slices.DeleteFunc(validTypes, func(ty string) bool {
return slices.Contains(includeTypes, ty)
})

return strings.Join(excludeFileTypes, ",")
}

// Return the reporter associated with the
// reportType string
func getReporter(reportType, outputDest *string) reporter.Reporter {
Expand Down
2 changes: 2 additions & 0 deletions cmd/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func Test_flags(t *testing.T) {
{"grouped sarif", []string{"-groupby=directory", "--reporter=sarif", "."}, 1},
{"groupby duplicate", []string{"--groupby=directory,directory", "."}, 1},
{"quiet flag", []string{"--quiet=true", "."}, 0},
{"exclude file types and file types", []string{"--exclude-file-types=yaml", "--file-types=yaml"}, 1},
{"file types set", []string{"--file-types=yaml"}, 0},
}
for _, tc := range cases {
// this call is required because otherwise flags panics,
Expand Down
Loading