/
matching_path.go
74 lines (62 loc) · 1.85 KB
/
matching_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
package component
import (
"strings"
"path/filepath"
)
type (
//MatchingPath represents the matching path of the searched content
MatchingPath interface {
//Component gives the usable component wherin the searched content has been located
Component() UsableComponent
//RelativePath specifies the relatives path of the searched content into the usable component
RelativePath() string
//AbsolutePath specifies the absolute path of the searched content into the usable component
AbsolutePath() string
}
mPath struct {
comp UsableComponent
relativePath string
}
//MatchingPaths represents the matching paths of the searched content
MatchingPaths struct {
//Paths holds the searched results
Paths []MatchingPath
}
)
func (p mPath) Component() UsableComponent {
return p.comp
}
func (p mPath) RelativePath() string {
return p.relativePath
}
func (p mPath) AbsolutePath() string {
return filepath.Join(p.Component().RootPath(), p.RelativePath())
}
//Release deletes, if any, the templated paths returned
func (mp MatchingPaths) Release() {
for _, v := range mp.Paths {
v.Component().Release()
}
}
//Count returns the number of matching paths
func (mp MatchingPaths) Count() int {
return len(mp.Paths)
}
//JoinAbsolutePaths joins all the matching paths using the given separator
func (mp MatchingPaths) JoinAbsolutePaths(separator string) string {
paths := make([]string, 0, 0)
for _, v := range mp.Paths {
paths = append(paths, filepath.Join(v.Component().RootPath(), v.RelativePath()))
}
return strings.Join(paths, separator)
}
//PrefixPaths returns the absolute mathcing paths prefixed with the given prefix
func (mp MatchingPaths) PrefixPaths(prefix string) []string {
l := len(mp.Paths)
res := make([]string, 0, 0)
for i := 0; i < l; i++ {
res = append(res, prefix)
res = append(res, mp.Paths[i].AbsolutePath())
}
return res
}