Skip to content

Commit

Permalink
feat: add logic to overwrite flags in viper
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Dec 4, 2024
1 parent 9295a08 commit 05fd0cb
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,25 @@ func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err err

if opts.GenerateDefaultConfig {
log.Info("Writing the default config to trivy-default.yaml...")

hiddenFlags := flag.HiddenFlags()
allFlags := map[string]any{}

Check failure on line 361 in pkg/commands/artifact/run.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

ruleguard: replace 'map[string]any{}' with 'make(map[string]any) (gocritic)
// Viper does not have the ability to remove flags.
// So we only save the necessary flags and set these flags after viper.Reset
for _, k := range viper.AllKeys() {
// Skip the `GenerateDefaultConfigFlag` and `ComplianceFlag` flags to avoid errors with default config file.
// Also don't keep removed or deprecated flags to avoid confusing users.
if k == flag.GenerateDefaultConfigFlag.ConfigName || k == flag.ComplianceFlag.ConfigName || slices.Contains(hiddenFlags, k) {
continue
}
allFlags[k] = viper.Get(k)
}

viper.Reset()
for k, v := range allFlags {
viper.Set(k, v)
}

return viper.SafeWriteConfigAs("trivy-default.yaml")
}

Expand Down

0 comments on commit 05fd0cb

Please sign in to comment.