Skip to content

Commit

Permalink
chore: update viper (#1443)
Browse files Browse the repository at this point in the history
* chore: update viper dependency

This reverts commit bc82618.

* chore: move flag types into own folder

* tests: improve test coverage for flags

* chore: tidy dependencies
  • Loading branch information
cfabianski authored Dec 18, 2023
1 parent 21dc624 commit 11beff4
Show file tree
Hide file tree
Showing 29 changed files with 757 additions and 651 deletions.
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/yashtewari/glob-intersection v0.2.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/term v0.15.0 // indirect
)

Expand Down Expand Up @@ -91,7 +91,7 @@ require (
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
Expand All @@ -116,11 +116,11 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/pelletier/go-toml v1.9.5
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
github.com/spf13/viper v1.18.1
github.com/subosito/gotenv v1.6.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
431 changes: 17 additions & 414 deletions go.sum

Large diffs are not rendered by default.

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
Loading

0 comments on commit 11beff4

Please sign in to comment.