Skip to content

Commit

Permalink
feat: add no-color flag (#1015)
Browse files Browse the repository at this point in the history
* feat: add no-color flag

* fix: use set logger for all report output

* chore: update snapshots
  • Loading branch information
elsapet committed May 29, 2023
1 parent 3990de3 commit 9c987ec
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/_data/bearer_scan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ options:
default_value: '[]'
usage: |
Define regular expressions for better classification of private or unreachable domains e.g. --internal-domains=".*.my-company.com,private.sh"
- name: no-color
default_value: "false"
usage: Disable color in output
- name: only-rule
default_value: '[]'
usage: |
Expand Down
1 change: 1 addition & 0 deletions e2e/flags/.snapshots/TestInitCommand
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
disable-version-check: false
report:
format: ""
no-color: false
output: ""
report: security
severity: critical,high,medium,low,warning
Expand Down
1 change: 1 addition & 0 deletions e2e/flags/.snapshots/TestMetadataFlags-help-scan
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Scan Flags
General Flags
--config-file string Load configuration from the specified path. (default "bearer.yml")
--disable-version-check Disable Bearer version checking
--no-color Disable color in output


--
Expand Down
1 change: 1 addition & 0 deletions e2e/flags/.snapshots/TestMetadataFlags-scan-help
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Scan Flags
General Flags
--config-file string Load configuration from the specified path. (default "bearer.yml")
--disable-version-check Disable Bearer version checking
--no-color Disable color in output


--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Scan Flags
General Flags
--config-file string Load configuration from the specified path. (default "bearer.yml")
--disable-version-check Disable Bearer version checking
--no-color Disable color in output


flag error: scan flag error: invalid context argument; supported values: health
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Scan Flags
General Flags
--config-file string Load configuration from the specified path. (default "bearer.yml")
--disable-version-check Disable Bearer version checking
--no-color Disable color in output


flag error: report flags error: invalid format argument; supported values: json, yaml, sarif, gitlab-sast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Scan Flags
General Flags
--config-file string Load configuration from the specified path. (default "bearer.yml")
--disable-version-check Disable Bearer version checking
--no-color Disable color in output


flag error: report flags error: invalid report argument; supported values: security, privacy
Expand Down
6 changes: 3 additions & 3 deletions pkg/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (r *runner) Report(config settings.Config, report types.Report) (bool, erro
return false, err
}

outputhandler.StdOutLogger().Msg(placeholderStr.String())
logger.Msg(placeholderStr.String())
return true, nil
}

Expand All @@ -340,7 +340,7 @@ func (r *runner) Report(config settings.Config, report types.Report) (bool, erro
detectionReport := detections.(*security.Results)
reportStr, reportPassed := security.BuildReportString(config, detectionReport, report.Inputgocloc, dataflow)

outputhandler.StdOutLogger().Msg(reportStr.String())
logger.Msg(reportStr.String())

return reportPassed, nil
} else if config.Report.Report == flag.ReportPrivacy {
Expand All @@ -350,7 +350,7 @@ func (r *runner) Report(config settings.Config, report types.Report) (bool, erro
return false, fmt.Errorf("error generating report %s", err)
}

outputhandler.StdOutLogger().Msg(*content)
logger.Msg(*content)

return true, nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/process/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Config struct {
BuiltInRules map[string]*Rule `mapstructure:"built_in_rules" json:"built_in_rules" yaml:"built_in_rules"`
CacheUsed bool `mapstructure:"cache_used" json:"cache_used" yaml:"cache_used"`
BearerRulesVersion string `mapstructure:"bearer_rules_version" json:"bearer_rules_version" yaml:"bearer_rules_version"`
NoColor bool `mapstructure:"no_color" json:"no_color" yaml:"no_color"`
}

type Modules []*PolicyModule
Expand Down Expand Up @@ -298,6 +299,7 @@ func FromOptions(opts flag.Options, foundLanguages []string) (Config, error) {
Worker: workerOptions,
Scan: opts.ScanOptions,
Report: opts.ReportOptions,
NoColor: opts.GeneralOptions.NoColor || opts.ReportOptions.Output != "",
Policies: policies,
Rules: result.Rules,
BuiltInRules: result.BuiltInRules,
Expand Down
11 changes: 11 additions & 0 deletions pkg/flag/general_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,28 @@ var (
Value: false,
Usage: "Disable Bearer version checking",
}
NoColorFlag = Flag{
Name: "no-color",
ConfigName: "report.no-color",
Value: false,
Usage: "Disable color in output",
}
)

type GeneralFlagGroup struct {
ConfigFile *Flag
APIKey *Flag
Host *Flag
DisableVersionCheck *Flag
NoColor *Flag
}

// GlobalOptions defines flags and other configuration parameters for all the subcommands
type GeneralOptions struct {
ConfigFile string `json:"config_file" yaml:"config_file"`
Client *api.API
DisableVersionCheck bool
NoColor bool `mapstructure:"no_color" json:"no_color" yaml:"no_color"`
}

func NewGeneralFlagGroup() *GeneralFlagGroup {
Expand All @@ -60,6 +68,7 @@ func NewGeneralFlagGroup() *GeneralFlagGroup {
APIKey: &APIKeyFlag,
Host: &HostFlag,
DisableVersionCheck: &DisableVersionCheckFlag,
NoColor: &NoColorFlag,
}
}

Expand All @@ -73,6 +82,7 @@ func (f *GeneralFlagGroup) Flags() []*Flag {
f.APIKey,
f.Host,
f.DisableVersionCheck,
f.NoColor,
}
}

Expand All @@ -98,5 +108,6 @@ func (f *GeneralFlagGroup) ToOptions() GeneralOptions {
Client: client,
ConfigFile: getString(f.ConfigFile),
DisableVersionCheck: getBool(f.DisableVersionCheck),
NoColor: getBool(f.NoColor),
}
}
3 changes: 1 addition & 2 deletions pkg/report/output/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,14 @@ func evaluateRules(
}

func BuildReportString(config settings.Config, results *Results, lineOfCodeOutput *gocloc.Result, dataflow *dataflow.DataFlow) (*strings.Builder, bool) {
withoutColor := config.Report.Output != ""
severityForFailure := config.Report.Severity
reportStr := &strings.Builder{}

reportStr.WriteString("\n\nSummary Report\n")
reportStr.WriteString("\n=====================================")

initialColorSetting := color.NoColor
if withoutColor && !initialColorSetting {
if config.NoColor && !initialColorSetting {
color.NoColor = true
}

Expand Down

0 comments on commit 9c987ec

Please sign in to comment.