util/path: add PathList function
This commit is contained in:
parent
3e9fcf9d7b
commit
7fa14c1b7a
|
@ -14,11 +14,16 @@
|
||||||
|
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import "path"
|
import (
|
||||||
|
"path"
|
||||||
|
)
|
||||||
|
|
||||||
// PathHierarchy return a slice of paths from the base path (root included as . or / ).
|
// 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:
|
// I.E. for a path like "path/to/file" it'll return a slice of these elements:
|
||||||
// ".", "path", "path/to", "path/to/file"
|
// ".", "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 {
|
func PathHierarchy(p string) []string {
|
||||||
paths := []string{}
|
paths := []string{}
|
||||||
for {
|
for {
|
||||||
|
@ -31,3 +36,18 @@ func PathHierarchy(p string) []string {
|
||||||
}
|
}
|
||||||
return paths
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue