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

     1  package report
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  
     8  	"golang.org/x/xerrors"
     9  
    10  	"github.com/devseccon/trivy/pkg/types"
    11  )
    12  
    13  // JSONWriter implements result Writer
    14  type JSONWriter struct {
    15  	Output io.Writer
    16  }
    17  
    18  // Write writes the results in JSON format
    19  func (jw JSONWriter) Write(report types.Report) error {
    20  	output, err := json.MarshalIndent(report, "", "  ")
    21  	if err != nil {
    22  		return xerrors.Errorf("failed to marshal json: %w", err)
    23  	}
    24  
    25  	if _, err = fmt.Fprintln(jw.Output, string(output)); err != nil {
    26  		return xerrors.Errorf("failed to write json: %w", err)
    27  	}
    28  	return nil
    29  }