github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/commands/convert/run.go (about) 1 package convert 2 3 import ( 4 "context" 5 "encoding/json" 6 "os" 7 8 "golang.org/x/xerrors" 9 10 "github.com/devseccon/trivy/pkg/commands/operation" 11 "github.com/devseccon/trivy/pkg/flag" 12 "github.com/devseccon/trivy/pkg/log" 13 "github.com/devseccon/trivy/pkg/report" 14 "github.com/devseccon/trivy/pkg/result" 15 "github.com/devseccon/trivy/pkg/types" 16 ) 17 18 func Run(ctx context.Context, opts flag.Options) (err error) { 19 f, err := os.Open(opts.Target) 20 if err != nil { 21 return xerrors.Errorf("file open error: %w", err) 22 } 23 defer f.Close() 24 25 var r types.Report 26 if err = json.NewDecoder(f).Decode(&r); err != nil { 27 return xerrors.Errorf("json decode error: %w", err) 28 } 29 30 // "convert" supports JSON results produced by Trivy scanning other than AWS and Kubernetes 31 if r.ArtifactName == "" && r.ArtifactType == "" { 32 return xerrors.New("AWS and Kubernetes scanning reports are not yet supported") 33 } 34 35 if err = result.Filter(ctx, r, opts.FilterOpts()); err != nil { 36 return xerrors.Errorf("unable to filter results: %w", err) 37 } 38 39 log.Logger.Debug("Writing report to output...") 40 if err = report.Write(r, opts); err != nil { 41 return xerrors.Errorf("unable to write results: %w", err) 42 } 43 44 operation.ExitOnEOL(opts, r.Metadata) 45 operation.Exit(opts, r.Results.Failed()) 46 47 return nil 48 }