From 93febc3b658ae91ddec85f30f0c8ce99c41b764c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Fabianski?= Date: Thu, 21 Dec 2023 10:24:21 +0100 Subject: [PATCH] refactor: move logic to doc gen --- go.mod | 4 +-- go.sum | 4 +-- internal/commands/ignore.go | 41 ++++++++++++++++++++++---- internal/commands/processing_worker.go | 8 ++++- internal/commands/scan.go | 8 ++++- internal/commands/version.go | 10 ++++++- internal/flag/options.go | 6 ---- internal/flag/test_helper.go | 4 +++ scripts/gen-doc-yaml.go | 40 +++++++++++++++++-------- 9 files changed, 93 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index f704ea3cf..8d3c122ed 100644 --- a/go.mod +++ b/go.mod @@ -123,8 +123,8 @@ require ( golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect - gopkg.in/yaml.v2 v2.4.0 + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 ) -replace github.com/spf13/viper => github.com/cfabianski/viper v1.15.1-0.20231220102258-f9bd0ace5732 +replace github.com/spf13/viper => github.com/cfabianski/viper v1.15.1-0.20231221085120-53a0f7864cd6 diff --git a/go.sum b/go.sum index f3c6a024e..6ec9805ae 100644 --- a/go.sum +++ b/go.sum @@ -27,8 +27,8 @@ github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/cfabianski/viper v1.15.1-0.20231220102258-f9bd0ace5732 h1:ZZPf01vbtU+72beKrkFH+oM6pEoMjaJ1QbUTPbuM9R4= -github.com/cfabianski/viper v1.15.1-0.20231220102258-f9bd0ace5732/go.mod h1:ViOjWl6F2V3JRTuQU+T8ioJOUDoYu07nbAfFtiMlXzY= +github.com/cfabianski/viper v1.15.1-0.20231221085120-53a0f7864cd6 h1:HW9hLBTXvVxHrqSgnRsknxVlJfh7IUyxUGv7lmOa4rY= +github.com/cfabianski/viper v1.15.1-0.20231221085120-53a0f7864cd6/go.mod h1:ViOjWl6F2V3JRTuQU+T8ioJOUDoYu07nbAfFtiMlXzY= github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87inij9N3wJY= github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= diff --git a/internal/commands/ignore.go b/internal/commands/ignore.go index 7ba8b0492..9813fc603 100644 --- a/internal/commands/ignore.go +++ b/internal/commands/ignore.go @@ -80,6 +80,13 @@ func newIgnoreShowCommand() *cobra.Command { Short: "Show an ignored fingerprint", Example: `# Show the details of an ignored fingerprint from your ignore file $ bearer ignore show `, + PreRunE: func(cmd *cobra.Command, args []string) error { + if err := IgnoreShowFlags.Bind(cmd); err != nil { + return fmt.Errorf("flag bind error: %w", err) + } + + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { setLogLevel(cmd) @@ -135,7 +142,6 @@ $ bearer ignore show `, SilenceUsage: false, } IgnoreShowFlags.AddFlags(cmd) - IgnoreShowFlags.Bind(cmd) // nolint: errcheck cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, IgnoreShowFlags.Usages(cmd))) return cmd @@ -152,6 +158,13 @@ func newIgnoreAddCommand() *cobra.Command { Short: "Add an ignored fingerprint", Example: `# Add an ignored fingerprint to your ignore file $ bearer ignore add --author Mish --comment "Possible false positive"`, + PreRunE: func(cmd *cobra.Command, args []string) error { + if err := IgnoreShowFlags.Bind(cmd); err != nil { + return fmt.Errorf("flag bind error: %w", err) + } + + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { return cmd.Help() @@ -233,7 +246,6 @@ $ bearer ignore add --author Mish --comment "Possible false positi SilenceUsage: false, } IgnoreShowFlags.AddFlags(cmd) - IgnoreShowFlags.Bind(cmd) // nolint: errcheck cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, IgnoreShowFlags.Usages(cmd))) return cmd @@ -247,6 +259,13 @@ func newIgnoreRemoveCommand() *cobra.Command { Short: "Remove an ignored fingerprint", Example: `# Remove an ignored fingerprint from your ignore file $ bearer ignore remove `, + PreRunE: func(cmd *cobra.Command, args []string) error { + if err := flags.Bind(cmd); err != nil { + return fmt.Errorf("flag bind error: %w", err) + } + + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { return cmd.Help() @@ -290,7 +309,6 @@ $ bearer ignore remove `, } flags.AddFlags(cmd) - flags.Bind(cmd) // nolint: errcheck cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, flags.Usages(cmd))) return cmd @@ -304,6 +322,13 @@ func newIgnorePullCommand() *cobra.Command { Short: "Pull ignored fingerprints from Cloud", Example: `# Pull ignored fingerprints from the Cloud (requires API key) $ bearer ignore pull /path/to/your_project --api-key=XXXXX`, + PreRunE: func(cmd *cobra.Command, args []string) error { + if err := flags.Bind(cmd); err != nil { + return fmt.Errorf("flag bind error: %w", err) + } + + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { setLogLevel(cmd) @@ -378,7 +403,6 @@ $ bearer ignore pull /path/to/your_project --api-key=XXXXX`, SilenceUsage: false, } flags.AddFlags(cmd) - flags.Bind(cmd) // nolint: errcheck cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, flags.Usages(cmd))) return cmd @@ -394,6 +418,13 @@ func newIgnoreMigrateCommand() *cobra.Command { Short: "Migrate ignored fingerprints from bearer.yml to ignore file", Example: `# Migrate existing ignored (excluded) fingerprints from bearer.yml file to ignore file $ bearer ignore migrate`, + PreRunE: func(cmd *cobra.Command, args []string) error { + if err := flags.Bind(cmd); err != nil { + return fmt.Errorf("flag bind error: %w", err) + } + + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { setLogLevel(cmd) @@ -446,8 +477,6 @@ $ bearer ignore migrate`, SilenceErrors: false, SilenceUsage: false, } - flags.AddFlags(cmd) - flags.Bind(cmd) // nolint: errcheck cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, flags.Usages(cmd))) return cmd diff --git a/internal/commands/processing_worker.go b/internal/commands/processing_worker.go index d40874bab..4148395c0 100644 --- a/internal/commands/processing_worker.go +++ b/internal/commands/processing_worker.go @@ -19,6 +19,13 @@ func NewProcessingWorkerCommand() *cobra.Command { cmd := &cobra.Command{ Use: "processing-worker [flags] PATH", Short: "start scan processing server", + PreRunE: func(cmd *cobra.Command, args []string) error { + if err := flags.Bind(cmd); err != nil { + return fmt.Errorf("flag bind error: %w", err) + } + + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { output.Setup(cmd, output.SetupRequest{ LogLevel: viper.GetString(flag.LogLevelFlag.ConfigName), @@ -50,7 +57,6 @@ func NewProcessingWorkerCommand() *cobra.Command { return err }) flags.AddFlags(cmd) - flags.Bind(cmd) // nolint: errcheck return cmd } diff --git a/internal/commands/scan.go b/internal/commands/scan.go index 7a42193b4..a0eff5d7c 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -45,6 +45,13 @@ func NewScanCommand() *cobra.Command { Short: "Scan a directory or file", Example: ` # Scan a local project, including language-specific files $ bearer scan /path/to/your_project`, + PreRunE: func(cmd *cobra.Command, args []string) error { + if err := ScanFlags.Bind(cmd); err != nil { + return fmt.Errorf("flag bind error: %w", err) + } + + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { logLevel := viper.GetString(flag.LogLevelFlag.ConfigName) if viper.GetBool(flag.DebugFlag.ConfigName) { @@ -86,7 +93,6 @@ func NewScanCommand() *cobra.Command { } ScanFlags.AddFlags(cmd) - ScanFlags.Bind(cmd) // nolint:errcheck cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, ScanFlags.Usages(cmd))) return cmd diff --git a/internal/commands/version.go b/internal/commands/version.go index 48b894454..768ba8d97 100644 --- a/internal/commands/version.go +++ b/internal/commands/version.go @@ -1,6 +1,8 @@ package commands import ( + "fmt" + "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -16,6 +18,13 @@ func NewVersionCommand(version string, commitSHA string) *cobra.Command { Use: "version", Short: "Print the version", Args: cobra.NoArgs, + PreRunE: func(cmd *cobra.Command, args []string) error { + if err := flags.Bind(cmd); err != nil { + return fmt.Errorf("flag bind error: %w", err) + } + + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { logLevel := viper.GetString(flag.LogLevelFlag.ConfigName) @@ -44,7 +53,6 @@ func NewVersionCommand(version string, commitSHA string) *cobra.Command { }) flags.AddFlags(cmd) - flags.Bind(cmd) // nolint: errcheck return cmd } diff --git a/internal/flag/options.go b/internal/flag/options.go index 000d3ba9b..42f69fa39 100644 --- a/internal/flag/options.go +++ b/internal/flag/options.go @@ -19,8 +19,6 @@ import ( type Flags []flagtypes.FlagGroup var envPrefix = "bearer" -var boundFlags = set.New[*flagtypes.Flag]() - var ErrInvalidScannerReportCombination = errors.New("invalid scanner argument; privacy report requires sast scanner") type flagGroupBase struct { @@ -50,10 +48,6 @@ func addFlag(cmd *cobra.Command, flag *flagtypes.Flag) { } func BindViper(flag *flagtypes.Flag) error { - if !boundFlags.Add(flag) { - return nil - } - arguments := append( []string{ flag.ConfigName, diff --git a/internal/flag/test_helper.go b/internal/flag/test_helper.go index cb3fd8fc8..0bd11015f 100644 --- a/internal/flag/test_helper.go +++ b/internal/flag/test_helper.go @@ -1,6 +1,7 @@ package flag import ( + "fmt" "testing" flagtypes "github.com/bearer/bearer/internal/flag/types" @@ -34,6 +35,9 @@ func RunFlagTest(testCase TestCase, t *testing.T) { t.Setenv(testCase.env.key, testCase.env.value) } + fmt.Println(testCase.name) + fmt.Println("envVar", viper.AllEnvVar()) + sl := getStringSlice(testCase.flag) assert.Equal(t, testCase.want, sl) diff --git a/scripts/gen-doc-yaml.go b/scripts/gen-doc-yaml.go index 18c1c7844..6b5be06ab 100644 --- a/scripts/gen-doc-yaml.go +++ b/scripts/gen-doc-yaml.go @@ -40,19 +40,33 @@ type cmdDoc struct { Aliases []string `yaml:"aliases,omitempty"` } -var AllFlags = []*flagtypes.Flag{} -var envVars = viper.AllEnvVar() +var ( + AllFlags = []*flagtypes.Flag{} + EnvVars = viper.AllEnvVar() + AllGroups = []flagtypes.FlagGroup{ + flag.GeneralFlagGroup, + flag.IgnoreAddFlagGroup, + flag.IgnoreMigrateFlagGroup, + flag.IgnoreShowFlagGroup, + flag.ReportFlagGroup, + flag.RepositoryFlagGroup, + flag.RuleFlagGroup, + flag.ScanFlagGroup, + flag.WorkerFlagGroup, + } + boundFlags = set.New[*flagtypes.Flag]() +) func main() { - AllFlags = append(AllFlags, flag.GeneralFlagGroup.Flags()...) - AllFlags = append(AllFlags, flag.IgnoreAddFlagGroup.Flags()...) - AllFlags = append(AllFlags, flag.IgnoreMigrateFlagGroup.Flags()...) - AllFlags = append(AllFlags, flag.IgnoreShowFlagGroup.Flags()...) - AllFlags = append(AllFlags, flag.ReportFlagGroup.Flags()...) - AllFlags = append(AllFlags, flag.RepositoryFlagGroup.Flags()...) - AllFlags = append(AllFlags, flag.RuleFlagGroup.Flags()...) - AllFlags = append(AllFlags, flag.ScanFlagGroup.Flags()...) - AllFlags = append(AllFlags, flag.WorkerFlagGroup.Flags()...) + for _, group := range AllGroups { + AllFlags = append(AllFlags, group.Flags()...) + } + + for _, f := range AllFlags { + if boundFlags.Add(f) { + flag.BindViper(f) // nolint: errcheck + } + } dir := "./docs/_data" if _, err := os.Stat(dir); err != nil { @@ -188,9 +202,9 @@ func lookupEnvVariables(flag *pflag.Flag, otherFlag *flagtypes.Flag) []string { var vars []string if otherFlag == nil { - vars = envVars[flag.Name] + vars = EnvVars[flag.Name] } else { - vars = envVars[otherFlag.ConfigName] + vars = EnvVars[otherFlag.ConfigName] } return removeDup(vars)