Allow for ${ENV_VARIABLES} in paths. Also ~ is now supported, even on windows.
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user