Allow for ${ENV_VARIABLES} in paths. Also ~ is now supported, even on windows.

This commit is contained in:
2024-11-21 11:19:28 +01:00
parent 48b15de4c5
commit 4b0bfa8e4c
4 changed files with 76 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package fsutils
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
@ -46,3 +47,29 @@ func IsDir(path string) bool {
return err == nil && stat.IsDir()
}
func ResolvePath(path string) string {
// We assume that the user doesn't use the involved feature if the path is empty.
if path == "" {
return ""
}
expandedPath := os.ExpandEnv(path)
if strings.HasPrefix(expandedPath, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
expandedPath = filepath.Join(homeDir, expandedPath[1:])
}
path, err := filepath.Abs(expandedPath)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return path
}