Skip to content

Commit

Permalink
switch cobra cmds to runE
Browse files Browse the repository at this point in the history
  • Loading branch information
rumenvasilev committed Sep 19, 2023
1 parent 3822fbf commit 7f0003e
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 81 deletions.
7 changes: 2 additions & 5 deletions cmd/init-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/scan/scan-github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/scan/scan-gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
10 changes: 6 additions & 4 deletions cmd/scan/scan-local-git-repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
},
}

Expand Down
10 changes: 6 additions & 4 deletions cmd/scan/scan-localpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
},
}

Expand Down
10 changes: 6 additions & 4 deletions cmd/updateRules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ const (
Gitlab ScanType = "gitlab"
LocalGit ScanType = "localGit"
LocalPath ScanType = "localpath"
Unknown ScanType = "unknown" // for testing
UpdateSignatures ScanType = "update-signatures"
)
9 changes: 1 addition & 8 deletions internal/pkg/ghe/ghe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 1 addition & 8 deletions internal/pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 1 addition & 8 deletions internal/pkg/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 1 addition & 8 deletions internal/pkg/localgit/localgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 1 addition & 7 deletions internal/pkg/localpath/localpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/")

Expand Down
15 changes: 8 additions & 7 deletions internal/pkg/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
}
Expand Down
9 changes: 1 addition & 8 deletions internal/pkg/signatures/signatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down

0 comments on commit 7f0003e

Please sign in to comment.