From 49dcde5122f232a517fa19f9e35168930ab214af Mon Sep 17 00:00:00 2001 From: Dries De Peuter Date: Mon, 5 Feb 2024 21:55:26 +0100 Subject: [PATCH] feat: Allow streaming results --- internal/pkg/command/list.go | 25 ++++++++++++++++++++ internal/pkg/command/root.go | 1 + internal/pkg/command/tmux.go | 6 ++--- internal/pkg/repo/discover.go | 43 ++++++++++++++++++++++++++++++++--- 4 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 internal/pkg/command/list.go diff --git a/internal/pkg/command/list.go b/internal/pkg/command/list.go new file mode 100644 index 0000000..c55b0cf --- /dev/null +++ b/internal/pkg/command/list.go @@ -0,0 +1,25 @@ +package command + +import ( + "fmt" + "github.com/nousefreak/projecthelper/internal/pkg/config" + "github.com/nousefreak/projecthelper/internal/pkg/repo" + "github.com/spf13/cobra" +) + +func getListCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "list_fzf", + Short: "List all repositories", + Hidden: true, + Run: func(cmd *cobra.Command, args []string) { + baseDir := config.GetBaseDir() + repos := repo.GetRepoPathsChan(baseDir, true) + for repo := range repos { + fmt.Println(repo) + } + }, + } + return cmd +} + diff --git a/internal/pkg/command/root.go b/internal/pkg/command/root.go index 6cf2c2a..376ead4 100644 --- a/internal/pkg/command/root.go +++ b/internal/pkg/command/root.go @@ -47,6 +47,7 @@ func Execute() { rootCmd.AddCommand(getOrgCmd()) rootCmd.AddCommand(getWDIDCmd()) rootCmd.AddCommand(getTmuxCmd()) + rootCmd.AddCommand(getListCmd()) cobra.OnInitialize(config.InitConfig) cobra.OnInitialize(initLogging) diff --git a/internal/pkg/command/tmux.go b/internal/pkg/command/tmux.go index a81def6..b92ef47 100644 --- a/internal/pkg/command/tmux.go +++ b/internal/pkg/command/tmux.go @@ -14,9 +14,9 @@ import ( func getTmuxCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "tmux [search]", - Aliases: []string{"cd"}, - Short: "tmux session of result", + Use: "tmux [search]", + Hidden: true, + Short: "tmux session of result", Run: func(cmd *cobra.Command, args []string) { path, err := findPath(strings.Join(args, " ")) name := strings.ReplaceAll(filepath.Base(path), ".", "_") diff --git a/internal/pkg/repo/discover.go b/internal/pkg/repo/discover.go index 12e7d30..8d4b6b2 100644 --- a/internal/pkg/repo/discover.go +++ b/internal/pkg/repo/discover.go @@ -8,10 +8,47 @@ import ( "github.com/spf13/viper" ) +func GetRepoPathsChan(basedir string, includeExtras bool) <-chan string { + out := make(chan string) + go func() { + defer close(out) + if includeExtras { + for _, p := range viper.GetStringSlice("extraDirs") { + out <- p + } + } + + entries, err := os.ReadDir(basedir) + if err != nil { + return + } + subdirs := []string{} + for _, entry := range entries { + if !entry.IsDir() { + continue + } + if entry.Name() == ".git" { + out <- basedir + return + } + subdirs = append(subdirs, entry.Name()) + } + + for _, subdir := range subdirs { + subchan := GetRepoPathsChan(fmt.Sprintf("%s/%s", basedir, subdir), false) + for p := range subchan { + out <- p + } + } + }() + + return out +} + func GetRepoPathsAsync(baseDir string, result *[]string) error { - if len(*result) == 0 { - *result = append(*result, viper.GetStringSlice("extraDirs")...) - } + if len(*result) == 0 { + *result = append(*result, viper.GetStringSlice("extraDirs")...) + } entries, err := os.ReadDir(baseDir) if err != nil {