Skip to content

Commit

Permalink
feat: Allow streaming results
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Feb 5, 2024
1 parent 9471226 commit 49dcde5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
25 changes: 25 additions & 0 deletions internal/pkg/command/list.go
Original file line number Diff line number Diff line change
@@ -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
}

1 change: 1 addition & 0 deletions internal/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/command/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), ".", "_")
Expand Down
43 changes: 40 additions & 3 deletions internal/pkg/repo/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 49dcde5

Please sign in to comment.