Skip to content

Commit

Permalink
feat: adds repository plugin (#62)
Browse files Browse the repository at this point in the history
Co-authored-by: Baruch Odem <[email protected]>
  • Loading branch information
cx-monicac and Baruch Odem authored May 5, 2023
1 parent 18ea1c4 commit 43caab3
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var Version = ""

var allPlugins = []plugins.IPlugin{
&plugins.ConfluencePlugin{},
&plugins.RepositoryPlugin{},
}

func initLog() {
Expand Down
102 changes: 102 additions & 0 deletions plugins/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package plugins

import (
"errors"
"os"
"path/filepath"
"sync"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

const argRepository = "path"

type RepositoryPlugin struct {
Plugin
Path string
}

func (p *RepositoryPlugin) IsEnabled() bool {
return p.Enabled
}

func (p *RepositoryPlugin) DefineCommandLineArgs(cmd *cobra.Command) error {
flags := cmd.Flags()
flags.String(argRepository, "", "scan repository folder")
return nil
}

func (p *RepositoryPlugin) Initialize(cmd *cobra.Command) error {
flags := cmd.Flags()
directoryPath, _ := flags.GetString(argRepository)
if directoryPath == "" {
return errors.New("path to repository missing. Plugin initialization failed")
}

p.Path = directoryPath
p.Enabled = true
return nil
}

func (p *RepositoryPlugin) GetItems(items chan Item, errs chan error, wg *sync.WaitGroup) {
defer wg.Done()

wg.Add(1)
go p.getFiles(items, errs, wg)
}

func (p *RepositoryPlugin) getFiles(items chan Item, errs chan error, wg *sync.WaitGroup) {
defer wg.Done()
fileList := make([]string, 0)
err := filepath.Walk(p.Path, func(path string, fInfo os.FileInfo, err error) error {
if err != nil {
log.Fatal().Err(err).Msg("error while walking through the directory")
}
if fInfo.Name() == ".git" && fInfo.IsDir() {
return filepath.SkipDir
}
if fInfo.Size() == 0 {
return nil
}
if !fInfo.IsDir() {
fileList = append(fileList, path)
}
return err
})

if err != nil {
log.Fatal().Err(err).Msg("error while walking through the directory")
}

p.getItems(items, errs, wg, fileList)
}

func (p *RepositoryPlugin) getItems(items chan Item, errs chan error, wg *sync.WaitGroup, fileList []string) {
for _, filePath := range fileList {
wg.Add(1)
go func(filePath string) {
actualFile, err := p.getItem(wg, filePath)
if err != nil {
errs <- err
return
}
items <- *actualFile
}(filePath)
}
}

func (p *RepositoryPlugin) getItem(wg *sync.WaitGroup, filePath string) (*Item, error) {
defer wg.Done()
b, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}

content := &Item{
Content: string(b),
Source: filePath,
ID: filePath,
}
return content, nil
}
19 changes: 13 additions & 6 deletions reporting/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reporting

import (
"fmt"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -41,9 +42,9 @@ func (r *Report) ShowReport() {

func (r *Report) generateResultsReport() {
for source, secrets := range r.Results {
itemLink := getItemId(source)
fmt.Printf("- Item ID: %s\n", itemLink)
fmt.Printf(" - Item Link: %s\n", source)
itemId := getItemId(source)
fmt.Printf("- Item ID: %s\n", itemId)
fmt.Printf(" - Item Full Path: %s\n", source)
fmt.Println(" - Secrets:")
for _, secret := range secrets {
fmt.Printf(" - Type: %s\n", secret.Description)
Expand All @@ -53,7 +54,13 @@ func (r *Report) generateResultsReport() {
}

func getItemId(fullPath string) string {
itemLinkStrings := strings.Split(fullPath, "/")
itemLink := itemLinkStrings[len(itemLinkStrings)-1]
return itemLink
var itemId string
if strings.Contains(fullPath, "/") {
itemLinkStrings := strings.Split(fullPath, "/")
itemId = itemLinkStrings[len(itemLinkStrings)-1]
}
if strings.Contains(fullPath, "\\") {
itemId = filepath.Base(fullPath)
}
return itemId
}

0 comments on commit 43caab3

Please sign in to comment.