diff --git a/cmd/init-config.go b/cmd/init-config.go index c7a61da..8b99bba 100644 --- a/cmd/init-config.go +++ b/cmd/init-config.go @@ -10,12 +10,9 @@ import ( var initConfigCmd = &cobra.Command{ Use: "init-config", Short: "Creates configuration file", - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { log := log.NewLogger(viper.GetBool("debug"), viper.GetBool("silent")) - err := pkg.SaveConfig(log) - if err != nil { - log.Fatal("%v", err) - } + return pkg.SaveConfig(log) }, } diff --git a/cmd/scan/scan-github.go b/cmd/scan/scan-github.go index 62d3e60..2b9d17b 100644 --- a/cmd/scan/scan-github.go +++ b/cmd/scan/scan-github.go @@ -16,17 +16,17 @@ var scanGithubCmd = &cobra.Command{ Use: "github", Aliases: []string{"gh"}, Short: "Scan one or more github.com orgs or users for secrets.", - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { scanType := api.Github if cmd.Flags().Changed("enterprise") { scanType = api.GithubEnterprise } - cfg, _ := config.Load(scanType) - log := log.NewLogger(cfg.Global.Debug, cfg.Global.Silent) - err := pkg.Scan(scanType, log) + cfg, err := config.Load(scanType) if err != nil { - log.Fatal("%v", err) + return err } + log := log.NewLogger(cfg.Global.Debug, cfg.Global.Silent) + return pkg.Scan(cfg, log) }, } diff --git a/cmd/scan/scan-gitlab.go b/cmd/scan/scan-gitlab.go index 0b4e70f..8426ad2 100644 --- a/cmd/scan/scan-gitlab.go +++ b/cmd/scan/scan-gitlab.go @@ -16,12 +16,13 @@ var scanGitlabCmd = &cobra.Command{ Use: "gitlab", Aliases: []string{"gl"}, Short: "Scan one or more gitlab groups or users for secrets", - Run: func(cmd *cobra.Command, args []string) { - log := log.NewLogger(viper.GetBool("debug"), viper.GetBool("silent")) - err := pkg.Scan(api.Gitlab, log) + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load(api.Gitlab) if err != nil { - log.Fatal("%v", err) + return err } + log := log.NewLogger(cfg.Global.Debug, cfg.Global.Silent) + return pkg.Scan(cfg, log) }, } diff --git a/cmd/scan/scan-local-git-repo.go b/cmd/scan/scan-local-git-repo.go index ded2f60..3f73ed4 100644 --- a/cmd/scan/scan-local-git-repo.go +++ b/cmd/scan/scan-local-git-repo.go @@ -3,6 +3,7 @@ package scan import ( + "github.com/rumenvasilev/rvsecret/internal/config" "github.com/rumenvasilev/rvsecret/internal/log" "github.com/rumenvasilev/rvsecret/internal/pkg" "github.com/rumenvasilev/rvsecret/internal/pkg/api" @@ -14,12 +15,13 @@ import ( var scanLocalGitRepoCmd = &cobra.Command{ Use: "local-git-repo", Short: "Scan a git repo on a local machine", - Run: func(cmd *cobra.Command, args []string) { - log := log.NewLogger(viper.GetBool("debug"), viper.GetBool("silent")) - err := pkg.Scan(api.LocalGit, log) + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load(api.LocalGit) if err != nil { - log.Fatal("%v", err) + return err } + log := log.NewLogger(cfg.Global.Debug, cfg.Global.Silent) + return pkg.Scan(cfg, log) }, } diff --git a/cmd/scan/scan-localpath.go b/cmd/scan/scan-localpath.go index e598b5a..0ab25e2 100644 --- a/cmd/scan/scan-localpath.go +++ b/cmd/scan/scan-localpath.go @@ -3,6 +3,7 @@ package scan import ( + "github.com/rumenvasilev/rvsecret/internal/config" "github.com/rumenvasilev/rvsecret/internal/log" "github.com/rumenvasilev/rvsecret/internal/pkg" "github.com/rumenvasilev/rvsecret/internal/pkg/api" @@ -15,12 +16,13 @@ var scanLocalPathCmd = &cobra.Command{ TraverseChildren: true, Use: "localpath", Short: "Scan local files and directories", - Run: func(cmd *cobra.Command, args []string) { - log := log.NewLogger(viper.GetBool("debug"), viper.GetBool("silent")) - err := pkg.Scan(api.LocalPath, log) + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load(api.LocalPath) if err != nil { - log.Fatal("%v", err) + return err } + log := log.NewLogger(cfg.Global.Debug, cfg.Global.Silent) + return pkg.Scan(cfg, log) }, } diff --git a/cmd/updateRules.go b/cmd/updateRules.go index dd40a5e..e96658d 100644 --- a/cmd/updateRules.go +++ b/cmd/updateRules.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/rumenvasilev/rvsecret/internal/config" "github.com/rumenvasilev/rvsecret/internal/log" + "github.com/rumenvasilev/rvsecret/internal/pkg/api" "github.com/rumenvasilev/rvsecret/internal/pkg/signatures" "github.com/spf13/cobra" @@ -14,12 +15,13 @@ var updateSignaturesCmd = &cobra.Command{ Use: "updateSignatures", Short: "Update the signatures to the latest version available", Long: "Update the signatures to the latest version available", - Run: func(cmd *cobra.Command, args []string) { - log := log.NewLogger(viper.GetBool("debug"), viper.GetBool("silent")) - err := signatures.Update(log) + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load(api.UpdateSignatures) if err != nil { - log.Fatal("%v", err) + return err } + log := log.NewLogger(cfg.Global.Debug, cfg.Global.Silent) + return signatures.Update(cfg, log) }, } diff --git a/go.mod b/go.mod index cbc4ad9..32a93b9 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,6 @@ require ( github.com/otiai10/copy v1.12.0 github.com/smartystreets/goconvey v1.8.1 github.com/spf13/cobra v1.7.0 - github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.16.0 github.com/whilp/git-urls v1.0.0 github.com/xanzy/go-gitlab v0.90.0 @@ -61,6 +60,7 @@ require ( github.com/spf13/afero v1.9.5 // indirect github.com/spf13/cast v1.5.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/src-d/gcfg v1.4.0 // indirect github.com/subosito/gotenv v1.4.2 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect diff --git a/internal/pkg/api/api.go b/internal/pkg/api/api.go index 300721b..11f8cf9 100644 --- a/internal/pkg/api/api.go +++ b/internal/pkg/api/api.go @@ -8,5 +8,6 @@ const ( Gitlab ScanType = "gitlab" LocalGit ScanType = "localGit" LocalPath ScanType = "localpath" + Unknown ScanType = "unknown" // for testing UpdateSignatures ScanType = "update-signatures" ) diff --git a/internal/pkg/ghe/ghe.go b/internal/pkg/ghe/ghe.go index a8725a3..21b50c9 100644 --- a/internal/pkg/ghe/ghe.go +++ b/internal/pkg/ghe/ghe.go @@ -7,17 +7,10 @@ import ( "github.com/rumenvasilev/rvsecret/internal/config" "github.com/rumenvasilev/rvsecret/internal/core" "github.com/rumenvasilev/rvsecret/internal/log" - "github.com/rumenvasilev/rvsecret/internal/pkg/api" "github.com/rumenvasilev/rvsecret/internal/webserver" ) -func Scan(log *log.Logger) error { - // load config - cfg, err := config.Load(api.GithubEnterprise) - if err != nil { - return err - } - +func Scan(cfg *config.Config, log *log.Logger) error { // create session sess, err := core.NewSessionWithConfig(cfg, log) if err != nil { diff --git a/internal/pkg/github/github.go b/internal/pkg/github/github.go index 3dc98d3..776488b 100644 --- a/internal/pkg/github/github.go +++ b/internal/pkg/github/github.go @@ -7,17 +7,10 @@ import ( "github.com/rumenvasilev/rvsecret/internal/config" "github.com/rumenvasilev/rvsecret/internal/core" "github.com/rumenvasilev/rvsecret/internal/log" - "github.com/rumenvasilev/rvsecret/internal/pkg/api" "github.com/rumenvasilev/rvsecret/internal/webserver" ) -func Scan(log *log.Logger) error { - // load config - cfg, err := config.Load(api.Github) - if err != nil { - return err - } - +func Scan(cfg *config.Config, log *log.Logger) error { // create session sess, err := core.NewSessionWithConfig(cfg, log) if err != nil { diff --git a/internal/pkg/gitlab/gitlab.go b/internal/pkg/gitlab/gitlab.go index c6e1fb9..5647a85 100644 --- a/internal/pkg/gitlab/gitlab.go +++ b/internal/pkg/gitlab/gitlab.go @@ -6,18 +6,11 @@ import ( "github.com/rumenvasilev/rvsecret/internal/config" "github.com/rumenvasilev/rvsecret/internal/core" "github.com/rumenvasilev/rvsecret/internal/log" - "github.com/rumenvasilev/rvsecret/internal/pkg/api" "github.com/rumenvasilev/rvsecret/internal/webserver" "github.com/spf13/viper" ) -func Scan(log *log.Logger) error { - // load config - cfg, err := config.Load(api.Gitlab) - if err != nil { - return err - } - +func Scan(cfg *config.Config, log *log.Logger) error { // create session sess, err := core.NewSessionWithConfig(cfg, log) if err != nil { diff --git a/internal/pkg/localgit/localgit.go b/internal/pkg/localgit/localgit.go index d6cd4c3..df5cf07 100644 --- a/internal/pkg/localgit/localgit.go +++ b/internal/pkg/localgit/localgit.go @@ -6,17 +6,10 @@ import ( "github.com/rumenvasilev/rvsecret/internal/config" "github.com/rumenvasilev/rvsecret/internal/core" "github.com/rumenvasilev/rvsecret/internal/log" - "github.com/rumenvasilev/rvsecret/internal/pkg/api" "github.com/rumenvasilev/rvsecret/internal/webserver" ) -func Scan(log *log.Logger) error { - // load config - cfg, err := config.Load(api.LocalGit) - if err != nil { - return err - } - +func Scan(cfg *config.Config, log *log.Logger) error { // create session sess, err := core.NewSessionWithConfig(cfg, log) if err != nil { diff --git a/internal/pkg/localpath/localpath.go b/internal/pkg/localpath/localpath.go index 4af0f61..1506acb 100644 --- a/internal/pkg/localpath/localpath.go +++ b/internal/pkg/localpath/localpath.go @@ -6,17 +6,11 @@ import ( "github.com/rumenvasilev/rvsecret/internal/config" "github.com/rumenvasilev/rvsecret/internal/core" "github.com/rumenvasilev/rvsecret/internal/log" - "github.com/rumenvasilev/rvsecret/internal/pkg/api" "github.com/rumenvasilev/rvsecret/internal/util" "github.com/rumenvasilev/rvsecret/internal/webserver" ) -func Scan(log *log.Logger) error { - // load config - cfg, err := config.Load(api.LocalPath) - if err != nil { - return err - } +func Scan(cfg *config.Config, log *log.Logger) error { // exclude the .git directory from local scans as it is not handled properly here cfg.Global.SkippablePath = util.AppendIfMissing(cfg.Global.SkippablePath, ".git/") diff --git a/internal/pkg/scan.go b/internal/pkg/scan.go index 4809e1b..cdf1368 100644 --- a/internal/pkg/scan.go +++ b/internal/pkg/scan.go @@ -3,6 +3,7 @@ package pkg import ( "fmt" + "github.com/rumenvasilev/rvsecret/internal/config" "github.com/rumenvasilev/rvsecret/internal/log" "github.com/rumenvasilev/rvsecret/internal/pkg/api" "github.com/rumenvasilev/rvsecret/internal/pkg/ghe" @@ -12,18 +13,18 @@ import ( "github.com/rumenvasilev/rvsecret/internal/pkg/localpath" ) -func Scan(scanType api.ScanType, log *log.Logger) error { - switch scanType { +func Scan(cfg *config.Config, log *log.Logger) error { + switch cfg.Global.ScanType { case api.LocalPath: - return localpath.Scan(log) + return localpath.Scan(cfg, log) case api.LocalGit: - return localgit.Scan(log) + return localgit.Scan(cfg, log) case api.Github: - return github.Scan(log) + return github.Scan(cfg, log) case api.GithubEnterprise: - return ghe.Scan(log) + return ghe.Scan(cfg, log) case api.Gitlab: - return gitlab.Scan(log) + return gitlab.Scan(cfg, log) default: return fmt.Errorf("unsupported scan type") } diff --git a/internal/pkg/signatures/signatures.go b/internal/pkg/signatures/signatures.go index 69a8ecf..d0a9efd 100644 --- a/internal/pkg/signatures/signatures.go +++ b/internal/pkg/signatures/signatures.go @@ -13,7 +13,6 @@ import ( "github.com/rumenvasilev/rvsecret/internal/config" "github.com/rumenvasilev/rvsecret/internal/core" "github.com/rumenvasilev/rvsecret/internal/log" - "github.com/rumenvasilev/rvsecret/internal/pkg/api" "github.com/rumenvasilev/rvsecret/internal/util" whilp "github.com/whilp/git-urls" ) @@ -25,15 +24,9 @@ import ( // `current` => main branch latest commit // `semver` => specific version -func Update(log *log.Logger) error { +func Update(cfg *config.Config, log *log.Logger) error { var dir string - // load config - cfg, err := config.Load(api.UpdateSignatures) - if err != nil { - return err - } - // create session sess, err := core.NewSessionWithConfig(cfg, log) if err != nil {