From 2db69c91c9682252d8f3f6f1f3db77978558e3d4 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 14 Apr 2020 14:48:45 +1000 Subject: [PATCH] Added strip comments functionality --- cmd/commands_test.go | 23 +++++++++++++++++++++++ cmd/constant.go | 1 + cmd/read.go | 1 + cmd/utils.go | 20 ++++++++++++++++++++ 4 files changed, 45 insertions(+) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 861dfdeb..00f27267 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -109,6 +109,29 @@ func TestReadUnwrapCmd(t *testing.T) { test.AssertResult(t, "'frog' # my favourite\n", result.Output) } +func TestReadStripCommentsCmd(t *testing.T) { + + content := `# this is really cool +b: # my favourite + c: 5 # cats +# blah +` + + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("read %s --stripComments", filename)) + + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `b: + c: 5 +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestReadUnwrapJsonByDefaultCmd(t *testing.T) { content := `b: 'frog' # my favourite` diff --git a/cmd/constant.go b/cmd/constant.go index af0efc46..9019236c 100644 --- a/cmd/constant.go +++ b/cmd/constant.go @@ -9,6 +9,7 @@ var customTag = "" var printMode = "v" var printLength = false var unwrapScalar = true +var stripComments = false var collectIntoArray = false var writeInplace = false var writeScript = "" diff --git a/cmd/read.go b/cmd/read.go index 03c84f18..48e40347 100644 --- a/cmd/read.go +++ b/cmd/read.go @@ -29,6 +29,7 @@ yq r -- things.yaml '--key-starting-with-dashes.blah' 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(&stripComments, "stripComments", "", false, "print yaml without any comments") cmdRead.PersistentFlags().BoolVarP(&explodeAnchors, "explodeAnchors", "X", false, "explode anchors") return cmdRead } diff --git a/cmd/utils.go b/cmd/utils.go index d193d915..658657a3 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -114,6 +114,22 @@ func printNode(node *yaml.Node, writer io.Writer) error { return encoder.Encode(node) } +func removeComments(matchingNodes []*yqlib.NodeContext) { + for _, nodeContext := range matchingNodes { + removeCommentOfNode(nodeContext.Node) + } +} + +func removeCommentOfNode(node *yaml.Node) { + node.HeadComment = "" + node.LineComment = "" + node.FootComment = "" + + for _, child := range node.Content { + removeCommentOfNode(child) + } +} + func setStyle(matchingNodes []*yqlib.NodeContext, style yaml.Style) { for _, nodeContext := range matchingNodes { updateStyleOfNode(nodeContext.Node, style) @@ -160,6 +176,10 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error { setStyle(matchingNodes, 0) } + if stripComments { + removeComments(matchingNodes) + } + //always explode anchors when printing json if explodeAnchors || outputToJSON { errorExploding := explode(matchingNodes)