Skip to content

Commit d8cfcd6

Browse files
authored
Allow the user to enable/disable colorisation of the text report in the stdout
1 parent a8b633f commit d8cfcd6

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

cmd/gosec/main.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ var (
120120
// stdout the results as well as write it in the output file
121121
flagStdOut = flag.Bool("stdout", false, "Stdout the results as well as write it in the output file")
122122

123+
//print the text report with color, this is enabled by default
124+
flagColor = flag.Bool("color", true, "Prints the text format report with colorization when it goes in the stdout")
125+
123126
// overrides the output format when stdout the results while saving them in the output file
124127
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")
125128

@@ -222,14 +225,14 @@ func printReport(format string, color bool, rootPaths []string, issues []*gosec.
222225
return nil
223226
}
224227

225-
func saveReport(filename, format string, color bool, rootPaths []string, issues []*gosec.Issue, metrics *gosec.Metrics, errors map[string][]gosec.Error) error {
228+
func saveReport(filename, format string, rootPaths []string, issues []*gosec.Issue, metrics *gosec.Metrics, errors map[string][]gosec.Error) error {
226229

227230
outfile, err := os.Create(filename)
228231
if err != nil {
229232
return err
230233
}
231234
defer outfile.Close() // #nosec G307
232-
err = report.CreateReport(outfile, format, color, rootPaths, issues, metrics, errors)
235+
err = report.CreateReport(outfile, format, false, rootPaths, issues, metrics, errors)
233236
if err != nil {
234237
return err
235238
}
@@ -310,12 +313,6 @@ func main() {
310313
logger = log.New(logWriter, "[gosec] ", log.LstdFlags)
311314
}
312315

313-
// Color flag is allowed for text format
314-
var color bool
315-
if *flagFormat == "text" || *flagVerbose == "text" {
316-
color = true
317-
}
318-
319316
failSeverity, err := convertToScore(*flagSeverity)
320317
if err != nil {
321318
logger.Fatalf("Invalid severity value: %v", err)
@@ -388,12 +385,12 @@ func main() {
388385

389386
if *flagOutput == "" || *flagStdOut {
390387
var fileFormat = getPrintedFormat(*flagOutput, *flagVerbose)
391-
if err := printReport(fileFormat, color, rootPaths, issues, metrics, errors); err != nil {
388+
if err := printReport(fileFormat, *flagColor, rootPaths, issues, metrics, errors); err != nil {
392389
logger.Fatal((err))
393390
}
394391
}
395392
if *flagOutput != "" {
396-
if err := saveReport(*flagOutput, *flagFormat, color, rootPaths, issues, metrics, errors); err != nil {
393+
if err := saveReport(*flagOutput, *flagFormat, rootPaths, issues, metrics, errors); err != nil {
397394
logger.Fatal(err)
398395
}
399396
}

0 commit comments

Comments
 (0)