v1.9.0
IMPROVED: * Removed *BROKEN* dep. lrn2fixurshitk
This commit is contained in:
1
auger/TODO
Normal file
1
auger/TODO
Normal file
@@ -0,0 +1 @@
|
||||
This module is still under work.
|
||||
@@ -7,3 +7,35 @@ const (
|
||||
augInclTfm string = "incl" // The transformer keyword for Augeas includes.
|
||||
augAppendSuffix string = "[last()+1]"
|
||||
)
|
||||
|
||||
var (
|
||||
dstPtrTrue bool = true
|
||||
dstPtrFalse bool = false
|
||||
)
|
||||
|
||||
var (
|
||||
// PtrTrue and PtrFalse are convenience references for constructing an AugFlags if needed. It is recommended you do not change these values if you do not like being confused.
|
||||
PtrTrue *bool = &dstPtrTrue
|
||||
PtrFalse *bool = &dstPtrFalse
|
||||
)
|
||||
|
||||
/*
|
||||
IncludeOptNone is the default include recursion option for Aug.RecursiveInclude.
|
||||
* No special behavior is defined
|
||||
* All include directives are assumed to refer:
|
||||
* Explicitly/exclusively to file paths
|
||||
* That must exist
|
||||
*/
|
||||
const IncludeOptNone includeOpt = 0
|
||||
const (
|
||||
// IncludeOptNoExist specifies that inclusions are allowed to not exist, otherwise an error will be raised while attempting to parse them.
|
||||
IncludeOptNoExist includeOpt = 1 << iota
|
||||
// IncludeOptGlobbing indicates that the inclusion system supports globbing (as supported by (github.com/gobwas/glob).Match).
|
||||
IncludeOptGlobbing
|
||||
// IncludeOptRegex indicates that the inclusion system supports matching by regex (as supported by regexp).
|
||||
IncludeOptRegex
|
||||
// IncludeOptDirs indicates that the inclusion system supports matching by directory.
|
||||
IncludeOptDirs
|
||||
// IncludeOptDirsRecursive indicates that the inclusion system also recurses into subdirectories of matched directories. Only used if IncludeOptDirs is also set.
|
||||
IncludeOptDirsRecursive
|
||||
)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
`strings`
|
||||
|
||||
`honnef.co/go/augeas`
|
||||
`r00t2.io/goutils/bitmask`
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -41,12 +42,17 @@ func NewAugerFromAugeas(orig augeas.Augeas) (aug *Aug) {
|
||||
}
|
||||
|
||||
/*
|
||||
AugpathToFspath returns the filesystem path from an Augeas path.
|
||||
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) {
|
||||
|
||||
@@ -95,3 +101,11 @@ func dedupePaths(new, existing []string) (missing []string) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// getInclPaths applies path options to inclusions.
|
||||
func getInclPaths(pathSpec string, inclFlags *bitmask.MaskBit) (fpaths []string, err error) {
|
||||
|
||||
// TODO
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
`github.com/davecgh/go-spew/spew`
|
||||
`github.com/google/shlex`
|
||||
`honnef.co/go/augeas`
|
||||
`r00t2.io/goutils/bitmask`
|
||||
`r00t2.io/sysutils/paths`
|
||||
)
|
||||
|
||||
@@ -146,10 +147,21 @@ breakCmd:
|
||||
An error will be returned if augLens is a nonexistent or not-loaded Augeas lens module.
|
||||
|
||||
Depending on how many files there are and whether globs vs. explicit filepaths are included, this may take a while.
|
||||
*/
|
||||
func (a *Aug) RecursiveInclude(augLens, includeDirective, fsRoot string) (err error) {
|
||||
|
||||
if err = a.addIncl(includeDirective, augLens, fsRoot, nil); err != nil {
|
||||
optFlags may be nil, multiple includeOpt (see the IncludeOpt* constants) as variadic parameters/expanded slice,
|
||||
bitwise-OR'd together, or multiple non-OR'd and OR'd together (all will be combined to a single value).
|
||||
*/
|
||||
func (a *Aug) RecursiveInclude(augLens, includeDirective, fsRoot string, optFlags ...includeOpt) (err error) {
|
||||
|
||||
var flags *bitmask.MaskBit = bitmask.NewMaskBit()
|
||||
|
||||
if optFlags != nil && len(optFlags) > 0 {
|
||||
for _, f := range optFlags {
|
||||
flags.AddFlag(f.toMb())
|
||||
}
|
||||
}
|
||||
|
||||
if err = a.addIncl(includeDirective, augLens, fsRoot, nil, flags); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -164,14 +176,16 @@ func (a *Aug) RecursiveInclude(augLens, includeDirective, fsRoot string) (err er
|
||||
newInclPaths are new filesystem paths/Augeas-compatible glob patterns to load into the filetree and recurse into.
|
||||
They may be nil, especially if the first run.
|
||||
*/
|
||||
func (a *Aug) addIncl(includeDirective, augLens string, fsRoot string, newInclPaths []string) (err error) {
|
||||
func (a *Aug) addIncl(includeDirective, augLens string, fsRoot string, newInclPaths []string, inclFlags *bitmask.MaskBit) (err error) {
|
||||
|
||||
var matches []string // Passed around set of Augeas matches.
|
||||
var exists bool // Used to indicate if the include path exists.
|
||||
var includes []string // Filepath(s)/glob(s) from fetching includeDirective in lensInclPath. These are internal to the application but are recursed.
|
||||
var lensInclPath string // The path of the included paths in the tree. These are internal to Augeas, not the application.
|
||||
var appendPath string // The path for new Augeas includes.
|
||||
var match []string // A placeholder for iterating when populating includes.
|
||||
var fpath string // A placeholder for finding the path of a conf file that contains an includeDirective.
|
||||
var normalizedIncludes []string // A temporary slice to hold normalization operations and other dynamic building.
|
||||
var lensPath string = fmt.Sprintf(augLensTpl, augLens) // The path of the lens (augLens) itself.
|
||||
var augErr *augeas.Error = new(augeas.Error) // We use this to skip "nonexistent" lens.
|
||||
|
||||
@@ -193,7 +207,7 @@ func (a *Aug) addIncl(includeDirective, augLens string, fsRoot string, newInclPa
|
||||
|
||||
// First canonize paths.
|
||||
if newInclPaths != nil && len(newInclPaths) > 0 {
|
||||
// Existing includes. We don't return on an empty lensInclPath because
|
||||
// Existing includes. We don't return on an empty lensInclPath.
|
||||
if matches, err = a.aug.Match(lensInclPath); err != nil {
|
||||
if errors.As(err, augErr) && augErr.Code == augeas.NoMatch {
|
||||
err = nil
|
||||
@@ -221,6 +235,17 @@ func (a *Aug) addIncl(includeDirective, augLens string, fsRoot string, newInclPa
|
||||
// We don't want to bother adding multiple incl's for the same path(s); it can negatively affect Augeas loads.
|
||||
newInclPaths = dedupePaths(newInclPaths, matches)
|
||||
|
||||
// And then apply things like recursion, globbing, etc.
|
||||
normalizedIncludes = make([]string, 0, len(newInclPaths))
|
||||
if inclFlags.HasFlag(IncludeOptGlobbing.toMb()) {
|
||||
// TODO
|
||||
/*
|
||||
if strings.Contains(newInclPaths[idx], "*") {
|
||||
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// Add the new path(s) as Augeas include entries.
|
||||
if newInclPaths != nil {
|
||||
for _, fsPath := range newInclPaths {
|
||||
@@ -285,10 +310,13 @@ func (a *Aug) addIncl(includeDirective, augLens string, fsRoot string, newInclPa
|
||||
}
|
||||
|
||||
if matches != nil && len(matches) != 0 {
|
||||
if err = a.addIncl(includeDirective, augLens, fsRoot, matches); err != nil {
|
||||
if err = a.addIncl(includeDirective, augLens, fsRoot, matches, inclFlags); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
_, _ = exists, normalizedIncludes
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
13
auger/funcs_includeopt.go
Normal file
13
auger/funcs_includeopt.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package auger
|
||||
|
||||
import (
|
||||
`r00t2.io/goutils/bitmask`
|
||||
)
|
||||
|
||||
// toMb returns a bitmask.MaskBit of this includeOpt.
|
||||
func (i includeOpt) toMb() (mb bitmask.MaskBit) {
|
||||
|
||||
mb = bitmask.MaskBit(i)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -22,3 +22,18 @@ func TestNewAuger(t *testing.T) {
|
||||
|
||||
_ = aug
|
||||
}
|
||||
|
||||
func TestRecursiveInclude(t *testing.T) {
|
||||
|
||||
var aug *Aug
|
||||
var err error
|
||||
|
||||
if aug, err = NewAuger("/", "", &AugFlags{DryRun: PtrTrue}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// This requires Nginx to be installed and with a particularly complex nested include system.
|
||||
if err = aug.RecursiveInclude("Nginx", "include", "/etc/nginx"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,11 @@ package auger
|
||||
|
||||
import (
|
||||
`honnef.co/go/augeas`
|
||||
`r00t2.io/goutils/bitmask`
|
||||
)
|
||||
|
||||
type includeOpt bitmask.MaskBit
|
||||
|
||||
// Aug is a wrapper around (honnef.co/go/)augeas.Augeas. Remember to call Aug.Close().
|
||||
type Aug struct {
|
||||
aug augeas.Augeas
|
||||
|
||||
Reference in New Issue
Block a user