From 4c1c6ead3d069062aee79b7b70e0629797d2168e Mon Sep 17 00:00:00 2001 From: Dries De Peuter Date: Mon, 19 Feb 2024 22:20:53 +0100 Subject: [PATCH] feat: Allow variable expansion in config paths --- internal/pkg/config/config.go | 5 ++++- internal/pkg/repo/discover.go | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index d9f06d5..02a39ee 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -4,6 +4,7 @@ import ( "os" "path/filepath" + "github.com/nousefreak/projecthelper/internal/pkg/repo" "github.com/sirupsen/logrus" "github.com/spf13/viper" ) @@ -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) } + diff --git a/internal/pkg/repo/discover.go b/internal/pkg/repo/discover.go index 8d4b6b2..90f3780 100644 --- a/internal/pkg/repo/discover.go +++ b/internal/pkg/repo/discover.go @@ -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 } } @@ -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)