From 33e35d10ddc974acac4ca65fd29f45c9dad4dd64 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 14 Apr 2020 14:11:44 +1000 Subject: [PATCH] Dont unwrap json output by default --- cmd/commands_test.go | 15 +++++++++++++++ cmd/utils.go | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 085e5570..861dfdeb 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -109,6 +109,21 @@ func TestReadUnwrapCmd(t *testing.T) { test.AssertResult(t, "'frog' # my favourite\n", result.Output) } +func TestReadUnwrapJsonByDefaultCmd(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 -j", filename)) + + if result.Error != nil { + t.Error(result.Error) + } + test.AssertResult(t, "\"frog\"\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/utils.go b/cmd/utils.go index 4a1df542..d193d915 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 && unwrapScalar { + if node.Kind == yaml.ScalarNode && unwrapScalar && !outputToJSON { return writeString(writer, node.Value+"\n") } if outputToJSON {