Add a recursive flag -r to skip specifying ./... path

* added recursive flag to skip specifying ./... path

* refactored to remove code duplication
This commit is contained in:
Per Arn 2022-03-07 10:31:22 +01:00 committed by GitHub
parent 48bbf96b56
commit ea5d31f7f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -133,6 +133,9 @@ var (
// print the text report with color, this is enabled by default // print the text report with color, this is enabled by default
flagColor = flag.Bool("color", true, "Prints the text format report with colorization when it goes in the stdout") flagColor = flag.Bool("color", true, "Prints the text format report with colorization when it goes in the stdout")
// append ./... to the target dir.
flagRecursive = flag.Bool("r", false, "Appends \"./...\" to the target dir.")
// overrides the output format when stdout the results while saving them in the output file // overrides the output format when stdout the results while saving them in the output file
flagVerbose = flag.String("verbose", "", "Overrides the output format when stdout the results while saving them in the output file.\nValid options are: json, yaml, csv, junit-xml, html, sonarqube, golint, sarif or text") flagVerbose = flag.String("verbose", "", "Overrides the output format when stdout the results while saving them in the output file.\nValid options are: json, yaml, csv, junit-xml, html, sonarqube, golint, sarif or text")
@ -319,9 +322,9 @@ func main() {
os.Exit(0) os.Exit(0)
} }
// Ensure at least one file was specified // Ensure at least one file was specified or that the recursive -r flag was set.
if flag.NArg() == 0 { if flag.NArg() == 0 && !*flagRecursive {
fmt.Fprintf(os.Stderr, "\nError: FILE [FILE...] or './...' expected\n") //#nosec fmt.Fprintf(os.Stderr, "\nError: FILE [FILE...] or './...' or -r expected\n") //#nosec
flag.Usage() flag.Usage()
os.Exit(1) os.Exit(1)
} }
@ -380,13 +383,19 @@ func main() {
excludedDirs := gosec.ExcludedDirsRegExp(flagDirsExclude) excludedDirs := gosec.ExcludedDirsRegExp(flagDirsExclude)
var packages []string var packages []string
for _, path := range flag.Args() {
paths := flag.Args()
if len(paths) == 0 {
paths = append(paths, "./...")
}
for _, path := range paths {
pcks, err := gosec.PackagePaths(path, excludedDirs) pcks, err := gosec.PackagePaths(path, excludedDirs)
if err != nil { if err != nil {
logger.Fatal(err) logger.Fatal(err)
} }
packages = append(packages, pcks...) packages = append(packages, pcks...)
} }
if len(packages) == 0 { if len(packages) == 0 {
logger.Fatal("No packages found") logger.Fatal("No packages found")
} }