1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Mike Farah
e15633023f Increment version 2020-04-16 15:29:58 +10:00
Mike Farah
6f0a329331 Fixed inplace errors clearing out original file 2020-04-15 11:48:42 +10:00
apenav
55511de9af Upgrade yq_dev container to golang:1.14 base container and deb packages to python3 2020-04-15 09:07:40 +10:00
Mike Farah
2db69c91c9 Added strip comments functionality 2020-04-14 14:48:45 +10:00
7 changed files with 77 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
FROM golang:1.13
FROM golang:1.14
COPY scripts/devtools.sh /opt/devtools.sh
@@ -9,15 +9,15 @@ RUN set -e -x \
RUN set -ex \
&& buildDeps=' \
build-essential \
python-dev \
python3-dev \
' \
&& apt-get update && apt-get install -y --no-install-recommends \
$buildDeps \
python2.7 \
python-setuptools \
python-wheel \
python-pip \
&& pip install --upgrade \
python3 \
python3-setuptools \
python3-wheel \
python3-pip \
&& pip3 install --upgrade \
pip \
'Markdown>=2.6.9' \
'mkdocs>=0.16.3' \

View File

@@ -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`
@@ -1904,6 +1927,22 @@ func TestWriteCmd_Inplace(t *testing.T) {
test.AssertResult(t, expectedOutput, strings.Trim(gotOutput, "\n "))
}
func TestWriteCmd_InplaceError(t *testing.T) {
content := `b: cat
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write -i %s b.c 7", filename))
if result.Error == nil {
t.Error("Expected Error to occur!")
}
gotOutput := test.ReadTempYamlFile(filename)
test.AssertResult(t, content, gotOutput)
}
func TestWriteCmd_Append(t *testing.T) {
content := `b:
- foo

View File

@@ -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 = ""

View File

@@ -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
}

View File

@@ -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)
@@ -331,6 +351,7 @@ func updateDoc(inputFile string, updateCommands []yqlib.UpdateCommand, writer io
func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn) error {
var destination io.Writer
var destinationName string
var completedSuccessfully = false
if writeInplace {
info, err := os.Stat(inputFile)
if err != nil {
@@ -348,7 +369,9 @@ func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn)
destination = tempFile
defer func() {
safelyCloseFile(tempFile)
safelyRenameFile(tempFile.Name(), inputFile)
if completedSuccessfully {
safelyRenameFile(tempFile.Name(), inputFile)
}
}()
} else {
destination = stdOut
@@ -367,7 +390,9 @@ func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn)
encoder = yqlib.NewYamlEncoder(bufferedWriter, indent, colorsEnabled)
}
return readStream(inputFile, mapYamlDecoder(updateData, encoder))
var errorProcessing = readStream(inputFile, mapYamlDecoder(updateData, encoder))
completedSuccessfully = errorProcessing == nil
return errorProcessing
}
type updateCommandParsed struct {

View File

@@ -11,7 +11,7 @@ var (
GitDescribe string
// Version is main version number that is being run at the moment.
Version = "3.2.1"
Version = "3.2.2"
// VersionPrerelease is a pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release

View File

@@ -1,5 +1,5 @@
name: yq
version: '3.2.1'
version: '3.2.2'
summary: A lightweight and portable command-line YAML processor
description: |
The aim of the project is to be the jq or sed of yaml files.