Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(flag): skip hidden flags for --generate-default-config command #8046

Merged
merged 5 commits into from
Dec 5, 2024

Conversation

DmitriyLewen
Copy link
Contributor

Description

Currently we include all flags in the default config.
But Trivy returns an error for removed flags.

Unfortunately viper has no options to remove flags, so we need to reset viper and add the required flags to --generate-default-config

Before:

➜  trivy image --generate-default-config && cat trivy-default.yaml | grep reset
2024-12-04T17:35:49+06:00       INFO    Writing the default config to trivy-default.yaml...
    reset-checks-bundle: false
reset: false

After:

➜  ./trivy image --generate-default-config && cat trivy-default.yaml | grep reset
2024-12-04T17:36:18+06:00       INFO    Writing the default config to trivy-default.yaml...

Related issues

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@DmitriyLewen DmitriyLewen self-assigned this Dec 4, 2024
@DmitriyLewen DmitriyLewen changed the title fix(flag): skip hidden flags for --generate-default-config flag fix(flag): skip hidden flags for --generate-default-config command Dec 4, 2024
@DmitriyLewen DmitriyLewen marked this pull request as ready for review December 4, 2024 11:42
Comment on lines 364 to 377
for _, k := range viper.AllKeys() {
// Skip the `GenerateDefaultConfigFlag` flags to avoid errors with default config file.
// Users often use "normal" formats instead of compliance. So we'll skip ComplianceFlag
// 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)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to reuse the global viper instance? Can't we initiate a new instance?

Suggested change
for _, k := range viper.AllKeys() {
// Skip the `GenerateDefaultConfigFlag` flags to avoid errors with default config file.
// Users often use "normal" formats instead of compliance. So we'll skip ComplianceFlag
// 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)
}
v := viper.New()
for _, k := range viper.AllKeys() {
// Skip the `GenerateDefaultConfigFlag` flags to avoid errors with default config file.
// Users often use "normal" formats instead of compliance. So we'll skip ComplianceFlag
// 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
}
v.Set(k, viper.Get(k))
}
v.SafeWriteConfigAs("trivy-default.yaml")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm.. why not.
Updated in d8c0edf

@knqyf263 knqyf263 added this pull request to the merge queue Dec 5, 2024
Merged via the queue into aquasecurity:main with commit 5e68bdc Dec 5, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: config file generated by --generate-default-config contains removed and deprecated flags
2 participants