Skip to content

Commit

Permalink
feat: allow multiple roots
Browse files Browse the repository at this point in the history
  • Loading branch information
hay-kot committed Aug 2, 2022
1 parent 8a30b1c commit 2c077fa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
13 changes: 8 additions & 5 deletions gofind/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,22 @@ func (gf *GoFind) Run(entry string) ([]Match, error) {

func (gf *GoFind) SearchFor(search SearchEntry) []Match {
var matches []Match
var p = ParsePath(search.Root)

paths := make([]string, len(search.Roots))
for i, root := range search.Roots {
paths[i] = ParsePath(root)
}

finder := Finder{
MaxRecursion: 5,
Ignore: gf.Conf.Ignore,
}

var results = Must(finder.Find(p, search.MatchStr))
var results = Must(finder.Find(paths, search.MatchStr))

if len(results) == 0 {
yal.Warnf("no results found for path %s", search.Root)
yal.Debugf("gf.SearchFor(Root=%s, MatchStr=%s) returned no results", search.Root, search.MatchStr)
yal.Warnf("no results found for path %s", search.Roots)
yal.Debugf("gf.SearchFor(Root=%s, MatchStr=%s) returned no results", search.Roots, search.MatchStr)
return matches
}

Expand All @@ -97,4 +101,3 @@ func (gf *GoFind) SearchFor(search SearchEntry) []Match {

return matches
}

4 changes: 2 additions & 2 deletions gofind/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type Config struct {
}

type SearchEntry struct {
Root string `json:"root"`
MatchStr string `json:"match"`
Roots []string `json:"roots"`
MatchStr string `json:"match"`
}

func ConfigSetup() error {
Expand Down
20 changes: 13 additions & 7 deletions gofind/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (fdr *Finder) DirWalker(channel chan string, root string, pattern string) {

func (fdr *Finder) FindAll(wg *sync.WaitGroup, channel chan string, root string, pattern string) {
defer wg.Done()
defer close(channel)

fdr.DirWalker(channel, root, pattern)

Expand All @@ -73,20 +72,27 @@ func (fdr *Finder) CollectResults(wg *sync.WaitGroup, channel chan string, resul

}

func (fdr *Finder) Find(path string, glob string) ([]string, error) {
func (fdr *Finder) Find(path []string, glob string) ([]string, error) {
var (
matches = []string{}
results = make(chan string)
)

wg := sync.WaitGroup{}
finderWg := sync.WaitGroup{}
finderWg.Add(len(path))

wg.Add(2)
for _, root := range path {
go fdr.FindAll(&finderWg, results, root, glob)
}

collectWg := sync.WaitGroup{}
collectWg.Add(1)

go fdr.FindAll(&wg, results, path, glob)
go fdr.CollectResults(&wg, results, &matches)
go fdr.CollectResults(&collectWg, results, &matches)

wg.Wait()
finderWg.Wait()
close(results)

collectWg.Wait()
return matches, nil
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ func main() {

key := c.Args().Get(0)
entry := gofind.SearchEntry{
Root: c.Args().Get(1),
Roots: []string{c.Args().Get(1)}, //TODO: Let user setup multiple roots!
MatchStr: c.Args().Get(2),
}
cfg.Commands[key] = entry
cfg.Save()

yal.Infof("Key=%s, Root=%s, MatchStr=%s", key, entry.Root, entry.MatchStr)
yal.Infof("Key=%s, Root=%s, MatchStr=%s", key, entry.Roots, entry.MatchStr)
yal.Info("config entry added successfully")
return nil
},
Expand Down Expand Up @@ -179,7 +179,7 @@ func main() {
}

for key, entry := range cfg.Commands {
items = append(items, []string{key, entry.Root, entry.MatchStr})
items = append(items, []string{key, strings.Join(entry.Roots, ", "), entry.MatchStr})
}

str.WriteString(ui.Table(items))
Expand Down

0 comments on commit 2c077fa

Please sign in to comment.