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

Display excludeable file types and validate #178

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
60 changes: 42 additions & 18 deletions cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (

configfilevalidator "github.com/Boeing/config-file-validator"
"github.com/Boeing/config-file-validator/pkg/cli"
"github.com/Boeing/config-file-validator/pkg/filetype"
"github.com/Boeing/config-file-validator/pkg/finder"
"github.com/Boeing/config-file-validator/pkg/reporter"
)
Expand Down Expand Up @@ -65,21 +66,46 @@ func validatorUsage() {
flag.PrintDefaults()
}

// Assemble pretty formatted list of file types
func getFileTypes() []string {
options := make([]string, 0, len(filetype.FileTypes))
for _, typ := range filetype.FileTypes {
options = append(options, typ.Name)
}
return options
}

// Given a slice of strings, validates each is a valid file type
func validateFileTypeList(input []string) bool {
types := getFileTypes()
for _, t := range input {
if len(t) == 0 {
continue
}
if !slices.Contains(types, t) {
return false
}
}
return true
}

// Parses, validates, and returns the flags
// flag.String returns a pointer
// If a required parameter is missing the help
// output will be displayed and the function
// will return with exit = 1
func getFlags() (validatorConfig, error) {
flag.Usage = validatorUsage
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")
outputPtr := flag.String("output", "", "Destination to a file to output results")
reportTypePtr := flag.String("reporter", "standard", "Format of the printed report. Options are standard and json")
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")
quietPrt := flag.Bool("quiet", false, "If quiet flag is set. It doesn't print any output to stdout.")
var (
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.\nValid options: "+strings.Join(getFileTypes(), ","))
outputPtr = flag.String("output", "", "Destination to a file to output results")
reportTypePtr = flag.String("reporter", "standard", "Format of the printed report. Options are standard and json")
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")
quietPrt = flag.Bool("quiet", false, "If quiet flag is set. It doesn't print any output to stdout.")
)
flag.Parse()

searchPaths := make([]string, 0)
Expand All @@ -94,23 +120,23 @@ func getFlags() (validatorConfig, error) {
}

if *reportTypePtr != "standard" && *reportTypePtr != "json" && *reportTypePtr != "junit" {
fmt.Println("Wrong parameter value for reporter, only supports standard, json or junit")
flag.Usage()
return validatorConfig{}, errors.New("Wrong parameter value for reporter, only supports standard, json or junit")
}

if *reportTypePtr == "junit" && *groupOutputPtr != "" {
fmt.Println("Wrong parameter value for reporter, groupby is not supported for JUnit reports")
flag.Usage()
return validatorConfig{}, errors.New("Wrong parameter value for reporter, groupby is not supported for JUnit reports")
}

if depthPtr != nil && isFlagSet("depth") && *depthPtr < 0 {
fmt.Println("Wrong parameter value for depth, value cannot be negative.")
flag.Usage()
return validatorConfig{}, errors.New("Wrong parameter value for depth, value cannot be negative")
}

if *excludeFileTypesPtr != "" {
if !validateFileTypeList(strings.Split(*excludeFileTypesPtr, ",")) {
return validatorConfig{}, errors.New("Invalid exclude file type")
}
}

groupByCleanString := cleanString("groupby")
groupByUserInput := strings.Split(groupByCleanString, ",")
groupByAllowedValues := []string{"filetype", "directory", "pass-fail"}
Expand All @@ -120,13 +146,9 @@ func getFlags() (validatorConfig, error) {
if groupOutputPtr != nil && isFlagSet("groupby") {
for _, groupBy := range groupByUserInput {
if !slices.Contains(groupByAllowedValues, groupBy) {
fmt.Println("Wrong parameter value for groupby, only supports filetype, directory, pass-fail")
flag.Usage()
return validatorConfig{}, errors.New("Wrong parameter value for groupby, only supports filetype, directory, pass-fail")
}
if _, ok := seenValues[groupBy]; ok {
fmt.Println("Wrong parameter value for groupby, duplicate values are not allowed")
flag.Usage()
return validatorConfig{}, errors.New("Wrong parameter value for groupby, duplicate values are not allowed")
}
seenValues[groupBy] = true
Expand Down Expand Up @@ -187,6 +209,8 @@ func cleanString(command string) string {
func mainInit() int {
validatorConfig, err := getFlags()
if err != nil {
fmt.Println(err.Error())
flag.Usage()
return 1
}

Expand Down
Loading