From 9295a08d7cce261981a5bc7dbaa925ebb56d3a2b Mon Sep 17 00:00:00 2001 From: DmitriyLewen Date: Wed, 4 Dec 2024 17:31:22 +0600 Subject: [PATCH 1/5] feat: add func to show all hidden flags --- pkg/flag/options.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pkg/flag/options.go b/pkg/flag/options.go index e00aa4cfa922..38a36a80ae39 100644 --- a/pkg/flag/options.go +++ b/pkg/flag/options.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "reflect" "slices" "strings" "sync" @@ -856,3 +857,37 @@ func (a flagAliases) NormalizeFunc() func(*pflag.FlagSet, string) pflag.Normaliz return pflag.NormalizedName(name) } } + +func HiddenFlags() []string { + var allFlagGroups = []FlagGroup{ + NewGlobalFlagGroup(), + NewCacheFlagGroup(), + NewCleanFlagGroup(), + NewClientFlags(), + NewDBFlagGroup(), + NewImageFlagGroup(), + NewK8sFlagGroup(), + NewLicenseFlagGroup(), + NewMisconfFlagGroup(), + NewModuleFlagGroup(), + NewPackageFlagGroup(), + NewRegistryFlagGroup(), + NewRegoFlagGroup(), + NewReportFlagGroup(), + NewRepoFlagGroup(), + NewScanFlagGroup(), + NewSecretFlagGroup(), + NewServerFlags(), + NewVulnerabilityFlagGroup(), + } + + var hiddenFlags []string + for _, flagGroup := range allFlagGroups { + for _, flag := range flagGroup.Flags() { + if !reflect.ValueOf(flag).IsNil() && flag.Hidden() { + hiddenFlags = append(hiddenFlags, flag.GetConfigName()) + } + } + } + return hiddenFlags +} From 05fd0cbc865a09b7fe93816a89faa3540400f8dc Mon Sep 17 00:00:00 2001 From: DmitriyLewen Date: Wed, 4 Dec 2024 17:31:54 +0600 Subject: [PATCH 2/5] feat: add logic to overwrite flags in viper --- pkg/commands/artifact/run.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/commands/artifact/run.go b/pkg/commands/artifact/run.go index 5018434d10c2..b66010f10e0e 100644 --- a/pkg/commands/artifact/run.go +++ b/pkg/commands/artifact/run.go @@ -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{} + // 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") } From 71500b2f52603549d292f6a9dc2e38d7d72e1150 Mon Sep 17 00:00:00 2001 From: DmitriyLewen Date: Wed, 4 Dec 2024 17:41:26 +0600 Subject: [PATCH 3/5] chore: update comment --- pkg/commands/artifact/run.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/commands/artifact/run.go b/pkg/commands/artifact/run.go index b66010f10e0e..8673b4bc2711 100644 --- a/pkg/commands/artifact/run.go +++ b/pkg/commands/artifact/run.go @@ -362,7 +362,8 @@ func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err err // 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. + // 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 From fd7fba892818104135cb79d3a9fb0718de5478c0 Mon Sep 17 00:00:00 2001 From: DmitriyLewen Date: Wed, 4 Dec 2024 17:55:12 +0600 Subject: [PATCH 4/5] fix: linter error --- pkg/commands/artifact/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/artifact/run.go b/pkg/commands/artifact/run.go index 8673b4bc2711..38523900b683 100644 --- a/pkg/commands/artifact/run.go +++ b/pkg/commands/artifact/run.go @@ -358,7 +358,7 @@ func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err err log.Info("Writing the default config to trivy-default.yaml...") hiddenFlags := flag.HiddenFlags() - allFlags := map[string]any{} + allFlags := make(map[string]any) // 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() { From d8c0edffb68b4fd64a5e3960fd8298e0a34d2724 Mon Sep 17 00:00:00 2001 From: DmitriyLewen Date: Thu, 5 Dec 2024 14:08:12 +0600 Subject: [PATCH 5/5] refactor: use new viper instance --- pkg/commands/artifact/run.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/pkg/commands/artifact/run.go b/pkg/commands/artifact/run.go index 38523900b683..798838259ed2 100644 --- a/pkg/commands/artifact/run.go +++ b/pkg/commands/artifact/run.go @@ -358,9 +358,9 @@ func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err err log.Info("Writing the default config to trivy-default.yaml...") hiddenFlags := flag.HiddenFlags() - allFlags := make(map[string]any) // Viper does not have the ability to remove flags. // So we only save the necessary flags and set these flags after viper.Reset + 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 @@ -368,15 +368,10 @@ func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err err if k == flag.GenerateDefaultConfigFlag.ConfigName || k == flag.ComplianceFlag.ConfigName || slices.Contains(hiddenFlags, k) { continue } - allFlags[k] = viper.Get(k) + v.Set(k, viper.Get(k)) } - viper.Reset() - for k, v := range allFlags { - viper.Set(k, v) - } - - return viper.SafeWriteConfigAs("trivy-default.yaml") + return v.SafeWriteConfigAs("trivy-default.yaml") } r, err := NewRunner(ctx, opts)