-
Notifications
You must be signed in to change notification settings - Fork 2
/
path.go
64 lines (51 loc) · 1.09 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
package lib
import (
"os"
"path/filepath"
"strings"
)
type Path struct {
Path string
}
func (p Path) Pop() string {
_, last := filepath.Split(strings.TrimSuffix(p.Path, string(os.PathSeparator)))
return last
}
func (p Path) EndingSlash() bool {
if p.Path == "" {
return false
}
return p.Path[len(p.Path)-1:] == string(os.PathSeparator)
}
func (p Path) PruneStartingSlash() Path {
if p.Path == "" {
return p
}
if p.Path[0:1] == string(os.PathSeparator) {
return Path{Path: p.Path[1:]}
}
return p
}
func (p Path) PruneEndingSlash() Path {
if !p.EndingSlash() {
return Path{Path: p.Path}
}
return Path{Path: p.Path[0 : len(p.Path)-1]}
}
func (p Path) ConvertEmptyToRoot() Path {
if p.Path == "" {
return Path{Path: "."}
}
return p
}
func (p Path) Join(str ...string) Path {
return Path{Path: filepath.Join(p.String(), filepath.Join(str...))}
}
func (p Path) String() string {
return p.Path
}
func (p Path) NormalizePathSystemForAPI() Path {
return Path{
Path: UrlJoinNoEscape(strings.Split(filepath.Clean(p.Path), string(os.PathSeparator))...),
}.PruneStartingSlash()
}