util: safepath: Add a new Dir method and tests

This adds a new helper method. It should be considered for other types
as well.
This commit is contained in:
James Shubin
2022-05-10 23:44:48 -04:00
parent b26f842de1
commit 00f6045b12
2 changed files with 66 additions and 1 deletions

View File

@@ -176,7 +176,6 @@ func (obj AbsFile) Cmp(absFile AbsFile) error {
}
// Base returns the last component of the AbsFile, in this case, the filename.
// TODO: add tests
func (obj AbsFile) Base() RelFile {
obj.PanicValidate()
ix := strings.LastIndex(obj.path, "/")
@@ -185,6 +184,20 @@ func (obj AbsFile) Base() RelFile {
}
}
// Dir returns the head component of the AbsFile, in this case, the directory.
func (obj AbsFile) Dir() AbsDir {
obj.PanicValidate()
ix := strings.LastIndex(obj.path, "/")
if ix == 0 {
return AbsDir{
path: "/",
}
}
return AbsDir{
path: obj.path[0:ix],
}
}
// HasDir returns true if the input relative dir is present in the path.
func (obj AbsFile) HasDir(relDir RelDir) bool {
obj.PanicValidate()