Skip to content

Commit

Permalink
fix: Use async directory resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Dec 30, 2023
1 parent f3dcf8b commit ce92cb5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
15 changes: 10 additions & 5 deletions internal/pkg/command/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ func getGoCmd() *cobra.Command {
if baseDir == "" {
logrus.Fatal("Basedir not set. Run `ph setup` to set it.")
}
paths, err := repo.GetRepoPaths(baseDir)
if err != nil {
logrus.Fatal(fmt.Errorf("failed to get repo paths: %w", err))
}
paths := []string{}
go func(paths *[]string) {
err := repo.GetRepoPathsAsync(baseDir, paths)
if err != nil {
logrus.Fatal(fmt.Errorf("failed to get repo paths: %w", err))
}
}(&paths)

idx, err := fuzzyfinder.Find(
paths,
&paths,
func(i int) string {
return paths[i]
},
fuzzyfinder.WithQuery(strings.Join(args, " ")),
fuzzyfinder.WithSelectOne(),
fuzzyfinder.WithHotReload(),
)
switch err {
case nil:
Expand Down
25 changes: 25 additions & 0 deletions internal/pkg/repo/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ import (
"os"
)

func GetRepoPathsAsync(baseDir string, result *[]string) error {
entries, err := os.ReadDir(baseDir)
if err != nil {
return err
}

for _, entry := range entries {
if !entry.IsDir() {
continue
}

if entry.Name() == ".git" {
*result = append(*result, baseDir)
return nil
}

err := GetRepoPathsAsync(fmt.Sprintf("%s/%s", baseDir, entry.Name()), result)
if err != nil {
return err
}
}

return nil
}

func GetRepoPaths(baseDir string) ([]string, error) {
result := []string{}

Expand Down

0 comments on commit ce92cb5

Please sign in to comment.