Skip to content

Commit

Permalink
replace pkg.Scan() with interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rumenvasilev committed Sep 20, 2023
1 parent 67af06d commit 17cdd77
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 57 deletions.
6 changes: 3 additions & 3 deletions cmd/scan/scan-github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -26,7 +26,7 @@ var scanGithubCmd = &cobra.Command{
return err
}
log := log.NewLogger(cfg.Global.Debug, cfg.Global.Silent)
return pkg.Scan(cfg, log)
return scan.New(cfg, log).Do()
},
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/scan/scan-gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -22,7 +22,7 @@ var scanGitlabCmd = &cobra.Command{
return err
}
log := log.NewLogger(cfg.Global.Debug, cfg.Global.Silent)
return pkg.Scan(cfg, log)
return scan.New(cfg, log).Do()
},
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/scan/scan-local-git-repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -21,7 +21,7 @@ var scanLocalGitRepoCmd = &cobra.Command{
return err
}
log := log.NewLogger(cfg.Global.Debug, cfg.Global.Silent)
return pkg.Scan(cfg, log)
return scan.New(cfg, log).Do()
},
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/scan/scan-localpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -22,7 +22,7 @@ var scanLocalPathCmd = &cobra.Command{
return err
}
log := log.NewLogger(cfg.Global.Debug, cfg.Global.Silent)
return pkg.Scan(cfg, log)
return scan.New(cfg, log).Do()
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/updateRules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +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/scan/api"
"github.com/rumenvasilev/rvsecret/internal/pkg/signatures"

"github.com/spf13/cobra"
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/fatih/structs"
"github.com/mitchellh/go-homedir"
"github.com/rumenvasilev/rvsecret/internal/pkg/api"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/api"
"github.com/rumenvasilev/rvsecret/internal/util"
"github.com/rumenvasilev/rvsecret/version"
"github.com/spf13/cobra"
Expand Down
2 changes: 1 addition & 1 deletion internal/core/findings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strings"

"github.com/rumenvasilev/rvsecret/internal/pkg/api"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/api"
"github.com/rumenvasilev/rvsecret/internal/util"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/core/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
_coreapi "github.com/rumenvasilev/rvsecret/internal/core/api"
_github "github.com/rumenvasilev/rvsecret/internal/core/provider/github"
_gitlab "github.com/rumenvasilev/rvsecret/internal/core/provider/gitlab"
"github.com/rumenvasilev/rvsecret/internal/pkg/api"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/api"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
Expand Down
2 changes: 1 addition & 1 deletion internal/core/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
_coreapi "github.com/rumenvasilev/rvsecret/internal/core/api"
"github.com/rumenvasilev/rvsecret/internal/core/provider"
"github.com/rumenvasilev/rvsecret/internal/log"
"github.com/rumenvasilev/rvsecret/internal/pkg/api"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/api"
"github.com/rumenvasilev/rvsecret/internal/stats"
"github.com/rumenvasilev/rvsecret/internal/util"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/core/signatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/rumenvasilev/rvsecret/internal/log"
"github.com/rumenvasilev/rvsecret/internal/matchfile"
"github.com/rumenvasilev/rvsecret/internal/pkg/api"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/api"
"github.com/rumenvasilev/rvsecret/internal/util"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/yaml.v2"
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/save-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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/scan/api"
"github.com/rumenvasilev/rvsecret/internal/util"
"gopkg.in/yaml.v3"
)
Expand Down
31 changes: 0 additions & 31 deletions internal/pkg/scan.go

This file was deleted.

4 changes: 4 additions & 0 deletions internal/pkg/api/api.go → internal/pkg/scan/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ const (
Unknown ScanType = "unknown" // for testing
UpdateSignatures ScanType = "update-signatures"
)

type Scanner interface {
Do() error
}
14 changes: 12 additions & 2 deletions internal/pkg/ghe/ghe.go → internal/pkg/scan/github/ghe.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ghe
package github

import (
"fmt"
Expand All @@ -7,10 +7,18 @@ 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/scan/api"
"github.com/rumenvasilev/rvsecret/internal/webserver"
)

func Scan(cfg *config.Config, log *log.Logger) error {
type GHE struct {
Cfg *config.Config
Log *log.Logger
}

func (g GHE) Do() error {
cfg := g.Cfg
log := g.Log
// create session
sess, err := core.NewSessionWithConfig(cfg, log)
if err != nil {
Expand Down Expand Up @@ -102,3 +110,5 @@ func Scan(cfg *config.Config, log *log.Logger) error {
}
return nil
}

var _ api.Scanner = (*GHE)(nil)
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ 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/scan/api"
"github.com/rumenvasilev/rvsecret/internal/webserver"
)

func Scan(cfg *config.Config, log *log.Logger) error {
type Github struct {
Cfg *config.Config
Log *log.Logger
}

func (g Github) Do() error {
cfg := g.Cfg
log := g.Log
// create session
sess, err := core.NewSessionWithConfig(cfg, log)
if err != nil {
Expand Down Expand Up @@ -86,3 +94,5 @@ func Scan(cfg *config.Config, log *log.Logger) error {
}
return nil
}

var _ api.Scanner = (*Github)(nil)
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ 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/scan/api"
"github.com/rumenvasilev/rvsecret/internal/webserver"
"github.com/spf13/viper"
)

func Scan(cfg *config.Config, log *log.Logger) error {
type Gitlab struct {
Cfg *config.Config
Log *log.Logger
}

func (g Gitlab) Do() error {
cfg := g.Cfg
log := g.Log
// create session
sess, err := core.NewSessionWithConfig(cfg, log)
if err != nil {
Expand Down Expand Up @@ -48,3 +56,5 @@ func Scan(cfg *config.Config, log *log.Logger) error {
}
return nil
}

var _ api.Scanner = (*Gitlab)(nil)
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ 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/scan/api"
"github.com/rumenvasilev/rvsecret/internal/webserver"
)

func Scan(cfg *config.Config, log *log.Logger) error {
type LocalGit struct {
Cfg *config.Config
Log *log.Logger
}

func (l LocalGit) Do() error {
cfg := l.Cfg
log := l.Log
// create session
sess, err := core.NewSessionWithConfig(cfg, log)
if err != nil {
Expand Down Expand Up @@ -47,3 +55,5 @@ func Scan(cfg *config.Config, log *log.Logger) error {
}
return nil
}

var _ api.Scanner = (*LocalGit)(nil)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ 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/scan/api"
"github.com/rumenvasilev/rvsecret/internal/util"
"github.com/rumenvasilev/rvsecret/internal/webserver"
)

func Scan(cfg *config.Config, log *log.Logger) error {
type Localpath struct {
Cfg *config.Config
Log *log.Logger
}

func (l Localpath) Do() error {
cfg := l.Cfg
log := l.Log
// 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 Expand Up @@ -58,3 +66,5 @@ func Scan(cfg *config.Config, log *log.Logger) error {

return nil
}

var _ api.Scanner = (*Localpath)(nil)
36 changes: 36 additions & 0 deletions internal/pkg/scan/scan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package scan

import (
"errors"

"github.com/rumenvasilev/rvsecret/internal/config"
"github.com/rumenvasilev/rvsecret/internal/log"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/api"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/github"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/gitlab"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/localgit"
"github.com/rumenvasilev/rvsecret/internal/pkg/scan/localpath"
)

func New(cfg *config.Config, log *log.Logger) api.Scanner {
switch cfg.Global.ScanType {
case api.LocalPath:
return localpath.Localpath{Cfg: cfg, Log: log}
case api.LocalGit:
return localgit.LocalGit{Cfg: cfg, Log: log}
case api.GithubEnterprise:
return github.GHE{Cfg: cfg, Log: log}
case api.Github:
return github.Github{Cfg: cfg, Log: log}
case api.Gitlab:
return gitlab.Gitlab{Cfg: cfg, Log: log}
default:
return Unsupported{}
}
}

type Unsupported struct{}

func (u Unsupported) Do() error {
return errors.New("this type of scan is unsupported")
}
2 changes: 1 addition & 1 deletion internal/webserver/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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/pkg/scan/api"
"github.com/rumenvasilev/rvsecret/internal/util"
)

Expand Down

0 comments on commit 17cdd77

Please sign in to comment.