github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/k8s/writer.go (about)

     1  package k8s
     2  
     3  import (
     4  	"fmt"
     5  
     6  	cdx "github.com/CycloneDX/cyclonedx-go"
     7  
     8  	"github.com/devseccon/trivy/pkg/k8s/report"
     9  	"github.com/devseccon/trivy/pkg/report/table"
    10  	"github.com/devseccon/trivy/pkg/types"
    11  )
    12  
    13  type Writer interface {
    14  	Write(report.Report) error
    15  }
    16  
    17  // Write writes the results in the give format
    18  func Write(k8sreport report.Report, option report.Option) error {
    19  	k8sreport.PrintErrors()
    20  
    21  	switch option.Format {
    22  	case types.FormatJSON:
    23  		jwriter := report.JSONWriter{
    24  			Output: option.Output,
    25  			Report: option.Report,
    26  		}
    27  		return jwriter.Write(k8sreport)
    28  	case types.FormatTable:
    29  		separatedReports := report.SeparateMisconfigReports(k8sreport, option.Scanners, option.Components)
    30  
    31  		if option.Report == report.SummaryReport {
    32  			target := fmt.Sprintf("Summary Report for %s", k8sreport.ClusterName)
    33  			table.RenderTarget(option.Output, target, table.IsOutputToTerminal(option.Output))
    34  		}
    35  
    36  		for _, r := range separatedReports {
    37  			writer := &report.TableWriter{
    38  				Output:        option.Output,
    39  				Report:        option.Report,
    40  				Severities:    option.Severities,
    41  				ColumnHeading: report.ColumnHeading(option.Scanners, option.Components, r.Columns),
    42  			}
    43  
    44  			if err := writer.Write(r.Report); err != nil {
    45  				return err
    46  			}
    47  		}
    48  
    49  		return nil
    50  	case types.FormatCycloneDX:
    51  		w := report.NewCycloneDXWriter(option.Output, cdx.BOMFileFormatJSON, option.APIVersion)
    52  		return w.Write(k8sreport.RootComponent)
    53  	}
    54  	return nil
    55  }