-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathacfmgr_unix.go
38 lines (33 loc) · 1.27 KB
/
acfmgr_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// +build darwin linux
package acfmgr
import (
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
)
// expandPathO takes the path as a string and attempts to massage
// it into something the ioutils can use. Should support situations
// where people try to put stuff like ~ and $HOME in the path
func expandPathO(path string, usr *user.User) (expandedPath string, err error) {
dir := usr.HomeDir
prefixTildeNix := "~/"
prefixTildeWin := "~\\"
// fmt.Printf("checking path `%s` for prefix `%s`\n", path, prefix)
if strings.HasPrefix(path, prefixTildeNix) || strings.HasPrefix(path, prefixTildeWin) {
// Use strings.HasPrefix so we don't match paths like
// "/something/~/something/"
expandedPath = filepath.Join(dir, path[2:])
} else {
expandedPath = path
}
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
if strings.HasPrefix(expandedPath, "$") {
// need to extract and expand env var
expandedPath = os.ExpandEnv(expandedPath)
}
}
expandedPath, err = filepath.Abs(expandedPath)
return expandedPath, err
}