mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
Added strip comments functionality
This commit is contained in:
parent
33e35d10dd
commit
2db69c91c9
@ -109,6 +109,29 @@ func TestReadUnwrapCmd(t *testing.T) {
|
|||||||
test.AssertResult(t, "'frog' # my favourite\n", result.Output)
|
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) {
|
func TestReadUnwrapJsonByDefaultCmd(t *testing.T) {
|
||||||
|
|
||||||
content := `b: 'frog' # my favourite`
|
content := `b: 'frog' # my favourite`
|
||||||
|
@ -9,6 +9,7 @@ var customTag = ""
|
|||||||
var printMode = "v"
|
var printMode = "v"
|
||||||
var printLength = false
|
var printLength = false
|
||||||
var unwrapScalar = true
|
var unwrapScalar = true
|
||||||
|
var stripComments = false
|
||||||
var collectIntoArray = false
|
var collectIntoArray = false
|
||||||
var writeInplace = false
|
var writeInplace = false
|
||||||
var writeScript = ""
|
var writeScript = ""
|
||||||
|
@ -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(&printLength, "length", "l", false, "print length of results")
|
||||||
cmdRead.PersistentFlags().BoolVarP(&collectIntoArray, "collect", "c", false, "collect results into array")
|
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(&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")
|
cmdRead.PersistentFlags().BoolVarP(&explodeAnchors, "explodeAnchors", "X", false, "explode anchors")
|
||||||
return cmdRead
|
return cmdRead
|
||||||
}
|
}
|
||||||
|
20
cmd/utils.go
20
cmd/utils.go
@ -114,6 +114,22 @@ func printNode(node *yaml.Node, writer io.Writer) error {
|
|||||||
return encoder.Encode(node)
|
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) {
|
func setStyle(matchingNodes []*yqlib.NodeContext, style yaml.Style) {
|
||||||
for _, nodeContext := range matchingNodes {
|
for _, nodeContext := range matchingNodes {
|
||||||
updateStyleOfNode(nodeContext.Node, style)
|
updateStyleOfNode(nodeContext.Node, style)
|
||||||
@ -160,6 +176,10 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
|
|||||||
setStyle(matchingNodes, 0)
|
setStyle(matchingNodes, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if stripComments {
|
||||||
|
removeComments(matchingNodes)
|
||||||
|
}
|
||||||
|
|
||||||
//always explode anchors when printing json
|
//always explode anchors when printing json
|
||||||
if explodeAnchors || outputToJSON {
|
if explodeAnchors || outputToJSON {
|
||||||
errorExploding := explode(matchingNodes)
|
errorExploding := explode(matchingNodes)
|
||||||
|
Loading…
Reference in New Issue
Block a user