From a0cb691601c2877df2964246076f220ec220356c Mon Sep 17 00:00:00 2001 From: Roberto Mier Escandon Date: Sat, 8 Aug 2020 16:55:25 +0200 Subject: [PATCH 01/13] Bump version to 3.3.2 --- debian/changelog | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/debian/changelog b/debian/changelog index 68f9f83a..25f602bd 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +yq (3.3.2) focal; urgency=medium + + * Bug fix: existStatus bug (#459) + * Automatically makes a os temp directory if it does not exist (#461) + + -- Roberto Mier Escandon Fri, 07 Aug 2020 18:53:01 +0200 + yq (3.3-0) focal; urgency=medium * You can control string styles (quotes) using the new --style flag From 5e59803037e3c81175c722a0652583efbf1885a8 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 3 Sep 2020 10:58:00 +1000 Subject: [PATCH 02/13] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index c989905a..42e19b33 100644 --- a/README.md +++ b/README.md @@ -135,3 +135,6 @@ Flags: Use "yq [command] --help" for more information about a command. ``` + +## Known Issues +- `yq` attempts to preserve comment positions and whitespace as much as possible, but it does not handle all scenarios (see https://github.com/go-yaml/yaml/tree/v3 for details) From 759456e375135a34333a2f3a2fb1ed0145e5af1f Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 8 Sep 2020 09:13:50 +1000 Subject: [PATCH 03/13] Lib version bumps --- go.mod | 10 +++++----- go.sum | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index ead6b246..a0eae50f 100644 --- a/go.mod +++ b/go.mod @@ -2,16 +2,16 @@ module github.com/mikefarah/yq/v3 require ( github.com/fatih/color v1.9.0 - github.com/goccy/go-yaml v1.7.5 + github.com/goccy/go-yaml v1.8.1 github.com/kylelemons/godebug v1.1.0 - github.com/mattn/go-colorable v0.1.6 // indirect + github.com/mattn/go-colorable v0.1.7 // indirect github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.0.0 github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect - golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect + golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473 - gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c + gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 ) go 1.14 diff --git a/go.sum b/go.sum index 8b619670..5f40bf83 100644 --- a/go.sum +++ b/go.sum @@ -29,8 +29,8 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/goccy/go-yaml v1.7.5 h1:dWvj+p3BG11S/GlUzwzt1WZz0lhBTzTIDtmXT/ZOaPY= -github.com/goccy/go-yaml v1.7.5/go.mod h1:wS4gNoLalDSJxo/SpngzPQ2BN4uuZVLCmbM4S3vd4+Y= +github.com/goccy/go-yaml v1.8.1 h1:JuZRFlqLM5cWF6A+waL8AKVuCcqvKOuhJtUQI+L3ez0= +github.com/goccy/go-yaml v1.8.1/go.mod h1:wS4gNoLalDSJxo/SpngzPQ2BN4uuZVLCmbM4S3vd4+Y= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= @@ -64,8 +64,8 @@ github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgx github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE= -github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw= +github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= @@ -144,8 +144,8 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193 golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y= -golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f h1:Fqb3ao1hUmOR3GkUOg/Y+BadLwykBIzs5q8Ez2SbHyc= +golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -155,8 +155,8 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= @@ -175,6 +175,6 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo= -gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From bb6f07d1474c47b9908a0c885e07b4bc3646d7a8 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 8 Sep 2020 09:43:11 +1000 Subject: [PATCH 04/13] Use latest go-lang --- .github/workflows/go.yml | 4 ++-- Dockerfile | 2 +- Dockerfile.dev | 2 +- go.mod | 2 +- snap/snapcraft.yaml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 08a5cba4..6c35be9e 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -7,10 +7,10 @@ jobs: runs-on: ubuntu-latest steps: - - name: Set up Go 1.14 + - name: Set up Go 1.15 uses: actions/setup-go@v1 with: - go-version: 1.14 + go-version: 1.15 id: go - name: Check out code into the Go module directory diff --git a/Dockerfile b/Dockerfile index d9a7866f..f62d8f95 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.14 as builder +FROM golang:1.15 as builder WORKDIR /go/src/mikefarah/yq diff --git a/Dockerfile.dev b/Dockerfile.dev index 45301225..00e6a5fc 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -1,4 +1,4 @@ -FROM golang:1.14 +FROM golang:1.15 COPY scripts/devtools.sh /opt/devtools.sh diff --git a/go.mod b/go.mod index a0eae50f..9253a567 100644 --- a/go.mod +++ b/go.mod @@ -14,4 +14,4 @@ require ( gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 ) -go 1.14 +go 1.15 diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index a121cfc9..b292f2ca 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -16,7 +16,7 @@ apps: parts: yq: plugin: go - go-channel: 1.14/stable + go-channel: 1.15/stable source: . source-type: git go-importpath: github.com/mikefarah/yq From 9f43a4a2652482cb0cd9512f4464aebb944a7d83 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 8 Sep 2020 09:46:04 +1000 Subject: [PATCH 05/13] Keep comments when using the write commandt o update values --- cmd/utils.go | 3 ++- cmd/write_test.go | 18 ++++++++++++++++++ pkg/yqlib/lib.go | 1 + pkg/yqlib/update_navigation_strategy.go | 8 +++++--- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/cmd/utils.go b/cmd/utils.go index b48a00f3..8696a30b 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -511,7 +511,8 @@ func readUpdateCommands(args []string, expectedArgs int, badArgsMessage string) log.Debug("args %v", args) log.Debug("path %v", args[expectedArgs-2]) log.Debug("Value %v", args[expectedArgs-1]) - updateCommands[0] = yqlib.UpdateCommand{Command: "update", Path: args[expectedArgs-2], Value: valueParser.Parse(args[expectedArgs-1], customTag, customStyle, anchorName, makeAlias), Overwrite: true} + value := valueParser.Parse(args[expectedArgs-1], customTag, customStyle, anchorName, makeAlias) + updateCommands[0] = yqlib.UpdateCommand{Command: "update", Path: args[expectedArgs-2], Value: value, Overwrite: true, DontUpdateComments: true} } else if len(args) == expectedArgs-1 { // don't update the value updateCommands = make([]yqlib.UpdateCommand, 1) diff --git a/cmd/write_test.go b/cmd/write_test.go index f653ca3a..8249a890 100644 --- a/cmd/write_test.go +++ b/cmd/write_test.go @@ -27,6 +27,24 @@ func TestWriteCmd(t *testing.T) { test.AssertResult(t, expectedOutput, result.Output) } +func TestWriteKeepCommentsCmd(t *testing.T) { + content := `b: + c: 3 # comment +` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("write %s b.c 7", filename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `b: + c: 7 # comment +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestWriteWithTaggedStyleCmd(t *testing.T) { content := `b: c: dog diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 90682186..60d99fb7 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -18,6 +18,7 @@ type UpdateCommand struct { Value *yaml.Node Overwrite bool DontUpdateNodeValue bool + DontUpdateComments bool } func KindString(kind yaml.Kind) string { diff --git a/pkg/yqlib/update_navigation_strategy.go b/pkg/yqlib/update_navigation_strategy.go index bfb376e6..4721a26a 100644 --- a/pkg/yqlib/update_navigation_strategy.go +++ b/pkg/yqlib/update_navigation_strategy.go @@ -27,9 +27,11 @@ func UpdateNavigationStrategy(updateCommand UpdateCommand, autoCreate bool) Navi node.Content = changesToApply.Content node.Anchor = changesToApply.Anchor node.Alias = changesToApply.Alias - node.HeadComment = changesToApply.HeadComment - node.LineComment = changesToApply.LineComment - node.FootComment = changesToApply.FootComment + if !updateCommand.DontUpdateComments { + node.HeadComment = changesToApply.HeadComment + node.LineComment = changesToApply.LineComment + node.FootComment = changesToApply.FootComment + } } else { log.Debug("skipping update as node already has value %v and overwriteFlag is ", node.Value, updateCommand.Overwrite) } From 778f8c69160f531b26879f1a9094bfd022ef3665 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 8 Sep 2020 13:03:18 +1000 Subject: [PATCH 06/13] Removed doctools --- go.sum | 1 + scripts/doctools.sh | 6 ------ 2 files changed, 1 insertion(+), 6 deletions(-) delete mode 100755 scripts/doctools.sh diff --git a/go.sum b/go.sum index 5f40bf83..7672e2c1 100644 --- a/go.sum +++ b/go.sum @@ -152,6 +152,7 @@ golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd h1:/e+gpKk9r3dJobndpTytxS2gOy6m5uvpg+ISQoEcusQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/scripts/doctools.sh b/scripts/doctools.sh deleted file mode 100755 index cb3d90dc..00000000 --- a/scripts/doctools.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -brew install mkdocs libyaml -pip3 install markdown-include -pip3 install mkdocs-material - From 06bb3ac826805c115727f343231073dfa3204a8e Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 9 Sep 2020 11:02:00 +1000 Subject: [PATCH 07/13] Can create new yaml files using scripts again --- cmd/new.go | 7 +------ cmd/new_test.go | 19 +++++++++++++++++++ cmd/utils.go | 4 ++-- cmd/write.go | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cmd/new.go b/cmd/new.go index ebe018a3..fede2523 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -2,7 +2,6 @@ package cmd import ( "github.com/mikefarah/yq/v3/pkg/yqlib" - errors "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,11 +35,7 @@ Note that you can give a create script to perform more sophisticated yaml. This func newProperty(cmd *cobra.Command, args []string) error { var badArgsMessage = "Must provide " - if len(args) != 2 { - return errors.New(badArgsMessage) - } - - var updateCommands, updateCommandsError = readUpdateCommands(args, 2, badArgsMessage) + var updateCommands, updateCommandsError = readUpdateCommands(args, 2, badArgsMessage, false) if updateCommandsError != nil { return updateCommandsError } diff --git a/cmd/new_test.go b/cmd/new_test.go index 4697e8da..bdd9f274 100644 --- a/cmd/new_test.go +++ b/cmd/new_test.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "testing" "github.com/mikefarah/yq/v3/test" @@ -18,6 +19,24 @@ func TestNewCmd(t *testing.T) { test.AssertResult(t, expectedOutput, result.Output) } +func TestNewCmdScript(t *testing.T) { + updateScript := `- command: update + path: b.c + value: 7` + scriptFilename := test.WriteTempYamlFile(updateScript) + defer test.RemoveTempYamlFile(scriptFilename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("new --script %s", scriptFilename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `b: + c: 7 +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestNewAnchorCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "new b.c 3 --anchorName=fred") diff --git a/cmd/utils.go b/cmd/utils.go index 8696a30b..85f6c1d8 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -477,7 +477,7 @@ type updateCommandParsed struct { Value yaml.Node } -func readUpdateCommands(args []string, expectedArgs int, badArgsMessage string) ([]yqlib.UpdateCommand, error) { +func readUpdateCommands(args []string, expectedArgs int, badArgsMessage string, allowNoValue bool) ([]yqlib.UpdateCommand, error) { var updateCommands []yqlib.UpdateCommand = make([]yqlib.UpdateCommand, 0) if writeScript != "" { var parsedCommands = make([]updateCommandParsed, 0) @@ -513,7 +513,7 @@ func readUpdateCommands(args []string, expectedArgs int, badArgsMessage string) log.Debug("Value %v", args[expectedArgs-1]) value := valueParser.Parse(args[expectedArgs-1], customTag, customStyle, anchorName, makeAlias) updateCommands[0] = yqlib.UpdateCommand{Command: "update", Path: args[expectedArgs-2], Value: value, Overwrite: true, DontUpdateComments: true} - } else if len(args) == expectedArgs-1 { + } else if len(args) == expectedArgs-1 && allowNoValue { // don't update the value updateCommands = make([]yqlib.UpdateCommand, 1) log.Debug("args %v", args) diff --git a/cmd/write.go b/cmd/write.go index 052b48c5..be14a132 100644 --- a/cmd/write.go +++ b/cmd/write.go @@ -53,7 +53,7 @@ format is list of update commands (update or delete) like so: } func writeProperty(cmd *cobra.Command, args []string) error { - var updateCommands, updateCommandsError = readUpdateCommands(args, 3, "Must provide ") + var updateCommands, updateCommandsError = readUpdateCommands(args, 3, "Must provide ", true) if updateCommandsError != nil { return updateCommandsError } From c11c3df84fb1f7001bd6bb9941c8e612b0e4d6be Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 9 Sep 2020 12:02:30 +1000 Subject: [PATCH 08/13] Version 3.2.3 --- cmd/version.go | 2 +- snap/snapcraft.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/version.go b/cmd/version.go index 42710477..4513da1d 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -11,7 +11,7 @@ var ( GitDescribe string // Version is main version number that is being run at the moment. - Version = "3.3.2" + Version = "3.3.3" // 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 diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index b292f2ca..44381552 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: yq -version: '3.3.2' +version: '3.3.3' 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. From 1507f929a2ee229f9161a673231f21509a9ee63c Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 11 Sep 2020 11:11:49 +1000 Subject: [PATCH 09/13] Fixing version stuffup - now 3.3.4 --- cmd/version.go | 2 +- snap/snapcraft.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/version.go b/cmd/version.go index 4513da1d..660f7eb4 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -11,7 +11,7 @@ var ( GitDescribe string // Version is main version number that is being run at the moment. - Version = "3.3.3" + Version = "3.3.4" // 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 diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 44381552..0c13ca77 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: yq -version: '3.3.3' +version: '3.3.4' 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. From ee732fbf0bdefd68c670185e54bc1c6e0bacdd63 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sun, 13 Sep 2020 10:09:02 +1000 Subject: [PATCH 10/13] Added Alpine Linux instructions --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 42e19b33..f30d4cc8 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,15 @@ choco install yq ``` Supported by @chillum +### Alpine Linux +- Enable community repo by adding ```$MIRROR/alpine/v$VERSION/community``` to ```/etc/apk/repositories``` +- Update database index with ```apk update``` +- Install yq with ```apk add yq``` + +Supported by Tuan Hoang +https://pkgs.alpinelinux.org/package/edge/community/x86/yq + + ### Ubuntu and other Linux distros supporting `snap` packages: ``` snap install yq From 4f12e09e783445d993f37b36da9042b62bf43552 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sun, 13 Sep 2020 10:16:24 +1000 Subject: [PATCH 11/13] More info on other installation methods --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f30d4cc8..c0e4e452 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ brew install yq ``` choco install yq ``` -Supported by @chillum +Supported by @chillum (https://chocolatey.org/packages/yq) ### Alpine Linux - Enable community repo by adding ```$MIRROR/alpine/v$VERSION/community``` to ```/etc/apk/repositories``` @@ -64,7 +64,7 @@ sudo add-apt-repository ppa:rmescandon/yq sudo apt update sudo apt install yq -y ``` -Supported by @rmescandon +Supported by @rmescandon (https://launchpad.net/~rmescandon/+archive/ubuntu/yq) ### Go Get: ``` From 5b7b390a3330a387a2c875c7c7a53b6e5ae8128c Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sun, 13 Sep 2020 10:32:45 +1000 Subject: [PATCH 12/13] Force static linking --- scripts/xcompile.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/xcompile.sh b/scripts/xcompile.sh index 93b35160..4f640ba0 100755 --- a/scripts/xcompile.sh +++ b/scripts/xcompile.sh @@ -3,9 +3,10 @@ # This assumes that gonative and gox is installed as per the 'one time setup' instructions # at https://github.com/inconshreveable/gonative -gox -ldflags "${LDFLAGS}" -output="build/yq_{{.OS}}_{{.Arch}}" + +CGO_ENABLED=0 gox -ldflags "${LDFLAGS}" -output="build/yq_{{.OS}}_{{.Arch}}" # include non-default linux builds too -gox -ldflags "${LDFLAGS}" -os=linux -output="build/yq_{{.OS}}_{{.Arch}}" +CGO_ENABLED=0 gox -ldflags "${LDFLAGS}" -os=linux -output="build/yq_{{.OS}}_{{.Arch}}" cd build rhash -r -a . -P -o checksums From f528b28938d545e8e8fbe67a0738fcfb5f29e933 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sun, 13 Sep 2020 10:44:11 +1000 Subject: [PATCH 13/13] Convert to JSON now handles non string keys --- cmd/read_test.go | 22 ++++++++++++++++++++++ pkg/yqlib/encoder.go | 17 +++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/cmd/read_test.go b/cmd/read_test.go index 64f89a30..2dd436b9 100644 --- a/cmd/read_test.go +++ b/cmd/read_test.go @@ -94,6 +94,28 @@ func TestReadUnwrapJsonByDefaultCmd(t *testing.T) { test.AssertResult(t, "\"frog\"\n", result.Output) } +func TestReadOutputJsonNonStringKeysCmd(t *testing.T) { + + content := ` +true: true +5: + null: + 0.1: deeply + false: things` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("read %s -j", filename)) + + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `{"5":{"null":{"0.1":"deeply","false":"things"}},"true":true} +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestReadWithAdvancedFilterCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "read ../examples/sample.yaml b.e(name==sam).value") diff --git a/pkg/yqlib/encoder.go b/pkg/yqlib/encoder.go index 172fa389..fbacb963 100644 --- a/pkg/yqlib/encoder.go +++ b/pkg/yqlib/encoder.go @@ -58,6 +58,21 @@ type jsonEncoder struct { encoder *json.Encoder } +func mapKeysToStrings(node *yaml.Node) { + + if node.Kind == yaml.MappingNode { + for index, child := range node.Content { + if index % 2 == 0 { // its a map key + child.Tag = "!!str" + } + } + } + + for _, child := range node.Content { + mapKeysToStrings(child) + } +} + func NewJsonEncoder(destination io.Writer, prettyPrint bool, indent int) Encoder { var encoder = json.NewEncoder(destination) var indentString = "" @@ -73,6 +88,8 @@ func NewJsonEncoder(destination io.Writer, prettyPrint bool, indent int) Encoder func (je *jsonEncoder) Encode(node *yaml.Node) error { var dataBucket interface{} + // firstly, convert all map keys to strings + mapKeysToStrings(node) errorDecoding := node.Decode(&dataBucket) if errorDecoding != nil { return errorDecoding