From dfdbbbb24af009b6b0554a584e6b7b1a17bdad60 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 13 Apr 2020 11:03:18 +1000 Subject: [PATCH] Added unwrap flag --- cmd/commands_test.go | 15 +++++++++++++++ cmd/constant.go | 1 + cmd/read.go | 1 + cmd/utils.go | 2 +- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 5945746..085e557 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -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") diff --git a/cmd/constant.go b/cmd/constant.go index 69e3a8e..af0efc4 100644 --- a/cmd/constant.go +++ b/cmd/constant.go @@ -8,6 +8,7 @@ import ( var customTag = "" var printMode = "v" var printLength = false +var unwrapScalar = true var collectIntoArray = false var writeInplace = false var writeScript = "" diff --git a/cmd/read.go b/cmd/read.go index 35f62af..03c84f1 100644 --- a/cmd/read.go +++ b/cmd/read.go @@ -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 } diff --git a/cmd/utils.go b/cmd/utils.go index 879630c..4a1df54 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -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 {