mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Keep comments when using the write commandt o update values
This commit is contained in:
parent
bb6f07d147
commit
9f43a4a265
@ -511,7 +511,8 @@ func readUpdateCommands(args []string, expectedArgs int, badArgsMessage string)
|
|||||||
log.Debug("args %v", args)
|
log.Debug("args %v", args)
|
||||||
log.Debug("path %v", args[expectedArgs-2])
|
log.Debug("path %v", args[expectedArgs-2])
|
||||||
log.Debug("Value %v", args[expectedArgs-1])
|
log.Debug("Value %v", args[expectedArgs-1])
|
||||||
updateCommands[0] = yqlib.UpdateCommand{Command: "update", Path: args[expectedArgs-2], Value: valueParser.Parse(args[expectedArgs-1], customTag, customStyle, anchorName, makeAlias), Overwrite: true}
|
value := valueParser.Parse(args[expectedArgs-1], customTag, customStyle, anchorName, makeAlias)
|
||||||
|
updateCommands[0] = yqlib.UpdateCommand{Command: "update", Path: args[expectedArgs-2], Value: value, Overwrite: true, DontUpdateComments: true}
|
||||||
} else if len(args) == expectedArgs-1 {
|
} else if len(args) == expectedArgs-1 {
|
||||||
// don't update the value
|
// don't update the value
|
||||||
updateCommands = make([]yqlib.UpdateCommand, 1)
|
updateCommands = make([]yqlib.UpdateCommand, 1)
|
||||||
|
@ -27,6 +27,24 @@ func TestWriteCmd(t *testing.T) {
|
|||||||
test.AssertResult(t, expectedOutput, result.Output)
|
test.AssertResult(t, expectedOutput, result.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteKeepCommentsCmd(t *testing.T) {
|
||||||
|
content := `b:
|
||||||
|
c: 3 # comment
|
||||||
|
`
|
||||||
|
filename := test.WriteTempYamlFile(content)
|
||||||
|
defer test.RemoveTempYamlFile(filename)
|
||||||
|
|
||||||
|
cmd := getRootCommand()
|
||||||
|
result := test.RunCmd(cmd, fmt.Sprintf("write %s b.c 7", filename))
|
||||||
|
if result.Error != nil {
|
||||||
|
t.Error(result.Error)
|
||||||
|
}
|
||||||
|
expectedOutput := `b:
|
||||||
|
c: 7 # comment
|
||||||
|
`
|
||||||
|
test.AssertResult(t, expectedOutput, result.Output)
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteWithTaggedStyleCmd(t *testing.T) {
|
func TestWriteWithTaggedStyleCmd(t *testing.T) {
|
||||||
content := `b:
|
content := `b:
|
||||||
c: dog
|
c: dog
|
||||||
|
@ -18,6 +18,7 @@ type UpdateCommand struct {
|
|||||||
Value *yaml.Node
|
Value *yaml.Node
|
||||||
Overwrite bool
|
Overwrite bool
|
||||||
DontUpdateNodeValue bool
|
DontUpdateNodeValue bool
|
||||||
|
DontUpdateComments bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func KindString(kind yaml.Kind) string {
|
func KindString(kind yaml.Kind) string {
|
||||||
|
@ -27,9 +27,11 @@ func UpdateNavigationStrategy(updateCommand UpdateCommand, autoCreate bool) Navi
|
|||||||
node.Content = changesToApply.Content
|
node.Content = changesToApply.Content
|
||||||
node.Anchor = changesToApply.Anchor
|
node.Anchor = changesToApply.Anchor
|
||||||
node.Alias = changesToApply.Alias
|
node.Alias = changesToApply.Alias
|
||||||
node.HeadComment = changesToApply.HeadComment
|
if !updateCommand.DontUpdateComments {
|
||||||
node.LineComment = changesToApply.LineComment
|
node.HeadComment = changesToApply.HeadComment
|
||||||
node.FootComment = changesToApply.FootComment
|
node.LineComment = changesToApply.LineComment
|
||||||
|
node.FootComment = changesToApply.FootComment
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Debug("skipping update as node already has value %v and overwriteFlag is ", node.Value, updateCommand.Overwrite)
|
log.Debug("skipping update as node already has value %v and overwriteFlag is ", node.Value, updateCommand.Overwrite)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user