Skip to content

Commit

Permalink
refactor: move logic to doc gen
Browse files Browse the repository at this point in the history
  • Loading branch information
cfabianski committed Dec 21, 2023
1 parent 2cef3ac commit 93febc3
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 32 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
41 changes: 35 additions & 6 deletions internal/commands/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <fingerprint>`,
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)

Expand Down Expand Up @@ -135,7 +142,6 @@ $ bearer ignore show <fingerprint>`,
SilenceUsage: false,
}
IgnoreShowFlags.AddFlags(cmd)
IgnoreShowFlags.Bind(cmd) // nolint: errcheck
cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, IgnoreShowFlags.Usages(cmd)))

return cmd
Expand All @@ -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 <fingerprint> --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()
Expand Down Expand Up @@ -233,7 +246,6 @@ $ bearer ignore add <fingerprint> --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
Expand All @@ -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 <fingerprint>`,
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()
Expand Down Expand Up @@ -290,7 +309,6 @@ $ bearer ignore remove <fingerprint>`,
}

flags.AddFlags(cmd)
flags.Bind(cmd) // nolint: errcheck
cmd.SetUsageTemplate(fmt.Sprintf(scanTemplate, flags.Usages(cmd)))

return cmd
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion internal/commands/processing_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -50,7 +57,6 @@ func NewProcessingWorkerCommand() *cobra.Command {
return err
})
flags.AddFlags(cmd)
flags.Bind(cmd) // nolint: errcheck

return cmd
}
8 changes: 7 additions & 1 deletion internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion internal/commands/version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package commands

import (
"fmt"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -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)

Expand Down Expand Up @@ -44,7 +53,6 @@ func NewVersionCommand(version string, commitSHA string) *cobra.Command {
})

flags.AddFlags(cmd)
flags.Bind(cmd) // nolint: errcheck

return cmd
}
6 changes: 0 additions & 6 deletions internal/flag/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions internal/flag/test_helper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flag

import (
"fmt"
"testing"

flagtypes "github.com/bearer/bearer/internal/flag/types"
Expand Down Expand Up @@ -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)

Expand Down
40 changes: 27 additions & 13 deletions scripts/gen-doc-yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 93febc3

Please sign in to comment.