Skip to content

Commit

Permalink
refactor: simplify flag handling
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe committed Nov 20, 2023
1 parent 629757b commit 2a2d48e
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 705 deletions.
63 changes: 2 additions & 61 deletions internal/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ package commands
import (
"fmt"

"github.com/bearer/bearer/cmd/bearer/build"
"github.com/bearer/bearer/internal/commands/artifact"
"github.com/bearer/bearer/internal/flag"
"github.com/spf13/cobra"
)

// VersionInfo holds the bearer version
type VersionInfo struct {
Version string `json:",omitempty" yaml:",omitempty"`
}
"github.com/bearer/bearer/cmd/bearer/build"
)

// NewApp is the factory method to return CLI
func NewApp(version string, commitSHA string) *cobra.Command {
Expand Down Expand Up @@ -76,56 +70,3 @@ Learn More:

return cmd
}

func NewConfigCommand() *cobra.Command {
scanFlags := &flag.ScanFlagGroup{
SkipPathFlag: &flag.SkipPathFlag,
DisableDomainResolutionFlag: &flag.DisableDomainResolutionFlag,
DomainResolutionTimeoutFlag: &flag.DomainResolutionTimeoutFlag,
InternalDomainsFlag: &flag.InternalDomainsFlag,
ContextFlag: &flag.ContextFlag,
}

configFlags := &flag.Flags{
ScanFlagGroup: scanFlags,
}

cmd := &cobra.Command{
Use: "config [flags] DIR",
Aliases: []string{"conf"},
Short: "Scan config files for misconfigurations",
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := configFlags.Bind(cmd); err != nil {
return fmt.Errorf("flag bind error: %w", err)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := configFlags.Bind(cmd); err != nil {
return fmt.Errorf("flag bind error: %w", err)
}
options, err := configFlags.ToOptions(args)
if err != nil {
return fmt.Errorf("flag error: %w", err)
}

return artifact.Run(cmd.Context(), options)
},
SilenceErrors: true,
SilenceUsage: true,
}
cmd.SetFlagErrorFunc(flagErrorFunc)
configFlags.AddFlags(cmd)
cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, configFlags.Usages(cmd)))

return cmd
}

// show help on using the command when an invalid flag is encountered
func flagErrorFunc(command *cobra.Command, err error) error {
if err := command.Help(); err != nil {
return err
}
command.Println() // add empty line after list of flags
return err
}
61 changes: 30 additions & 31 deletions internal/commands/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ Examples:
}

func newIgnoreShowCommand() *cobra.Command {
var IgnoreShowFlags = &flag.Flags{
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
IgnoreShowFlagGroup: flag.NewIgnoreShowFlagGroup(),
var IgnoreShowFlags = flag.Flags{
flag.GeneralFlagGroup,
flag.IgnoreShowFlagGroup,
}
cmd := &cobra.Command{
Use: "show <fingerprint>",
Expand Down Expand Up @@ -135,17 +135,18 @@ $ bearer ignore show <fingerprint>`,
}

func newIgnoreAddCommand() *cobra.Command {
var IgnoreAddFlags = &flag.Flags{
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
IgnoreAddFlagGroup: flag.NewIgnoreAddFlagGroup(),
IgnoreShowFlags := flag.Flags{
flag.IgnoreAddFlagGroup,
flag.GeneralFlagGroup,
}

cmd := &cobra.Command{
Use: "add <fingerprint>",
Short: "Add an ignored fingerprint",
Example: `# Add an ignored fingerprint to your ignore file
$ bearer ignore add <fingerprint> --author Mish --comment "Possible false positive"`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := IgnoreAddFlags.Bind(cmd); err != nil {
if err := IgnoreShowFlags.Bind(cmd); err != nil {
return fmt.Errorf("flag bind error: %w", err)
}

Expand All @@ -155,7 +156,7 @@ $ bearer ignore add <fingerprint> --author Mish --comment "Possible false positi

setLogLevel(cmd)

options, err := IgnoreAddFlags.ToOptions(args)
options, err := IgnoreShowFlags.ToOptions(args)
if err != nil {
return fmt.Errorf("flag error: %s", err)
}
Expand Down Expand Up @@ -228,23 +229,22 @@ $ bearer ignore add <fingerprint> --author Mish --comment "Possible false positi
SilenceErrors: false,
SilenceUsage: false,
}
IgnoreAddFlags.AddFlags(cmd)
cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, IgnoreAddFlags.Usages(cmd)))
IgnoreShowFlags.AddFlags(cmd)
cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, IgnoreShowFlags.Usages(cmd)))

return cmd
}

func newIgnoreRemoveCommand() *cobra.Command {
var IgnoreRemoveFlags = &flag.Flags{
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
}
flags := flag.Flags{flag.GeneralFlagGroup}

cmd := &cobra.Command{
Use: "remove <fingerprint>",
Short: "Remove an ignored fingerprint",
Example: `# Remove an ignored fingerprint from your ignore file
$ bearer ignore remove <fingerprint>`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := IgnoreRemoveFlags.Bind(cmd); err != nil {
if err := flags.Bind(cmd); err != nil {
return fmt.Errorf("flag bind error: %w", err)
}

Expand All @@ -254,7 +254,7 @@ $ bearer ignore remove <fingerprint>`,

setLogLevel(cmd)

options, err := IgnoreRemoveFlags.ToOptions(args)
options, err := flags.ToOptions(args)
if err != nil {
return fmt.Errorf("flag error: %s", err)
}
Expand Down Expand Up @@ -288,29 +288,28 @@ $ bearer ignore remove <fingerprint>`,
SilenceErrors: false,
SilenceUsage: false,
}
IgnoreRemoveFlags.AddFlags(cmd)
cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, IgnoreRemoveFlags.Usages(cmd)))
flags.AddFlags(cmd)
cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, flags.Usages(cmd)))

return cmd
}

func newIgnorePullCommand() *cobra.Command {
var IgnorePullFlags = &flag.Flags{
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
}
var flags = flag.Flags{flag.GeneralFlagGroup}

cmd := &cobra.Command{
Use: "pull <path>",
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`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := IgnorePullFlags.Bind(cmd); err != nil {
if err := flags.Bind(cmd); err != nil {
return fmt.Errorf("flag bind error: %w", err)
}

setLogLevel(cmd)

options, err := IgnorePullFlags.ToOptions(args)
options, err := flags.ToOptions(args)
if err != nil {
return fmt.Errorf("flag error: %s", err)
}
Expand Down Expand Up @@ -381,30 +380,30 @@ $ bearer ignore pull /path/to/your_project --api-key=XXXXX`,
SilenceErrors: false,
SilenceUsage: false,
}
IgnorePullFlags.AddFlags(cmd)
cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, IgnorePullFlags.Usages(cmd)))
flags.AddFlags(cmd)
cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, flags.Usages(cmd)))

return cmd
}

func newIgnoreMigrateCommand() *cobra.Command {
IgnoreMigrateFlags := &flag.Flags{
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
IgnoreMigrateFlagGroup: flag.NewIgnoreMigrateFlagGroup(),
flags := flag.Flags{
flag.GeneralFlagGroup,
flag.IgnoreMigrateFlagGroup,
}
cmd := &cobra.Command{
Use: "migrate",
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`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := IgnoreMigrateFlags.Bind(cmd); err != nil {
if err := flags.Bind(cmd); err != nil {
return fmt.Errorf("flag bind error: %w", err)
}

setLogLevel(cmd)

options, err := IgnoreMigrateFlags.ToOptions(args)
options, err := flags.ToOptions(args)
if err != nil {
return fmt.Errorf("flag error: %s", err)
}
Expand Down Expand Up @@ -453,8 +452,8 @@ $ bearer ignore migrate`,
SilenceErrors: false,
SilenceUsage: false,
}
IgnoreMigrateFlags.AddFlags(cmd)
cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, IgnoreMigrateFlags.Usages(cmd)))
flags.AddFlags(cmd)
cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, flags.Usages(cmd)))

return cmd
}
Expand Down
24 changes: 12 additions & 12 deletions internal/commands/processing_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import (
)

func NewProcessingWorkerCommand() *cobra.Command {
flags := &flag.Flags{
ProcessFlagGroup: flag.NewProcessGroup(),
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
ScanFlagGroup: flag.NewScanFlagGroup(),
}
flags := flag.Flags{flag.WorkerFlagGroup}

cmd := &cobra.Command{
Use: "processing-worker [flags] PATH",
Expand All @@ -38,21 +34,25 @@ func NewProcessingWorkerCommand() *cobra.Command {
debugprofile.Start()
}

processOptions, err := flags.ProcessFlagGroup.ToOptions()
options, err := flags.ToOptions(args)
if err != nil {
return fmt.Errorf("options binding error: %w", err)
return fmt.Errorf("flag error: %s", err)
}

log.Debug().Msgf("running scan worker on port `%s`", processOptions.Port)

err = worker.Start(processOptions.ParentProcessID, processOptions.Port)
return err
log.Debug().Msgf("running scan worker on port `%s`", options.WorkerOptions.Port)
return worker.Start(options.WorkerOptions.ParentProcessID, options.WorkerOptions.Port)
},
Hidden: true,
SilenceErrors: true,
SilenceUsage: true,
}
cmd.SetFlagErrorFunc(flagErrorFunc)
cmd.SetFlagErrorFunc(func(command *cobra.Command, err error) error {
if err := command.Help(); err != nil {
return err
}
command.Println() // add empty line after list of flags
return err
})
flags.AddFlags(cmd)

return cmd
Expand Down
10 changes: 5 additions & 5 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "he
`
)

var ScanFlags = &flag.Flags{
ScanFlagGroup: flag.NewScanFlagGroup(),
RuleFlagGroup: flag.NewRuleFlagGroup(),
ReportFlagGroup: flag.NewReportFlagGroup(),
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
var ScanFlags = flag.Flags{
flag.ScanFlagGroup,
flag.ReportFlagGroup,
flag.RuleFlagGroup,
flag.GeneralFlagGroup,
}

func NewScanCommand() *cobra.Command {
Expand Down
8 changes: 3 additions & 5 deletions internal/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ import (
)

func NewVersionCommand(version string, commitSHA string) *cobra.Command {
var VersionFlags = &flag.Flags{
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
}
var flags = flag.Flags{flag.GeneralFlagGroup}
cmd := &cobra.Command{
Use: "version",
Short: "Print the version",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := VersionFlags.Bind(cmd); err != nil {
if err := flags.Bind(cmd); err != nil {
return fmt.Errorf("flag bind error: %w", err)
}

Expand Down Expand Up @@ -49,6 +47,6 @@ func NewVersionCommand(version string, commitSHA string) *cobra.Command {
cmd.SetUsageFunc(func(cmd *cobra.Command) error {
return nil
})
VersionFlags.AddFlags(cmd)
flags.AddFlags(cmd)
return cmd
}
Loading

0 comments on commit 2a2d48e

Please sign in to comment.