go_sysutils/auger/funcs.go

64 lines
1.1 KiB
Go
Raw Permalink Normal View History

package auger
import (
`io/fs`
`os`
`strings`
)
/*
AugpathToFspath returns the filesystem path from an Augeas path.
It is *required* and expected that the Augeas standard /files prefix be removed first;
if not, it is assumed to be part of the filesystem path.
If a valid path cannot be determined, fsPath will be empty.
*/
func AugpathToFspath(augPath string) (fsPath string, err error) {
var path string
var num int
var augSplit []string = strings.Split(augPath, "/")
num = len(augSplit)
for i := num - 1; i >= 0; i-- {
path = strings.Join(augSplit[:i], "/")
if !fs.ValidPath(path) {
continue
}
if _, err = os.Stat(path); err != nil {
if os.IsNotExist(err) {
err = nil
continue
} else {
return
}
}
fsPath = path
return
}
return
}
// dedupePaths is used to reduce new to only unique entries that do not exist in existing.
func dedupePaths(new, existing []string) (missing []string) {
var ok bool
var m map[string]bool = make(map[string]bool)
for _, path := range existing {
m[path] = true
}
for _, path := range new {
if _, ok = m[path]; !ok {
missing = append(missing, path)
}
}
return
}