util/path: add PathList function

This commit is contained in:
Simone Gotti 2019-03-28 16:02:11 +01:00
parent 3e9fcf9d7b
commit 7fa14c1b7a
1 changed files with 21 additions and 1 deletions

View File

@ -14,11 +14,16 @@
package util
import "path"
import (
"path"
)
// PathHierarchy return a slice of paths from the base path (root included as . or / ).
// I.E. for a path like "path/to/file" it'll return a slice of these elements:
// ".", "path", "path/to", "path/to/file"
// for a path like "/path/to/file" it'll return a slice of these elements:
// "/", "/path", "/path/to", "/path/to/file"
func PathHierarchy(p string) []string {
paths := []string{}
for {
@ -31,3 +36,18 @@ func PathHierarchy(p string) []string {
}
return paths
}
// PathList return a slice of paths from the base path (root exluded as . or / ).
// I.E. for a path like "path/to/file" or "/path/to/file" it'll return a slice of these elements:
// "path", "to", "file"
func PathList(p string) []string {
paths := []string{}
for {
paths = append([]string{path.Base(p)}, paths...)
p = path.Dir(p)
if p == "." || p == "/" {
break
}
}
return paths
}