-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Baruch Odem <[email protected]>
- Loading branch information
1 parent
18ea1c4
commit 43caab3
Showing
3 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters