Skip to content

Commit

Permalink
Add option to disable printing results in color (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
genevieveluyt committed Jul 28, 2021
1 parent ca64457 commit 47c31d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 9 additions & 3 deletions kubeaudit.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,23 @@
//
// Or, to run the audit in local mode:
//
// report, err := kubeAuditor.AuditLocal("/path/to/kubeconfig.yml")
// report, err := kubeAuditor.AuditLocal("/path/to/kubeconfig.yml", kubeaudit.AuditOptions{})
//
// Or, to run the audit in cluster mode (pass it a namespace name as a string to only audit resources in that namespace, or an empty string to audit resources in all namespaces):
//
// report, err := auditor.AuditCluster("")
// report, err := auditor.AuditCluster(kubeaudit.AuditOptions{})
//
// Get the results
//
// To print the results in a human readable way:
//
// report.PrintResults(os.Stdout, kubeaudit.Info, nil)
// report.PrintResults()
//
// Results are printed to standard out by default. To print to a string instead:
//
// var buf bytes.Buffer
// report.PrintResults(kubeaudit.WithWriter(&buf), kubeaudit.WithColor(false))
// resultsString := buf.String()
//
// Or, to get the result objects:
//
Expand Down
15 changes: 12 additions & 3 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,35 @@ type Printer struct {

type PrintOption func(p *Printer)

// WithMinSeverity sets the minimum severity of results that will be printed.
func WithMinSeverity(minSeverity SeverityLevel) PrintOption {
return func(p *Printer) {
p.minSeverity = minSeverity
}
}

// WithWriter sets the writer where results will be written to.
func WithWriter(writer io.Writer) PrintOption {
return func(p *Printer) {
p.writer = writer
}
}

// WithFormatter sets a logrus formatter to use to format results.
func WithFormatter(formatter log.Formatter) PrintOption {
return func(p *Printer) {
p.formatter = formatter
}
}

// WithColor specifies whether or not to colorize output. You will likely want to set this to false if
// not writing to standard out.
func WithColor(color bool) PrintOption {
return func(p *Printer) {
p.color = color
}
}

func (p *Printer) parseOptions(opts ...PrintOption) {
for _, opt := range opts {
opt(p)
Expand All @@ -48,11 +59,9 @@ func NewPrinter(opts ...PrintOption) Printer {
p := Printer{
writer: os.Stdout,
minSeverity: Info,
color: true,
}
p.parseOptions(opts...)
if p.writer == os.Stdout {
p.color = true
}
return p
}

Expand Down

0 comments on commit 47c31d5

Please sign in to comment.