Skip to content

Commit a8b633f

Browse files
Adding stdout and verbose flags and refactor how the report is saved
1 parent 103c429 commit a8b633f

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ file. The output format is controlled by the `-fmt` flag, and the output file is
302302
$ gosec -fmt=json -out=results.json *.go
303303
```
304304

305+
Results will be reported to stdout as well as to the provided output file by `-stdout` flag. The `-verbose` flag overrides the
306+
output format when stdout the results while saving them in the output file
307+
```bash
308+
# Write output in json format to results.json as well as stdout
309+
$ gosec -fmt=json -out=results.json -stdout *.go
310+
311+
# Overrides the output format to 'text' when stdout the results, while writing it to results.json
312+
$ gosec -fmt=json -out=results.json -stdout -verbose=text *.go
313+
```
314+
305315
**Note:** gosec generates the [generic issue import format](https://docs.sonarqube.org/latest/analysis/generic-issue/) for SonarQube, and a report has to be imported into SonarQube using `sonar.externalIssuesReportPaths=path/to/gosec-report.json`.
306316

307317
## Development

cmd/gosec/main.go

+51-20
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ var (
117117
// print version and quit with exit code 0
118118
flagVersion = flag.Bool("version", false, "Print version and quit with exit code 0")
119119

120+
// stdout the results as well as write it in the output file
121+
flagStdOut = flag.Bool("stdout", false, "Stdout the results as well as write it in the output file")
122+
123+
// overrides the output format when stdout the results while saving them in the output file
124+
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")
125+
120126
// exlude the folders from scan
121127
flagDirsExclude arrayFlags
122128

@@ -187,30 +193,45 @@ func loadRules(include, exclude string) rules.RuleList {
187193
return rules.Generate(filters...)
188194
}
189195

190-
func saveOutput(filename, format string, color bool, paths []string, issues []*gosec.Issue, metrics *gosec.Metrics, errors map[string][]gosec.Error) error {
196+
func getRootPaths(paths []string) []string {
191197
rootPaths := []string{}
192198
for _, path := range paths {
193199
rootPath, err := gosec.RootPath(path)
194200
if err != nil {
195-
return fmt.Errorf("failed to get the root path of the projects: %s", err)
201+
logger.Fatal(fmt.Errorf("failed to get the root path of the projects: %s", err))
196202
}
197203
rootPaths = append(rootPaths, rootPath)
198204
}
199-
if filename != "" {
200-
outfile, err := os.Create(filename)
201-
if err != nil {
202-
return err
203-
}
204-
defer outfile.Close() // #nosec G307
205-
err = report.CreateReport(outfile, format, color, rootPaths, issues, metrics, errors)
206-
if err != nil {
207-
return err
208-
}
209-
} else {
210-
err := report.CreateReport(os.Stdout, format, color, rootPaths, issues, metrics, errors)
211-
if err != nil {
212-
return err
213-
}
205+
return rootPaths
206+
}
207+
208+
func getPrintedFormat(format string, verbose string) string {
209+
var fileFormat = format
210+
if format != "" && verbose != "" {
211+
fileFormat = verbose
212+
}
213+
return fileFormat
214+
}
215+
216+
func printReport(format string, color bool, rootPaths []string, issues []*gosec.Issue, metrics *gosec.Metrics, errors map[string][]gosec.Error) error {
217+
218+
err := report.CreateReport(os.Stdout, format, color, rootPaths, issues, metrics, errors)
219+
if err != nil {
220+
return err
221+
}
222+
return nil
223+
}
224+
225+
func saveReport(filename, format string, color bool, rootPaths []string, issues []*gosec.Issue, metrics *gosec.Metrics, errors map[string][]gosec.Error) error {
226+
227+
outfile, err := os.Create(filename)
228+
if err != nil {
229+
return err
230+
}
231+
defer outfile.Close() // #nosec G307
232+
err = report.CreateReport(outfile, format, color, rootPaths, issues, metrics, errors)
233+
if err != nil {
234+
return err
214235
}
215236
return nil
216237
}
@@ -291,7 +312,7 @@ func main() {
291312

292313
// Color flag is allowed for text format
293314
var color bool
294-
if *flagFormat == "text" {
315+
if *flagFormat == "text" || *flagVerbose == "text" {
295316
color = true
296317
}
297318

@@ -363,8 +384,18 @@ func main() {
363384
}
364385

365386
// Create output report
366-
if err := saveOutput(*flagOutput, *flagFormat, color, flag.Args(), issues, metrics, errors); err != nil {
367-
logger.Fatal(err)
387+
rootPaths := getRootPaths(flag.Args())
388+
389+
if *flagOutput == "" || *flagStdOut {
390+
var fileFormat = getPrintedFormat(*flagOutput, *flagVerbose)
391+
if err := printReport(fileFormat, color, rootPaths, issues, metrics, errors); err != nil {
392+
logger.Fatal((err))
393+
}
394+
}
395+
if *flagOutput != "" {
396+
if err := saveReport(*flagOutput, *flagFormat, color, rootPaths, issues, metrics, errors); err != nil {
397+
logger.Fatal(err)
398+
}
368399
}
369400

370401
// Finalize logging

0 commit comments

Comments
 (0)