Run performs artifact scanning
(ctx context.Context, opts flag.Options, targetKind TargetKind)
| 377 | |
| 378 | // Run performs artifact scanning |
| 379 | func 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 | |
| 422 | func run(ctx context.Context, opts flag.Options, targetKind TargetKind) (types.Report, error) { |
| 423 | // Perform validation checks |
no test coverage detected