Added unwrap flag

This commit is contained in:
Mike Farah 2020-04-13 11:03:18 +10:00
parent 55c4d01a91
commit dfdbbbb24a
4 changed files with 18 additions and 1 deletions

View File

@ -94,6 +94,21 @@ func TestReadCmd(t *testing.T) {
test.AssertResult(t, "2\n", result.Output)
}
func TestReadUnwrapCmd(t *testing.T) {
content := `b: 'frog' # my favourite`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s b --unwrapScalar=false", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "'frog' # my favourite\n", result.Output)
}
func TestCompareSameCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "compare ../examples/data1.yaml ../examples/data1.yaml")

View File

@ -8,6 +8,7 @@ import (
var customTag = ""
var printMode = "v"
var printLength = false
var unwrapScalar = true
var collectIntoArray = false
var writeInplace = false
var writeScript = ""

View File

@ -28,6 +28,7 @@ yq r -- things.yaml '--key-starting-with-dashes.blah'
cmdRead.PersistentFlags().StringVarP(&defaultValue, "defaultValue", "D", "", "default value printed when there are no results")
cmdRead.PersistentFlags().BoolVarP(&printLength, "length", "l", false, "print length of results")
cmdRead.PersistentFlags().BoolVarP(&collectIntoArray, "collect", "c", false, "collect results into array")
cmdRead.PersistentFlags().BoolVarP(&unwrapScalar, "unwrapScalar", "", true, "unwrap scalar, print the value with no quotes, colors or comments")
cmdRead.PersistentFlags().BoolVarP(&explodeAnchors, "explodeAnchors", "X", false, "explode anchors")
return cmdRead
}

View File

@ -103,7 +103,7 @@ func transformNode(node *yaml.Node) *yaml.Node {
func printNode(node *yaml.Node, writer io.Writer) error {
var encoder yqlib.Encoder
if node.Kind == yaml.ScalarNode {
if node.Kind == yaml.ScalarNode && unwrapScalar {
return writeString(writer, node.Value+"\n")
}
if outputToJSON {