go_sysutils/auger/funcs.go

98 lines
1.9 KiB
Go
Raw Permalink Normal View History

package auger
import (
`io/fs`
`os`
`strings`
`honnef.co/go/augeas`
)
/*
NewAuger returns an auger.Aug.
See:
https://pkg.go.dev/honnef.co/go/augeas#readme-examples
https://pkg.go.dev/honnef.co/go/augeas#New
for the `root` and `loadPath` parameters
(and, by extension, the `flags` paraemter; note that the `flags`
is an auger.AugFlags, not an augeas.Flag!).
`flags` may be nil.
*/
func NewAuger(root, loadPath string, flags *AugFlags) (aug *Aug, err error) {
aug = new(Aug)
if aug.aug, err = augeas.New(root, loadPath, flags.Eval()); err != nil {
return
}
return
}
// NewAugerFromAugeas returns a wrapped auger.Aug from a (honnef.co/go/augeas).Augeas.
func NewAugerFromAugeas(orig augeas.Augeas) (aug *Aug) {
aug = new(Aug)
aug.aug = orig
return
}
/*
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
}