MCPcopy
hub / github.com/aquasecurity/trivy / Run

Function Run

pkg/commands/artifact/run.go:379–420  ·  view source on GitHub ↗

Run performs artifact scanning

(ctx context.Context, opts flag.Options, targetKind TargetKind)

Source from the content-addressed store, hash-verified

377
378// Run performs artifact scanning
379func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err error) {
380 ctx, cancel := context.WithTimeout(ctx, opts.Timeout)
381 defer cancel()
382
383 if opts.GenerateDefaultConfig {
384 log.Info("Writing the default config to trivy-default.yaml...")
385
386 hiddenFlags := flag.HiddenFlags()
387 // Viper does not have the ability to remove flags.
388 // So we only save the necessary flags and set these flags after viper.Reset
389 v := viper.New()
390 for _, k := range viper.AllKeys() {
391 // Skip the `GenerateDefaultConfigFlag` flags to avoid errors with default config file.
392 // Users often use "normal" formats instead of compliance. So we'll skip ComplianceFlag
393 // Also don't keep removed or deprecated flags to avoid confusing users.
394 if k == flag.GenerateDefaultConfigFlag.ConfigName || k == flag.ComplianceFlag.ConfigName || slices.Contains(hiddenFlags, k) {
395 continue
396 }
397 v.Set(k, viper.Get(k))
398 }
399
400 return v.SafeWriteConfigAs("trivy-default.yaml")
401 }
402
403 // Call pre-run hooks
404 if err := extension.PreRun(ctx, opts); err != nil {
405 return xerrors.Errorf("pre run error: %w", err)
406 }
407
408 // Run the application
409 report, err := run(ctx, opts, targetKind)
410 if err != nil {
411 return xerrors.Errorf("run error: %w", err)
412 }
413
414 // Call post-run hooks
415 if err := extension.PostRun(ctx, opts); err != nil {
416 return xerrors.Errorf("post run error: %w", err)
417 }
418
419 return operation.Exit(opts, report.Results.Failed(), report.Metadata)
420}
421
422func run(ctx context.Context, opts flag.Options, targetKind TargetKind) (types.Report, error) {
423 // Perform validation checks

Callers 8

NewImageCommandFunction · 0.92
NewFilesystemCommandFunction · 0.92
NewRootfsCommandFunction · 0.92
NewRepositoryCommandFunction · 0.92
NewClientCommandFunction · 0.92
NewConfigCommandFunction · 0.92
NewVMCommandFunction · 0.92
NewSBOMCommandFunction · 0.92

Calls 11

HiddenFlagsFunction · 0.92
PreRunFunction · 0.92
PostRunFunction · 0.92
ExitFunction · 0.92
InfoMethod · 0.80
runFunction · 0.70
NewMethod · 0.65
ContainsMethod · 0.65
GetMethod · 0.65
SetMethod · 0.45
FailedMethod · 0.45

Tested by

no test coverage detected