Skip to content

Commit

Permalink
fix: Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Mar 12, 2024
1 parent aa4e2a0 commit bb605ea
Showing 1 changed file with 17 additions and 42 deletions.
59 changes: 17 additions & 42 deletions internal/pkg/repo/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ import (
)

func ExpandPath(path string) string {
if strings.HasPrefix(path, "~") {
home, err := os.UserHomeDir()
if err != nil {
return path
}
return home + path[1:]
}
return os.ExpandEnv(path)
if strings.HasPrefix(path, "~") {
home, err := os.UserHomeDir()
if err != nil {
return path
}
return home + path[1:]
}
return os.ExpandEnv(path)
}

func extraDirs() []string {
paths := viper.GetStringSlice("extraDirs")
for i, path := range paths {
paths[i] = ExpandPath(path)
}
paths := viper.GetStringSlice("extraDirs")
for i, path := range paths {
paths[i] = ExpandPath(path)
}

return paths
return paths
}

func GetRepoPathsChan(basedir string, includeExtras bool) <-chan string {
basedir = ExpandPath(basedir)
out := make(chan string)
go func() {
defer close(out)
Expand Down Expand Up @@ -66,36 +67,10 @@ func GetRepoPathsChan(basedir string, includeExtras bool) <-chan string {
}

func GetRepoPathsAsync(baseDir string, result *[]string) error {
if len(*result) == 0 {
*result = append(*result, extraDirs()...)
ch := GetRepoPathsChan(baseDir, true)
for p := range ch {
*result = append(*result, p)
}

entries, err := os.ReadDir(baseDir)
if err != nil {
return err
}

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

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

subdirs = append(subdirs, entry.Name())
}

for _, subdir := range subdirs {
err := GetRepoPathsAsync(fmt.Sprintf("%s/%s", baseDir, subdir), result)
if err != nil {
return err
}
}

return nil
}

Expand Down

0 comments on commit bb605ea

Please sign in to comment.