From ce92cb522ed8d2a77ed4075134d855467db8a626 Mon Sep 17 00:00:00 2001 From: Dries De Peuter Date: Sat, 30 Dec 2023 13:26:07 +0100 Subject: [PATCH] fix: Use async directory resolving --- internal/pkg/command/go.go | 15 ++++++++++----- internal/pkg/repo/discover.go | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/internal/pkg/command/go.go b/internal/pkg/command/go.go index ee78336..b184a51 100644 --- a/internal/pkg/command/go.go +++ b/internal/pkg/command/go.go @@ -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: diff --git a/internal/pkg/repo/discover.go b/internal/pkg/repo/discover.go index bb962a1..ce6972a 100644 --- a/internal/pkg/repo/discover.go +++ b/internal/pkg/repo/discover.go @@ -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{}