/
path.go
48 lines (42 loc) · 881 Bytes
/
path.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
37
38
39
40
41
42
43
44
45
46
47
48
package process
import (
"os/user"
"path/filepath"
)
func pathSplit(path string) []string {
r := make([]string, 0)
curPath := path
for {
dir, file := filepath.Split(curPath)
if len(file) > 0 {
r = append(r, file)
}
if len(dir) <= 0 {
break
}
curPath = dir[0 : len(dir)-1]
}
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return r
}
// PathExpand replaces the ~ with user home directory
func PathExpand(path string) (string, error) {
pathList := pathSplit(path)
if len(pathList) > 0 && len(pathList[0]) > 0 && pathList[0][0] == '~' {
var usr *user.User
var err error
if pathList[0] == "~" {
usr, err = user.Current()
} else {
usr, err = user.Lookup(pathList[0][1:])
}
if err != nil {
return "", err
}
pathList[0] = usr.HomeDir
return filepath.Join(pathList...), nil
}
return path, nil
}