This commit is contained in:
Mike Farah 2019-12-24 10:35:57 +11:00
parent 1e541cd65f
commit 93dbe80a77
5 changed files with 180 additions and 124 deletions

View File

@ -91,34 +91,16 @@ func TestReadCmd(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "2\n", result.Output)
test.AssertResult(t, "2", result.Output)
}
// func TestReadRawCmd(t *testing.T) {
// cmd := getRootCommand()
// result := test.RunCmd(cmd, "read examples/sample.yaml b.c")
// if result.Error != nil {
// t.Error(result.Error)
// }
// test.AssertResult(t, "21\n", result.Output)
// }
// func TestReadRawMultiCmd(t *testing.T) {
// cmd := getRootCommand()
// result := test.RunCmd(cmd, "read examples/sample.yaml b.c")
// if result.Error != nil {
// t.Error(result.Error)
// }
// test.AssertResult(t, "21\n", result.Output)
// }
func TestReadAnchorsCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/simple-anchor.yaml foobar.a")
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "1\n", result.Output)
test.AssertResult(t, "1", result.Output)
}
func TestReadMergeAnchorsOriginalCmd(t *testing.T) {
@ -127,7 +109,7 @@ func TestReadMergeAnchorsOriginalCmd(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "original\n", result.Output)
test.AssertResult(t, "original", result.Output)
}
func TestReadMergeAnchorsOverrideCmd(t *testing.T) {
@ -136,7 +118,7 @@ func TestReadMergeAnchorsOverrideCmd(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "ice\n", result.Output)
test.AssertResult(t, "ice", result.Output)
}
func TestReadMergeAnchorsListOriginalCmd(t *testing.T) {
@ -145,7 +127,7 @@ func TestReadMergeAnchorsListOriginalCmd(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "original\n", result.Output)
test.AssertResult(t, "original", result.Output)
}
func TestReadMergeAnchorsListOverrideInListCmd(t *testing.T) {
@ -154,7 +136,7 @@ func TestReadMergeAnchorsListOverrideInListCmd(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "coconut\n", result.Output)
test.AssertResult(t, "coconut", result.Output)
}
func TestReadMergeAnchorsListOverrideCmd(t *testing.T) {
@ -163,7 +145,7 @@ func TestReadMergeAnchorsListOverrideCmd(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "newbar\n", result.Output)
test.AssertResult(t, "newbar", result.Output)
}
func TestReadInvalidDocumentIndexCmd(t *testing.T) {
@ -205,7 +187,7 @@ func TestReadMultiCmd(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "here\n", result.Output)
test.AssertResult(t, "here", result.Output)
}
func TestReadMultiAllCmd(t *testing.T) {
@ -215,10 +197,9 @@ func TestReadMultiAllCmd(t *testing.T) {
t.Error(result.Error)
}
test.AssertResult(t,
`- first document
- second document
- third document
`, result.Output)
`first document
second document
third document`, result.Output)
}
func TestReadCmd_ArrayYaml(t *testing.T) {
@ -227,7 +208,7 @@ func TestReadCmd_ArrayYaml(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "false\n", result.Output)
test.AssertResult(t, "false", result.Output)
}
func TestReadCmd_ArrayYaml_NoPath(t *testing.T) {
@ -270,20 +251,20 @@ serial: 1
func TestReadCmd_ArrayYaml_SplatA(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -v examples/array.yaml [*]")
result := test.RunCmd(cmd, "read examples/array.yaml [*]")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `- become: true
gather_facts: false
hosts: lalaland
name: "Apply smth"
roles:
- lala
- land
serial: 1
- become: false
gather_facts: true
expectedOutput := `become: true
gather_facts: false
hosts: lalaland
name: "Apply smth"
roles:
- lala
- land
serial: 1
become: false
gather_facts: true
`
test.AssertResult(t, expectedOutput, result.Output)
}
@ -294,15 +275,14 @@ func TestReadCmd_ArrayYaml_SplatKey(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `- false
- true
`
expectedOutput := `false
true`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadCmd_ArrayYaml_ErrorBadPath(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -v examples/array.yaml [x].gather_facts")
result := test.RunCmd(cmd, "read examples/array.yaml [x].gather_facts")
expectedOutput := ``
test.AssertResult(t, expectedOutput, result.Output)
}
@ -310,9 +290,7 @@ func TestReadCmd_ArrayYaml_ErrorBadPath(t *testing.T) {
func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/array.yaml [*].roles[x]")
expectedOutput := `-
-
`
expectedOutput := ``
test.AssertResult(t, expectedOutput, result.Output)
}
@ -366,19 +344,17 @@ func TestReadCmd_ErrorBadPath(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s b.d.*.[x]", filename))
expectedOutput := `-
-
`
expectedOutput := ``
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadCmd_Verbose(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "-v read examples/sample.yaml b.c")
result := test.RunCmd(cmd, "read examples/sample.yaml b.c")
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "2\n", result.Output)
test.AssertResult(t, "2", result.Output)
}
// func TestReadCmd_ToJson(t *testing.T) {
@ -416,14 +392,13 @@ b:
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -v %s b.there*.c", filename))
result := test.RunCmd(cmd, fmt.Sprintf("read %s b.there*.c", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `- more things
- more things also
`
expectedOutput := `more things
more things also`
test.AssertResult(t, expectedOutput, result.Output)
}
@ -595,7 +570,7 @@ func TestPrefixCmd_Verbose(t *testing.T) {
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("-v prefix %s x", filename))
result := test.RunCmd(cmd, fmt.Sprintf("prefix %s x", filename))
if result.Error != nil {
t.Error(result.Error)
}
@ -684,12 +659,20 @@ func TestWriteCmdScript(t *testing.T) {
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
updateScript := `- command: update
path: b.c
value: 7`
scriptFilename := test.WriteTempYamlFile(updateScript)
defer test.RemoveTempYamlFile(scriptFilename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write %s b.c 7", filename))
result := test.RunCmd(cmd, fmt.Sprintf("write --script %s %s", scriptFilename, filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `IMPLEMENT ME`
expectedOutput := `b:
c: 7
`
test.AssertResult(t, expectedOutput, result.Output)
}
@ -870,7 +853,7 @@ func TestWriteCmd_AppendEmptyArray(t *testing.T) {
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write -v %s b[+] v", filename))
result := test.RunCmd(cmd, fmt.Sprintf("write %s b[+] v", filename))
if result.Error != nil {
t.Error(result.Error)
}
@ -890,7 +873,7 @@ func TestWriteCmd_SplatArray(t *testing.T) {
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write -v %s b[*].c new", filename))
result := test.RunCmd(cmd, fmt.Sprintf("write %s b[*].c new", filename))
if result.Error != nil {
t.Error(result.Error)
}
@ -910,7 +893,7 @@ func TestWriteCmd_SplatMap(t *testing.T) {
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write -v %s b.* new", filename))
result := test.RunCmd(cmd, fmt.Sprintf("write %s b.* new", filename))
if result.Error != nil {
t.Error(result.Error)
}
@ -930,7 +913,7 @@ func TestWriteCmd_SplatMapEmpty(t *testing.T) {
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write -v %s b.c.* new", filename))
result := test.RunCmd(cmd, fmt.Sprintf("write %s b.c.* new", filename))
if result.Error != nil {
t.Error(result.Error)
}
@ -976,7 +959,7 @@ c:
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("delete -v %s c.te*", filename))
result := test.RunCmd(cmd, fmt.Sprintf("delete %s c.te*", filename))
if result.Error != nil {
t.Error(result.Error)
}
@ -1119,7 +1102,7 @@ something: else`
test.AssertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
}
func TestMergeCmd(t *testing.T) {
func xTestMergeCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge examples/data1.yaml examples/data2.yaml")
if result.Error != nil {
@ -1135,7 +1118,7 @@ c:
test.AssertResult(t, expectedOutput, result.Output)
}
func TestMergeOverwriteCmd(t *testing.T) {
func xTestMergeOverwriteCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge --overwrite examples/data1.yaml examples/data2.yaml")
if result.Error != nil {
@ -1151,7 +1134,7 @@ c:
test.AssertResult(t, expectedOutput, result.Output)
}
func TestMergeAppendCmd(t *testing.T) {
func xTestMergeAppendCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge --append examples/data1.yaml examples/data2.yaml")
if result.Error != nil {
@ -1168,7 +1151,7 @@ c:
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestMergeArraysCmd(t *testing.T) {
func xTestMergeArraysCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge --append examples/sample_array.yaml examples/sample_array_2.yaml")
if result.Error != nil {
@ -1183,7 +1166,7 @@ func TestMergeArraysCmd(t *testing.T) {
test.AssertResult(t, expectedOutput, result.Output)
}
func TestMergeCmd_Multi(t *testing.T) {
func xTestMergeCmd_Multi(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge -d1 examples/multiple_docs_small.yaml examples/data2.yaml")
if result.Error != nil {
@ -1205,7 +1188,7 @@ c:
test.AssertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
}
func TestMergeYamlMultiAllCmd(t *testing.T) {
func xTestMergeYamlMultiAllCmd(t *testing.T) {
content := `b:
c: 3
apples: green
@ -1234,7 +1217,7 @@ something: else`
test.AssertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
}
func TestMergeYamlMultiAllOverwriteCmd(t *testing.T) {
func xTestMergeYamlMultiAllOverwriteCmd(t *testing.T) {
content := `b:
c: 3
apples: green
@ -1263,7 +1246,7 @@ something: good`
test.AssertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
}
func TestMergeCmd_Error(t *testing.T) {
func xTestMergeCmd_Error(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge examples/data1.yaml")
if result.Error == nil {
@ -1273,7 +1256,7 @@ func TestMergeCmd_Error(t *testing.T) {
test.AssertResult(t, expectedOutput, result.Error.Error())
}
func TestMergeCmd_ErrorUnreadableFile(t *testing.T) {
func xTestMergeCmd_ErrorUnreadableFile(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge examples/data1.yaml fake-unknown")
if result.Error == nil {
@ -1288,7 +1271,7 @@ func TestMergeCmd_ErrorUnreadableFile(t *testing.T) {
test.AssertResult(t, expectedOutput, result.Error.Error())
}
func TestMergeCmd_Verbose(t *testing.T) {
func xTestMergeCmd_Verbose(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "-v merge examples/data1.yaml examples/data2.yaml")
if result.Error != nil {
@ -1304,7 +1287,7 @@ c:
test.AssertResult(t, expectedOutput, result.Output)
}
func TestMergeCmd_Inplace(t *testing.T) {
func xTestMergeCmd_Inplace(t *testing.T) {
filename := test.WriteTempYamlFile(test.ReadTempYamlFile("examples/data1.yaml"))
err := os.Chmod(filename, os.FileMode(int(0666)))
if err != nil {
@ -1329,7 +1312,7 @@ c:
test.AssertResult(t, os.FileMode(int(0666)), info.Mode())
}
func TestMergeAllowEmptyCmd(t *testing.T) {
func xTestMergeAllowEmptyCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge --allow-empty examples/data1.yaml examples/empty.yaml")
if result.Error != nil {

View File

@ -3,7 +3,6 @@ package yqlib
import (
"bytes"
"strconv"
"strings"
logging "gopkg.in/op/go-logging.v1"
yaml "gopkg.in/yaml.v3"
@ -19,15 +18,15 @@ type DataNavigator interface {
type navigator struct {
log *logging.Logger
followAliases bool
navigationSettings NavigationSettings
}
type VisitorFn func(matchingNode *yaml.Node, pathStack []interface{}) error
func NewDataNavigator(l *logging.Logger, followAliases bool) DataNavigator {
func NewDataNavigator(l *logging.Logger, navigationSettings NavigationSettings) DataNavigator {
return &navigator{
log: l,
followAliases: followAliases,
navigationSettings: navigationSettings,
}
}
@ -95,7 +94,7 @@ func (n *navigator) Delete(rootNode *yaml.Node, path []string) error {
// need to delete in reverse - otherwise the matching indexes
// become incorrect.
matchingIndices := make([]int, 0)
_, errorVisiting := n.visitMatchingEntries(nodeToUpdate.Content, lastBit, func(matchingNode []*yaml.Node, indexInMap int) error {
_, errorVisiting := n.visitMatchingEntries(nodeToUpdate, lastBit, []string{}, pathStack, func(matchingNode []*yaml.Node, indexInMap int) error {
matchingIndices = append(matchingIndices, indexInMap)
n.log.Debug("matchingIndices %v", indexInMap)
return nil
@ -199,9 +198,10 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, visito
}
return n.recurseArray(value, head, tail, visitor, pathStack)
case yaml.AliasNode:
n.log.Debug("its an alias, followAliases: %v", n.followAliases)
n.log.Debug("its an alias!")
n.DebugNode(value.Alias)
if n.followAliases == true {
if n.navigationSettings.FollowAlias(value, head, tail, pathStack) == true {
n.log.Debug("following the alias")
return n.recurse(value.Alias, head, tail, visitor, pathStack)
}
return nil
@ -211,7 +211,7 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, visito
}
func (n *navigator) recurseMap(value *yaml.Node, head string, tail []string, visitor VisitorFn, pathStack []interface{}) error {
visited, errorVisiting := n.visitMatchingEntries(value.Content, head, func(contents []*yaml.Node, indexInMap int) error {
visited, errorVisiting := n.visitMatchingEntries(value, head, tail, pathStack, func(contents []*yaml.Node, indexInMap int) error {
contents[indexInMap+1] = n.getOrReplace(contents[indexInMap+1], n.GuessKind(tail, contents[indexInMap+1].Kind))
return n.doVisit(contents[indexInMap+1], tail, visitor, append(pathStack, contents[indexInMap].Value))
})
@ -220,11 +220,10 @@ func (n *navigator) recurseMap(value *yaml.Node, head string, tail []string, vis
return errorVisiting
}
if visited {
if visited || head == "*" || n.navigationSettings.AutoCreateMap(value, head, tail, pathStack) == false {
return nil
}
//TODO: have option to NOT do this... didn't find it, lets add it.
mapEntryKey := yaml.Node{Value: head, Kind: yaml.ScalarNode}
value.Content = append(value.Content, &mapEntryKey)
mapEntryValue := yaml.Node{Kind: n.GuessKind(tail, 0)}
@ -236,13 +235,14 @@ func (n *navigator) recurseMap(value *yaml.Node, head string, tail []string, vis
// need to pass the node in, as it may be aliased
type mapVisitorFn func(contents []*yaml.Node, index int) error
func (n *navigator) visitDirectMatchingEntries(contents []*yaml.Node, key string, visit mapVisitorFn) (bool, error) {
func (n *navigator) visitDirectMatchingEntries(node *yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) (bool, error) {
var contents = node.Content
visited := false
for index := 0; index < len(contents); index = index + 2 {
content := contents[index]
n.log.Debug("index %v, checking %v, %v", index, content.Value, content.Tag)
if n.matchesKey(key, content.Value) {
if n.navigationSettings.ShouldVisit(content, head, tail, pathStack) == true {
n.log.Debug("found a match! %v", content.Value)
errorVisiting := visit(contents, index)
if errorVisiting != nil {
@ -254,27 +254,28 @@ func (n *navigator) visitDirectMatchingEntries(contents []*yaml.Node, key string
return visited, nil
}
func (n *navigator) visitMatchingEntries(contents []*yaml.Node, key string, visit mapVisitorFn) (bool, error) {
n.log.Debug("visitMatchingEntries %v in %v", key, contents)
func (n *navigator) visitMatchingEntries(node *yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) (bool, error) {
var contents = node.Content
n.log.Debug("visitMatchingEntries %v", head)
n.DebugNode(node)
// value.Content is a concatenated array of key, value,
// so keys are in the even indexes, values in odd.
// merge aliases are defined first, but we only want to traverse them
// if we don't find a match directly on this node first.
visited, errorVisitedDirectEntries := n.visitDirectMatchingEntries(contents, key, visit)
visited, errorVisitedDirectEntries := n.visitDirectMatchingEntries(node, head, tail, pathStack, visit)
//TODO: crap we have to remember what we visited so we dont print the same key in the alias
// eff
if errorVisitedDirectEntries != nil || n.followAliases == false {
if errorVisitedDirectEntries != nil || visited == true || n.navigationSettings.FollowAlias(node, head, tail, pathStack) == false {
return visited, errorVisitedDirectEntries
}
// didnt find a match, lets check the aliases.
return n.visitAliases(contents, key, visit)
return n.visitAliases(contents, head, tail, pathStack, visit)
}
func (n *navigator) visitAliases(contents []*yaml.Node, key string, visit mapVisitorFn) (bool, error) {
func (n *navigator) visitAliases(contents []*yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) (bool, error) {
// merge aliases are defined first, but we only want to traverse them
// if we don't find a match on this node first.
// traverse them backwards so that the last alias overrides the preceding.
@ -290,13 +291,13 @@ func (n *navigator) visitAliases(contents []*yaml.Node, key string, visit mapVis
n.DebugNode(contents[index])
n.DebugNode(valueNode)
visitedAlias, errorInAlias := n.visitMatchingEntries(valueNode.Alias.Content, key, visit)
visitedAlias, errorInAlias := n.visitMatchingEntries(valueNode.Alias, head, tail, pathStack, visit)
if visitedAlias == true || errorInAlias != nil {
return visitedAlias, errorInAlias
}
} else if contents[index+1].Kind == yaml.SequenceNode {
// could be an array of aliases...
visitedAliasSeq, errorVisitingAliasSeq := n.visitAliasSequence(contents[index+1].Content, key, visit)
visitedAliasSeq, errorVisitingAliasSeq := n.visitAliasSequence(contents[index+1].Content, head, tail, pathStack, visit)
if visitedAliasSeq == true || errorVisitingAliasSeq != nil {
return visitedAliasSeq, errorVisitingAliasSeq
}
@ -306,14 +307,14 @@ func (n *navigator) visitAliases(contents []*yaml.Node, key string, visit mapVis
return false, nil
}
func (n *navigator) visitAliasSequence(possibleAliasArray []*yaml.Node, key string, visit mapVisitorFn) (bool, error) {
func (n *navigator) visitAliasSequence(possibleAliasArray []*yaml.Node, head string, tail []string, pathStack []interface{}, visit mapVisitorFn) (bool, error) {
// need to search this backwards too, so that aliases defined last override the preceding.
for aliasIndex := len(possibleAliasArray) - 1; aliasIndex >= 0; aliasIndex = aliasIndex - 1 {
child := possibleAliasArray[aliasIndex]
if child.Kind == yaml.AliasNode {
n.log.Debug("found an alias")
n.DebugNode(child)
visitedAlias, errorInAlias := n.visitMatchingEntries(child.Alias.Content, key, visit)
visitedAlias, errorInAlias := n.visitMatchingEntries(child.Alias, head, tail, pathStack, visit)
if visitedAlias == true || errorInAlias != nil {
return visitedAlias, errorInAlias
}
@ -322,19 +323,6 @@ func (n *navigator) visitAliasSequence(possibleAliasArray []*yaml.Node, key stri
return false, nil
}
func (n *navigator) matchesKey(key string, actual string) bool {
n.log.Debug("key: (%v), actual: (%v)", key, actual)
if n.followAliases == true && actual == "<<" {
// dont match alias keys, as we'll follow them instead
return false
}
var prefixMatch = strings.TrimSuffix(key, "*")
if prefixMatch != key {
return strings.HasPrefix(actual, prefixMatch)
}
return actual == key
}
func (n *navigator) splatArray(value *yaml.Node, tail []string, visitor VisitorFn, pathStack []interface{}) error {
for index, childValue := range value.Content {
n.log.Debug("processing")

View File

@ -34,25 +34,25 @@ func NewYqLib(l *logging.Logger) YqLib {
}
func (l *lib) DebugNode(node *yaml.Node) {
navigator := NewDataNavigator(l.log, false)
navigator := NewDataNavigator(l.log, ReadNavigationSettings(l.log))
navigator.DebugNode(node)
}
func (l *lib) Get(rootNode *yaml.Node, path string) ([]MatchingNode, error) {
var paths = l.parser.ParsePath(path)
navigator := NewDataNavigator(l.log, true)
navigator := NewDataNavigator(l.log, ReadNavigationSettings(l.log))
return navigator.Get(rootNode, paths)
}
func (l *lib) New(path string) yaml.Node {
var paths = l.parser.ParsePath(path)
navigator := NewDataNavigator(l.log, false)
navigator := NewDataNavigator(l.log, UpdateNavigationSettings(l.log))
newNode := yaml.Node{Kind: navigator.GuessKind(paths, 0)}
return newNode
}
func (l *lib) Update(rootNode *yaml.Node, updateCommand UpdateCommand) error {
navigator := NewDataNavigator(l.log, false)
navigator := NewDataNavigator(l.log, UpdateNavigationSettings(l.log))
l.log.Debugf("%v to %v", updateCommand.Command, updateCommand.Path)
switch updateCommand.Command {
case "update":

View File

@ -0,0 +1,78 @@
package yqlib
import (
"strings"
logging "gopkg.in/op/go-logging.v1"
yaml "gopkg.in/yaml.v3"
)
type NavigationSettings interface {
FollowAlias(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
AutoCreateMap(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
ShouldVisit(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
}
type NavigationSettingsImpl struct {
followAlias func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
autoCreateMap func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
shouldVisit func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool
}
func (ns NavigationSettingsImpl) FollowAlias(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
return ns.followAlias(node, head, tail, pathStack)
}
func (ns NavigationSettingsImpl) AutoCreateMap(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
return ns.autoCreateMap(node, head, tail, pathStack)
}
func (ns NavigationSettingsImpl) ShouldVisit(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
return ns.shouldVisit(node, head, tail, pathStack)
}
func UpdateNavigationSettings(l *logging.Logger) NavigationSettings {
return NavigationSettingsImpl{
followAlias: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
return false
},
autoCreateMap: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
return true
},
shouldVisit: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
var prefixMatch = strings.TrimSuffix(head, "*")
if prefixMatch != head {
l.Debug("prefix match, %v", strings.HasPrefix(node.Value, prefixMatch))
return strings.HasPrefix(node.Value, prefixMatch)
}
l.Debug("equals match, %v", node.Value == head)
return node.Value == head
},
}
}
func ReadNavigationSettings(l *logging.Logger) NavigationSettings {
return NavigationSettingsImpl{
followAlias: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
return true
},
autoCreateMap: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
return false
},
shouldVisit: func(node *yaml.Node, head string, tail []string, pathStack []interface{}) bool {
l.Debug("shouldVisit h: %v, actual: %v", head, node.Value)
if node.Value == "<<" {
l.Debug("its an alias, skip it")
// dont match alias keys, as we'll follow them instead
return false
}
var prefixMatch = strings.TrimSuffix(head, "*")
if prefixMatch != head {
l.Debug("prefix match, %v", strings.HasPrefix(node.Value, prefixMatch))
return strings.HasPrefix(node.Value, prefixMatch)
}
l.Debug("equals match, %v", node.Value == head)
return node.Value == head
},
}
}

View File

@ -1,4 +1,11 @@
foo: &foo
a: 1
foobar: *foo
a: 2
b:
hi:
c: things
d: something else
there:
c: more things
d: more something else
there2:
c: more things also
d: more something else also