-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
55 lines (50 loc) · 1.37 KB
/
utils.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 gold
import "strings"
// Path constructs a path and returns it.
func Path(baseDir, path string) string {
if CurrentDirectoryBasedPath(path) || AbsolutePath(path) {
return path
}
if baseDir != "" {
return baseDir + "/" + path
}
return path
}
// CurrentDirectoryBasedPath checks whether the path is based on a current
// directory or not and returns the result.
func CurrentDirectoryBasedPath(path string) bool {
return strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../")
}
// AbsolutePath checks whether the path is an absolute path or not and
// returns the result.
func AbsolutePath(path string) bool {
return strings.HasPrefix(path, "/")
}
// assetPath constructs an asset path and returns it.
func assetPath(path, baseDir, assetBaseDir string) string {
if strings.HasPrefix(path, baseDir) {
path = strings.Replace(path, baseDir, "", 1)
if strings.HasPrefix(path, "/") {
path = strings.Replace(path, "/", "", 1)
}
}
if strings.HasPrefix(path, "./") {
path = strings.Replace(path, "./", "", 1)
}
path = strings.Replace(path, "/./", "/", -1)
var tokens []string
for _, s := range strings.Split(path, "/") {
if s == ".." {
if l := len(tokens); l > 0 {
tokens = tokens[0 : l-1]
}
} else {
tokens = append(tokens, s)
}
}
path = strings.Join(tokens, "/")
if assetBaseDir != "" {
path = assetBaseDir + "/" + path
}
return path
}