blob: cc3d16cac2d9dba70e857d3a24d80a297c32c8c1 (
plain)
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 crawl
import (
"path"
"path/filepath"
"slices"
"strings"
)
// Evaluated returns a path with evaluated symlinks
func Evaluated(loc string) (string, error) {
return filepath.EvalSymlinks(loc)
}
// RichLocations combines the path and Evaluated path Locations
func RichLocations(origin string) []string {
locs := Locations(origin)
eval, err := Evaluated(origin)
if err == nil && eval != origin {
evaledLocations := Locations(eval)
result := append(locs, evaledLocations...)
result = slices.Compact(result)
return result
}
return locs
}
// Locations returns a slice of increasingly shorter file paths,
// losing a segment each time.
func Locations(origin string) []string {
origin = path.Clean(origin)
var locs = []string{
origin,
}
for {
previous := locs[len(locs)-1]
parent := ParentDir(previous)
if previous == parent {
break
}
locs = append(locs, parent)
}
return locs
}
// ParentDir returns a path with the top element missing
func ParentDir(origin string) string {
trimmedOrigin := strings.TrimSuffix(origin, "/")
dir, _ := path.Split(trimmedOrigin)
if dir == "" {
return origin
}
return dir
}
|