Skip to content

Commit ea5d31f

Browse files
authored
Add a recursive flag -r to skip specifying ./... path
* added recursive flag to skip specifying ./... path * refactored to remove code duplication
1 parent 48bbf96 commit ea5d31f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

cmd/gosec/main.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ var (
133133
// print the text report with color, this is enabled by default
134134
flagColor = flag.Bool("color", true, "Prints the text format report with colorization when it goes in the stdout")
135135

136+
// append ./... to the target dir.
137+
flagRecursive = flag.Bool("r", false, "Appends \"./...\" to the target dir.")
138+
136139
// overrides the output format when stdout the results while saving them in the output file
137140
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")
138141

@@ -319,9 +322,9 @@ func main() {
319322
os.Exit(0)
320323
}
321324

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

381384
excludedDirs := gosec.ExcludedDirsRegExp(flagDirsExclude)
382385
var packages []string
383-
for _, path := range flag.Args() {
386+
387+
paths := flag.Args()
388+
if len(paths) == 0 {
389+
paths = append(paths, "./...")
390+
}
391+
for _, path := range paths {
384392
pcks, err := gosec.PackagePaths(path, excludedDirs)
385393
if err != nil {
386394
logger.Fatal(err)
387395
}
388396
packages = append(packages, pcks...)
389397
}
398+
390399
if len(packages) == 0 {
391400
logger.Fatal("No packages found")
392401
}

0 commit comments

Comments
 (0)