-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve-path.go
55 lines (46 loc) · 1.06 KB
/
resolve-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
49
50
51
52
53
54
55
package nef
import (
"os"
"path/filepath"
"github.com/snivilised/nefilim/internal/third/lo"
)
// ResolvePath performs 2 forms of path resolution. The first is resolving a
// home path reference, via the ~ character; ~ is replaced by the user's
// home path. The second resolves ./ or ../ relative path. The overrides
// do not need to be provided.
func ResolvePath(path string, mocks ...ResolveMocks) string {
result := path
if len(mocks) > 0 {
m := mocks[0]
result = lo.TernaryF(result[0] == '~',
func() string {
if h, err := m.HomeFunc(); err == nil {
return filepath.Join(h, result[1:])
}
return path
},
func() string {
if a, err := m.AbsFunc(result); err == nil {
return a
}
return path
},
)
} else {
result = lo.TernaryF(result[0] == '~',
func() string {
if h, err := os.UserHomeDir(); err == nil {
return filepath.Join(h, result[1:])
}
return path
},
func() string {
if a, err := filepath.Abs(result); err == nil {
return a
}
return path
},
)
}
return result
}