-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
123 lines (106 loc) · 2.26 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package xdg
import (
"fmt"
"os"
"path/filepath"
"strings"
)
type Path interface {
IsValid() bool
Segments() []Segment
String() string
Paths() []Path
First() Path
}
type path []Segment
var _ Path = (*path)(nil)
var _ Dirs = (*path)(nil)
func (p path) String() string {
lis := make([]string, len(p))
for i := range p {
if !p[i].IsValid() {
return ""
}
lis[i] = p[i].String()
}
return filepath.Join(lis...)
}
func (p path) Segments() []Segment {
return p
}
func (p path) Paths() []Path {
return []Path{p}
}
func (p path) First() Path {
return p
}
func (p path) IsValid() bool {
for _, s := range p.Segments() {
if !s.IsValid() {
return false
}
}
return true
}
type pathFn func() []Segment
var _ Path = (*pathFn)(nil)
var _ Dirs = (*pathFn)(nil)
func (p pathFn) String() string {
return path(p()).String()
}
func (p pathFn) Segments() []Segment {
return p()
}
func (p pathFn) Paths() []Path {
return []Path{p}
}
func (p pathFn) First() Path {
return p
}
func (p pathFn) IsValid() bool {
return p() != nil
}
func NewPath(lis ...Segment) Path {
return path(lis)
}
func ParsePath(s string) Path {
sep := string(os.PathSeparator)
sp := strings.Split(s, sep)
lis := make([]Segment, 0, len(sp))
for i := range sp {
switch {
case sp[i] == "~":
lis = append(lis, homeFn)
case strings.HasPrefix(sp[i], "$"):
e := Env(strings.TrimPrefix(sp[i], "$"))
lis = append(lis, e)
case strings.HasPrefix(sp[i], "%") && strings.HasSuffix(sp[i], "%"):
e := Env(strings.Trim(sp[i], "%"))
lis = append(lis, e)
case sp[i] == "":
lis = append(lis, Str(sep))
case len(sp[i]) == 2 && sp[i][1] == ':' && ((sp[i][0] >= 'a' && sp[i][0] <= 'z') || (sp[i][0] >= 'A' && sp[i][0] <= 'Z')):
lis = append(lis, Str(sp[i]+sep))
default:
lis = append(lis, Str(sp[i]))
}
}
return path(lis)
}
func exists(name string) bool {
_, err := fs.Stat(name)
return err == nil || fs.IsExist(err)
}
func createPath(p Path) (string, error) {
var searchedPaths []string
dir := p.String()
if exists(dir) {
return dir, nil
}
if err := fs.MkdirAll(dir, os.ModeDir|0700); err == nil {
return dir, nil
}
searchedPaths = append(searchedPaths, dir)
return "", fmt.Errorf("could not create any of the following paths: %s",
strings.Join(searchedPaths, ", "))
}