go_sysutils/auger/funcs.go

112 lines
2.3 KiB
Go
Raw Permalink Normal View History

package auger
import (
`io/fs`
`os`
`strings`
`honnef.co/go/augeas`
`r00t2.io/goutils/bitmask`
)
/*
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 (i.e. an existing file) 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.
To be clear, a file must exist for fsPath to not be empty;
the way AugpathToFsPath works is it recurses bottom-up a
given path and checks for the existence of a file,
continuing upwards if not found.
*/
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
}
// getInclPaths applies path options to inclusions.
func getInclPaths(pathSpec string, inclFlags *bitmask.MaskBit) (fpaths []string, err error) {
// TODO
return
}