Skip to content

Commit

Permalink
feat: Allow variable expansion in config paths
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Feb 19, 2024
1 parent 49dcde5 commit 4c1c6ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
5 changes: 4 additions & 1 deletion internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"path/filepath"

"github.com/nousefreak/projecthelper/internal/pkg/repo"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -44,5 +45,7 @@ func GetBaseDir() string {
if baseDir == "" {
logrus.Fatal("Basedir not set. Run `ph setup` to set it.")
}
return baseDir

return repo.ExpandPath(baseDir)
}

24 changes: 22 additions & 2 deletions internal/pkg/repo/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,32 @@ import (
"github.com/spf13/viper"
)

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)
}

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

return paths
}

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") {
for _, p := range extraDirs() {
out <- p
}
}
Expand Down Expand Up @@ -47,7 +67,7 @@ func GetRepoPathsChan(basedir string, includeExtras bool) <-chan string {

func GetRepoPathsAsync(baseDir string, result *[]string) error {
if len(*result) == 0 {
*result = append(*result, viper.GetStringSlice("extraDirs")...)
*result = append(*result, extraDirs()...)
}

entries, err := os.ReadDir(baseDir)
Expand Down

0 comments on commit 4c1c6ea

Please sign in to comment.