Skip to content

Commit

Permalink
Add a recursive flag -r to skip specifying ./... path
Browse files Browse the repository at this point in the history
* added recursive flag to skip specifying ./... path

* refactored to remove code duplication
  • Loading branch information
ArnPellesGit committed Mar 7, 2022
1 parent 48bbf96 commit ea5d31f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions cmd/gosec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ var (
// 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")

// 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
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")

Expand Down Expand Up @@ -319,9 +322,9 @@ func main() {
os.Exit(0)
}

// Ensure at least one file was specified
if flag.NArg() == 0 {
fmt.Fprintf(os.Stderr, "\nError: FILE [FILE...] or './...' expected\n") //#nosec
// Ensure at least one file was specified or that the recursive -r flag was set.
if flag.NArg() == 0 && !*flagRecursive {
fmt.Fprintf(os.Stderr, "\nError: FILE [FILE...] or './...' or -r expected\n") //#nosec
flag.Usage()
os.Exit(1)
}
Expand Down Expand Up @@ -380,13 +383,19 @@ func main() {

excludedDirs := gosec.ExcludedDirsRegExp(flagDirsExclude)
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)
if err != nil {
logger.Fatal(err)
}
packages = append(packages, pcks...)
}

if len(packages) == 0 {
logger.Fatal("No packages found")
}
Expand Down

0 comments on commit ea5d31f

Please sign in to comment.