Skip to content

Commit

Permalink
chore: move flag types into own folder
Browse files Browse the repository at this point in the history
  • Loading branch information
cfabianski committed Dec 18, 2023
1 parent 479a59d commit 31a76bb
Show file tree
Hide file tree
Showing 25 changed files with 403 additions and 213 deletions.
7 changes: 4 additions & 3 deletions internal/classification/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/bearer/bearer/internal/flag"
flagtypes "github.com/bearer/bearer/internal/flag/types"
"github.com/tangzero/inflector"
)

Expand Down Expand Up @@ -141,11 +142,11 @@ func DefaultWithMapping(subjectMappingPath string) DefaultDB {
return defaultDB("", subjectMappingPath)
}

func DefaultWithContext(context flag.Context) DefaultDB {
func DefaultWithContext(context flagtypes.Context) DefaultDB {
return defaultDB(context, "")
}

func defaultDB(context flag.Context, subjectMappingPath string) DefaultDB {
func defaultDB(context flagtypes.Context, subjectMappingPath string) DefaultDB {
dataCategories := defaultDataCategories(context)
categories := map[string]DataCategory{}
for _, category := range dataCategories {
Expand Down Expand Up @@ -189,7 +190,7 @@ func defaultRecipes() []Recipe {
return recipes
}

func defaultDataCategories(context flag.Context) []DataCategory {
func defaultDataCategories(context flagtypes.Context) []DataCategory {
skipHealthContext := true
if context == flag.Health {
skipHealthContext = false
Expand Down
4 changes: 2 additions & 2 deletions internal/classification/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package schema
import (
"regexp"

"github.com/bearer/bearer/internal/flag"
flagtypes "github.com/bearer/bearer/internal/flag/types"

"github.com/bearer/bearer/internal/classification/db"
"github.com/bearer/bearer/internal/report/detectors"
Expand Down Expand Up @@ -40,7 +40,7 @@ type Config struct {
DataTypes []db.DataType
DataTypeClassificationPatterns []db.DataTypeClassificationPattern
KnownPersonObjectPatterns []db.KnownPersonObjectPattern
Context flag.Context
Context flagtypes.Context
}

func New(config Config) *Classifier {
Expand Down
7 changes: 4 additions & 3 deletions internal/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/bearer/bearer/internal/commands/process/orchestrator/work"
"github.com/bearer/bearer/internal/commands/process/settings"
"github.com/bearer/bearer/internal/flag"
flagtypes "github.com/bearer/bearer/internal/flag/types"
"github.com/bearer/bearer/internal/report/basebranchfindings"
reportoutput "github.com/bearer/bearer/internal/report/output"
"github.com/bearer/bearer/internal/report/output/stats"
Expand Down Expand Up @@ -56,7 +57,7 @@ type Runner interface {
// ReportPath returns the filename of the report
ReportPath() string
// Scan gathers the findings
Scan(ctx context.Context, opts flag.Options) ([]files.File, *basebranchfindings.Findings, error)
Scan(ctx context.Context, opts flagtypes.Options) ([]files.File, *basebranchfindings.Findings, error)
// Report a writes a report
Report(files []files.File, baseBranchFindings *basebranchfindings.Findings) (bool, error)
}
Expand Down Expand Up @@ -139,7 +140,7 @@ func (r *runner) CacheUsed() bool {
return r.reuseDetection
}

func (r *runner) Scan(ctx context.Context, opts flag.Options) ([]files.File, *basebranchfindings.Findings, error) {
func (r *runner) Scan(ctx context.Context, opts flagtypes.Options) ([]files.File, *basebranchfindings.Findings, error) {
if r.reuseDetection {
return nil, nil, nil
}
Expand Down Expand Up @@ -260,7 +261,7 @@ func getIgnoredFingerprints(client *api.API, settings settings.Config, gitContex
}

// Run performs artifact scanning
func Run(ctx context.Context, opts flag.Options) (err error) {
func Run(ctx context.Context, opts flagtypes.Options) (err error) {
targetPath, err := file.CanonicalPath(opts.Target)
if err != nil {
return fmt.Errorf("failed to get absolute target: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions internal/commands/process/filelist/filelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/bearer/bearer/internal/commands/process/filelist"
"github.com/bearer/bearer/internal/commands/process/filelist/files"
"github.com/bearer/bearer/internal/commands/process/settings"
"github.com/bearer/bearer/internal/flag"
flagtypes "github.com/bearer/bearer/internal/flag/types"
"github.com/hhatto/gocloc"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestFileList(t *testing.T) {
Input: input{
projectPath: filepath.Join("testdata", "happy_path", "skip"),
config: settings.Config{
Scan: flag.ScanOptions{
Scan: flagtypes.ScanOptions{
SkipPath: []string{"users/admin.go"},
},
Worker: settings.WorkerOptions{
Expand All @@ -94,7 +94,7 @@ func TestFileList(t *testing.T) {
Input: input{
projectPath: filepath.Join("testdata", "happy_path", "skip"),
config: settings.Config{
Scan: flag.ScanOptions{
Scan: flagtypes.ScanOptions{
SkipPath: []string{"users"},
},
Worker: settings.WorkerOptions{
Expand All @@ -110,7 +110,7 @@ func TestFileList(t *testing.T) {
Input: input{
projectPath: filepath.Join("testdata", "happy_path", "skip"),
config: settings.Config{
Scan: flag.ScanOptions{
Scan: flagtypes.ScanOptions{
SkipPath: []string{"users"},
},
Worker: settings.WorkerOptions{
Expand Down
20 changes: 10 additions & 10 deletions internal/commands/process/gitrepository/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"golang.org/x/oauth2"
"gopkg.in/yaml.v3"

"github.com/bearer/bearer/internal/flag"
flagtypes "github.com/bearer/bearer/internal/flag/types"
"github.com/bearer/bearer/internal/git"
)

Expand All @@ -35,7 +35,7 @@ type Context struct {
HasUncommittedChanges bool
}

func NewContext(options *flag.Options) (*Context, error) {
func NewContext(options *flagtypes.Options) (*Context, error) {
if options.IgnoreGit {
return nil, nil
}
Expand Down Expand Up @@ -118,23 +118,23 @@ func NewContext(options *flag.Options) (*Context, error) {
return context, nil
}

func getBranch(options *flag.Options, currentBranch string) string {
func getBranch(options *flagtypes.Options, currentBranch string) string {
if options.Branch != "" {
return options.Branch
}

return currentBranch
}

func getDefaultBranch(options *flag.Options, rootDir string) (string, error) {
func getDefaultBranch(options *flagtypes.Options, rootDir string) (string, error) {
if options.DefaultBranch != "" {
return options.DefaultBranch, nil
}

return git.GetDefaultBranch(rootDir)
}

func getBaseBranch(options *flag.Options, defaultBranch string) (string, error) {
func getBaseBranch(options *flagtypes.Options, defaultBranch string) (string, error) {
if !options.Diff {
return "", nil
}
Expand All @@ -154,7 +154,7 @@ func getBaseBranch(options *flag.Options, defaultBranch string) (string, error)
)
}

func getCommitHash(options *flag.Options, currentCommitHash string) string {
func getCommitHash(options *flagtypes.Options, currentCommitHash string) string {
if options.Commit != "" {
return options.Commit
}
Expand All @@ -163,7 +163,7 @@ func getCommitHash(options *flag.Options, currentCommitHash string) string {
}

func getBaseCommitHash(
options *flag.Options,
options *flagtypes.Options,
rootDir string,
baseBranch string,
currentCommitHash string,
Expand Down Expand Up @@ -210,7 +210,7 @@ func getBaseCommitHash(
)
}

func lookupBaseCommitHashFromGithub(options *flag.Options, baseBranch string, currentCommitHash string) (string, error) {
func lookupBaseCommitHashFromGithub(options *flagtypes.Options, baseBranch string, currentCommitHash string) (string, error) {
if options.GithubToken == "" || options.GithubRepository == "" {
return "", nil
}
Expand Down Expand Up @@ -245,15 +245,15 @@ func lookupBaseCommitHashFromGithub(options *flag.Options, baseBranch string, cu
return *comparison.MergeBaseCommit.SHA, nil
}

func getOriginURL(options *flag.Options, rootDir string) (string, error) {
func getOriginURL(options *flagtypes.Options, rootDir string) (string, error) {
if options.OriginURL != "" {
return options.OriginURL, nil
}

return git.GetOriginURL(rootDir)
}

func newGithubClient(options *flag.Options) (*github.Client, error) {
func newGithubClient(options *flagtypes.Options) (*github.Client, error) {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: options.GithubToken})
httpClient := oauth2.NewClient(context.Background(), tokenSource)
client := github.NewClient(httpClient)
Expand Down
10 changes: 5 additions & 5 deletions internal/commands/process/settings/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"

"github.com/bearer/bearer/internal/flag"
flagtypes "github.com/bearer/bearer/internal/flag/types"
"github.com/bearer/bearer/internal/report/customdetectors"
"github.com/bearer/bearer/internal/util/output"
"github.com/bearer/bearer/internal/util/set"
Expand Down Expand Up @@ -45,7 +45,7 @@ func GetSupportedRuleLanguages() map[string]bool {

func loadRules(
externalRuleDirs []string,
options flag.RuleOptions,
options flagtypes.RuleOptions,
versionMeta *version_check.VersionMeta,
force bool,
) (
Expand Down Expand Up @@ -93,7 +93,7 @@ func loadRules(

func loadRuleDefinitionsFromRemote(
definitions map[string]RuleDefinition,
options flag.RuleOptions,
options flagtypes.RuleOptions,
versionMeta *version_check.VersionMeta,
) {
if options.DisableDefaultRules {
Expand Down Expand Up @@ -310,7 +310,7 @@ func getSanitizers(definition *RuleDefinition) set.Set[string] {
}

func validateRuleOptionIDs(
options flag.RuleOptions,
options flagtypes.RuleOptions,
definitions map[string]RuleDefinition,
builtInDefinitions map[string]RuleDefinition,
) error {
Expand Down Expand Up @@ -341,7 +341,7 @@ func validateRuleOptionIDs(
return nil
}

func getEnabledRules(options flag.RuleOptions, definitions map[string]RuleDefinition, rules map[string]struct{}) map[string]struct{} {
func getEnabledRules(options flagtypes.RuleOptions, definitions map[string]RuleDefinition, rules map[string]struct{}) map[string]struct{} {
enabledRules := make(map[string]struct{})

for ruleId := range rules {
Expand Down
7 changes: 4 additions & 3 deletions internal/commands/process/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/bearer/bearer/api"
"github.com/bearer/bearer/internal/flag"
flagtypes "github.com/bearer/bearer/internal/flag/types"
"github.com/bearer/bearer/internal/util/ignore"
ignoretypes "github.com/bearer/bearer/internal/util/ignore/types"
"github.com/bearer/bearer/internal/util/output"
Expand Down Expand Up @@ -49,8 +50,8 @@ type WorkerOptions struct {
type Config struct {
Client *api.API
Worker WorkerOptions `mapstructure:"worker" json:"worker" yaml:"worker"`
Scan flag.ScanOptions `mapstructure:"scan" json:"scan" yaml:"scan"`
Report flag.ReportOptions `mapstructure:"report" json:"report" yaml:"report"`
Scan flagtypes.ScanOptions `mapstructure:"scan" json:"scan" yaml:"scan"`
Report flagtypes.ReportOptions `mapstructure:"report" json:"report" yaml:"report"`
IgnoredFingerprints map[string]ignoretypes.IgnoredFingerprint `mapstructure:"ignored_fingerprints" json:"ignored_fingerprints" yaml:"ignored_fingerprints"`
StaleIgnoredFingerprintIds []string `mapstructure:"stale_ignored_fingerprint_ids" json:"stale_ignored_fingerprint_ids" yaml:"stale_ignored_fingerprint_ids"`
CloudIgnoresUsed bool `mapstructure:"cloud_ignores_used" json:"cloud_ignores_used" yaml:"cloud_ignores_used"`
Expand Down Expand Up @@ -318,7 +319,7 @@ func defaultWorkerOptions() WorkerOptions {
}
}

func FromOptions(opts flag.Options, versionMeta *version_check.VersionMeta) (Config, error) {
func FromOptions(opts flagtypes.Options, versionMeta *version_check.VersionMeta) (Config, error) {
policies := DefaultPolicies()
workerOptions := defaultWorkerOptions()
result, err := loadRules(
Expand Down
1 change: 1 addition & 0 deletions internal/flag/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package flag
25 changes: 13 additions & 12 deletions internal/flag/general_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/bearer/bearer/api"
flagtypes "github.com/bearer/bearer/internal/flag/types"
pointer "github.com/bearer/bearer/internal/util/pointers"
"github.com/rs/zerolog/log"
)
Expand All @@ -20,7 +21,7 @@ type generalFlagGroup struct{ flagGroupBase }
var GeneralFlagGroup = &generalFlagGroup{flagGroupBase{name: "General"}}

var (
HostFlag = GeneralFlagGroup.add(Flag{
HostFlag = GeneralFlagGroup.add(flagtypes.Flag{
Name: "host",
ConfigName: "host",
Value: "my.bearer.sh",
Expand All @@ -29,7 +30,7 @@ var (
Hide: true,
})

APIKeyFlag = GeneralFlagGroup.add(Flag{
APIKeyFlag = GeneralFlagGroup.add(flagtypes.Flag{
Name: "api-key",
ConfigName: "api-key",
Value: "",
Expand All @@ -38,52 +39,52 @@ var (
Hide: true,
})

ConfigFileFlag = GeneralFlagGroup.add(Flag{
ConfigFileFlag = GeneralFlagGroup.add(flagtypes.Flag{
Name: "config-file",
ConfigName: "config-file",
Value: "bearer.yml",
Usage: "Load configuration from the specified path.",
DisableInConfig: true,
})

DisableVersionCheckFlag = GeneralFlagGroup.add(Flag{
DisableVersionCheckFlag = GeneralFlagGroup.add(flagtypes.Flag{
Name: "disable-version-check",
ConfigName: "disable-version-check",
Value: false,
Usage: "Disable Bearer version checking",
})

NoColorFlag = GeneralFlagGroup.add(Flag{
NoColorFlag = GeneralFlagGroup.add(flagtypes.Flag{
Name: "no-color",
ConfigName: "report.no-color",
Value: false,
Usage: "Disable color in output",
})

IgnoreFileFlag = GeneralFlagGroup.add(Flag{
IgnoreFileFlag = GeneralFlagGroup.add(flagtypes.Flag{
Name: "ignore-file",
ConfigName: "ignore-file",
Value: "bearer.ignore",
Usage: "Load ignore file from the specified path.",
DisableInConfig: true,
})

DebugFlag = GeneralFlagGroup.add(Flag{
DebugFlag = GeneralFlagGroup.add(flagtypes.Flag{
Name: "debug",
ConfigName: "debug",
Value: false,
Usage: "Enable debug logs. Equivalent to --log-level=debug",
DisableInConfig: true,
})

LogLevelFlag = GeneralFlagGroup.add(Flag{
LogLevelFlag = GeneralFlagGroup.add(flagtypes.Flag{
Name: "log-level",
ConfigName: "log-level",
Value: "info",
Usage: "Set log level (error, info, debug, trace)",
})

DebugProfileFlag = GeneralFlagGroup.add(Flag{
DebugProfileFlag = GeneralFlagGroup.add(flagtypes.Flag{
Name: "debug-profile",
ConfigName: "debug-profile",
Value: false,
Expand All @@ -92,7 +93,7 @@ var (
DisableInConfig: true,
})

IgnoreGitFlag = GeneralFlagGroup.add(Flag{
IgnoreGitFlag = GeneralFlagGroup.add(flagtypes.Flag{
Name: "ignore-git",
ConfigName: "ignore-git",
Value: false,
Expand All @@ -115,7 +116,7 @@ type GeneralOptions struct {
IgnoreGit bool `mapstructure:"ignore-git" json:"ignore-git" yaml:"ignore-git"`
}

func (generalFlagGroup) SetOptions(options *Options, args []string) error {
func (generalFlagGroup) SetOptions(options *flagtypes.Options, args []string) error {
var client *api.API
apiKey := getString(APIKeyFlag)
if apiKey != "" {
Expand All @@ -139,7 +140,7 @@ func (generalFlagGroup) SetOptions(options *Options, args []string) error {
logLevel = DebugLogLevel
}

options.GeneralOptions = GeneralOptions{
options.GeneralOptions = flagtypes.GeneralOptions{
Client: client,
ConfigFile: getString(ConfigFileFlag),
DisableVersionCheck: getBool(DisableVersionCheckFlag),
Expand Down
Loading

0 comments on commit 31a76bb

Please sign in to comment.