mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d46d555b07 | ||
|
|
fb87f638f2 | ||
|
|
facc81d1f4 | ||
|
|
c1f9065c68 | ||
|
|
be84cc3082 | ||
|
|
a2571da1a1 | ||
|
|
6d6e476ac8 | ||
|
|
ae0c042ae6 |
@@ -20,7 +20,7 @@ sudo apt install yq -y
|
||||
```
|
||||
or, [Download latest binary](https://github.com/mikefarah/yq/releases/latest) or alternatively:
|
||||
```
|
||||
go get github.com/mikefarah/yq
|
||||
go get gopkg.in/mikefarah/yq.v1
|
||||
```
|
||||
|
||||
## Run with Docker
|
||||
@@ -81,7 +81,7 @@ Use "yq [command] --help" for more information about a command.
|
||||
## Contribute
|
||||
1. `make [local] vendor`
|
||||
2. add unit tests
|
||||
3. apply changes
|
||||
3. apply changes (use govendor with a preference to [gopkg](https://gopkg.in/) for package dependencies)
|
||||
4. `make [local] build`
|
||||
5. If required, update the user documentation
|
||||
- Update README.md and/or documentation under the mkdocs folder
|
||||
|
||||
121
commands_test.go
121
commands_test.go
@@ -5,7 +5,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/spf13/cobra.v0"
|
||||
)
|
||||
|
||||
func getRootCommand() *cobra.Command {
|
||||
@@ -394,6 +394,28 @@ apples: ok
|
||||
assertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
func TestWriteMultiAllCmd(t *testing.T) {
|
||||
content := `b:
|
||||
c: 3
|
||||
---
|
||||
apples: great
|
||||
`
|
||||
filename := writeTempYamlFile(content)
|
||||
defer removeTempYamlFile(filename)
|
||||
|
||||
cmd := getRootCommand()
|
||||
result := runCmd(cmd, fmt.Sprintf("write %s -d * apples ok", filename))
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `b:
|
||||
c: 3
|
||||
apples: ok
|
||||
---
|
||||
apples: ok`
|
||||
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
|
||||
}
|
||||
|
||||
func TestWriteCmd_EmptyArray(t *testing.T) {
|
||||
content := `b: 3`
|
||||
filename := writeTempYamlFile(content)
|
||||
@@ -569,6 +591,29 @@ func TestDeleteYamlMulti(t *testing.T) {
|
||||
assertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
func TestDeleteYamlMultiAllCmd(t *testing.T) {
|
||||
content := `b:
|
||||
c: 3
|
||||
apples: great
|
||||
---
|
||||
apples: great
|
||||
something: else
|
||||
`
|
||||
filename := writeTempYamlFile(content)
|
||||
defer removeTempYamlFile(filename)
|
||||
|
||||
cmd := getRootCommand()
|
||||
result := runCmd(cmd, fmt.Sprintf("delete %s -d * apples", filename))
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `b:
|
||||
c: 3
|
||||
---
|
||||
something: else`
|
||||
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
|
||||
}
|
||||
|
||||
func TestMergeCmd(t *testing.T) {
|
||||
cmd := getRootCommand()
|
||||
result := runCmd(cmd, "merge examples/data1.yaml examples/data2.yaml")
|
||||
@@ -585,6 +630,22 @@ c:
|
||||
assertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
func TestMergeOverwriteCmd(t *testing.T) {
|
||||
cmd := getRootCommand()
|
||||
result := runCmd(cmd, "merge --overwrite examples/data1.yaml examples/data2.yaml")
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `a: other
|
||||
b:
|
||||
- 1
|
||||
- 2
|
||||
c:
|
||||
test: 1
|
||||
`
|
||||
assertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
func TestMergeCmd_Multi(t *testing.T) {
|
||||
cmd := getRootCommand()
|
||||
result := runCmd(cmd, "merge -d1 examples/multiple_docs_small.yaml examples/data2.yaml")
|
||||
@@ -604,6 +665,64 @@ c:
|
||||
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
|
||||
}
|
||||
|
||||
func TestMergeYamlMultiAllCmd(t *testing.T) {
|
||||
content := `b:
|
||||
c: 3
|
||||
apples: green
|
||||
---
|
||||
something: else`
|
||||
filename := writeTempYamlFile(content)
|
||||
defer removeTempYamlFile(filename)
|
||||
|
||||
mergeContent := `apples: red
|
||||
something: good`
|
||||
mergeFilename := writeTempYamlFile(mergeContent)
|
||||
defer removeTempYamlFile(mergeFilename)
|
||||
|
||||
cmd := getRootCommand()
|
||||
result := runCmd(cmd, fmt.Sprintf("merge -d* %s %s", filename, mergeFilename))
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `apples: green
|
||||
b:
|
||||
c: 3
|
||||
something: good
|
||||
---
|
||||
apples: red
|
||||
something: else`
|
||||
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
|
||||
}
|
||||
|
||||
func TestMergeYamlMultiAllOverwriteCmd(t *testing.T) {
|
||||
content := `b:
|
||||
c: 3
|
||||
apples: green
|
||||
---
|
||||
something: else`
|
||||
filename := writeTempYamlFile(content)
|
||||
defer removeTempYamlFile(filename)
|
||||
|
||||
mergeContent := `apples: red
|
||||
something: good`
|
||||
mergeFilename := writeTempYamlFile(mergeContent)
|
||||
defer removeTempYamlFile(mergeFilename)
|
||||
|
||||
cmd := getRootCommand()
|
||||
result := runCmd(cmd, fmt.Sprintf("merge --overwrite -d* %s %s", filename, mergeFilename))
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `apples: red
|
||||
b:
|
||||
c: 3
|
||||
something: good
|
||||
---
|
||||
apples: red
|
||||
something: good`
|
||||
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
|
||||
}
|
||||
|
||||
func TestMergeCmd_Error(t *testing.T) {
|
||||
cmd := getRootCommand()
|
||||
result := runCmd(cmd, "merge examples/data1.yaml")
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
yaml "gopkg.in/mikefarah/yaml.v2"
|
||||
)
|
||||
|
||||
func entryInSlice(context yaml.MapSlice, key interface{}) *yaml.MapItem {
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
yaml "gopkg.in/mikefarah/yaml.v2"
|
||||
)
|
||||
|
||||
func TestReadMap_simple(t *testing.T) {
|
||||
|
||||
@@ -380,7 +380,7 @@ sudo apt install yq -y
|
||||
</code></pre>
|
||||
|
||||
<p>or, <a href="https://github.com/mikefarah/yq/releases/latest">Download latest binary</a> or alternatively:</p>
|
||||
<pre><code>go get github.com/mikefarah/yq
|
||||
<pre><code>go get gopkg.in/mikefarah/yq.v1
|
||||
</code></pre>
|
||||
|
||||
<p><a href="https://github.com/mikefarah/yq">View on GitHub</a></p>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"docs": [
|
||||
{
|
||||
"location": "/",
|
||||
"text": "yq\n\u00b6\n\n\nyq is a lightweight and portable command-line YAML processor\n\n\nThe aim of the project is to be the \njq\n or sed of yaml files.\n\n\nInstall\n\u00b6\n\n\nOn MacOS:\n\n\nbrew install yq\n\n\n\n\nOn Ubuntu and other Linux distros supporting \nsnap\n packages:\n\n\nsnap install yq\n\n\n\n\nOn Ubuntu 16.04 or higher from Debian package:\n\n\nsudo add-apt-repository ppa:rmescandon/yq\nsudo apt update\nsudo apt install yq -y\n\n\n\n\nor, \nDownload latest binary\n or alternatively:\n\n\ngo get github.com/mikefarah/yq\n\n\n\n\nView on GitHub",
|
||||
"text": "yq\n\u00b6\n\n\nyq is a lightweight and portable command-line YAML processor\n\n\nThe aim of the project is to be the \njq\n or sed of yaml files.\n\n\nInstall\n\u00b6\n\n\nOn MacOS:\n\n\nbrew install yq\n\n\n\n\nOn Ubuntu and other Linux distros supporting \nsnap\n packages:\n\n\nsnap install yq\n\n\n\n\nOn Ubuntu 16.04 or higher from Debian package:\n\n\nsudo add-apt-repository ppa:rmescandon/yq\nsudo apt update\nsudo apt install yq -y\n\n\n\n\nor, \nDownload latest binary\n or alternatively:\n\n\ngo get gopkg.in/mikefarah/yq.v1\n\n\n\n\nView on GitHub",
|
||||
"title": "Install"
|
||||
},
|
||||
{
|
||||
@@ -12,7 +12,7 @@
|
||||
},
|
||||
{
|
||||
"location": "/#install",
|
||||
"text": "On MacOS: brew install yq On Ubuntu and other Linux distros supporting snap packages: snap install yq On Ubuntu 16.04 or higher from Debian package: sudo add-apt-repository ppa:rmescandon/yq\nsudo apt update\nsudo apt install yq -y or, Download latest binary or alternatively: go get github.com/mikefarah/yq View on GitHub",
|
||||
"text": "On MacOS: brew install yq On Ubuntu and other Linux distros supporting snap packages: snap install yq On Ubuntu 16.04 or higher from Debian package: sudo add-apt-repository ppa:rmescandon/yq\nsudo apt update\nsudo apt install yq -y or, Download latest binary or alternatively: go get gopkg.in/mikefarah/yq.v1 View on GitHub",
|
||||
"title": "Install"
|
||||
},
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/</loc>
|
||||
<lastmod>2018-06-12</lastmod>
|
||||
<lastmod>2018-06-18</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/read/</loc>
|
||||
<lastmod>2018-06-12</lastmod>
|
||||
<lastmod>2018-06-18</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/write/</loc>
|
||||
<lastmod>2018-06-12</lastmod>
|
||||
<lastmod>2018-06-18</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/delete/</loc>
|
||||
<lastmod>2018-06-12</lastmod>
|
||||
<lastmod>2018-06-18</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/create/</loc>
|
||||
<lastmod>2018-06-12</lastmod>
|
||||
<lastmod>2018-06-18</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/convert/</loc>
|
||||
<lastmod>2018-06-12</lastmod>
|
||||
<lastmod>2018-06-18</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
@@ -52,7 +52,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/merge/</loc>
|
||||
<lastmod>2018-06-12</lastmod>
|
||||
<lastmod>2018-06-18</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
commonKey: first document
|
||||
a: Easy! as one two three
|
||||
b:
|
||||
c: 2
|
||||
@@ -8,8 +9,14 @@ b:
|
||||
- name: sam
|
||||
value: 4
|
||||
---
|
||||
commonKey: second document
|
||||
another:
|
||||
document: here
|
||||
---
|
||||
commonKey: third document
|
||||
wow:
|
||||
- here is another
|
||||
- here is another
|
||||
---
|
||||
- 1
|
||||
- 2
|
||||
- 3
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
yaml "gopkg.in/mikefarah/yaml.v2"
|
||||
)
|
||||
|
||||
func jsonToString(context interface{}) (string, error) {
|
||||
|
||||
4
merge.go
4
merge.go
@@ -1,8 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/imdario/mergo"
|
||||
)
|
||||
import "gopkg.in/imdario/mergo.v0"
|
||||
|
||||
func merge(dst, src interface{}, overwrite bool) error {
|
||||
if overwrite {
|
||||
|
||||
@@ -20,7 +20,7 @@ sudo apt install yq -y
|
||||
```
|
||||
or, [Download latest binary](https://github.com/mikefarah/yq/releases/latest) or alternatively:
|
||||
```
|
||||
go get github.com/mikefarah/yq
|
||||
go get gopkg.in/mikefarah/yq.v1
|
||||
```
|
||||
|
||||
[View on GitHub](https://github.com/mikefarah/yq)
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
yaml "gopkg.in/mikefarah/yaml.v2"
|
||||
"gopkg.in/spf13/cobra.v0"
|
||||
)
|
||||
|
||||
type resulter struct {
|
||||
|
||||
50
vendor/vendor.json
vendored
50
vendor/vendor.json
vendored
@@ -2,24 +2,12 @@
|
||||
"comment": "",
|
||||
"ignore": "test",
|
||||
"package": [
|
||||
{
|
||||
"checksumSHA1": "66lykxpWgSmQodnhkADqn6tnroQ=",
|
||||
"path": "github.com/imdario/mergo",
|
||||
"revision": "e3000cb3d28c72b837601cac94debd91032d19fe",
|
||||
"revisionTime": "2017-06-20T10:47:01Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "40vJyUB4ezQSn/NSadsKEOrudMc=",
|
||||
"path": "github.com/inconshreveable/mousetrap",
|
||||
"revision": "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75",
|
||||
"revisionTime": "2014-10-17T20:07:13Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "BoXdUBWB8UnSlFlbnuTQaPqfCGk=",
|
||||
"path": "github.com/op/go-logging",
|
||||
"revision": "970db520ece77730c7e4724c61121037378659d9",
|
||||
"revisionTime": "2016-03-15T20:05:05Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "ljd3FhYRJ91cLZz3wsH9BQQ2JbA=",
|
||||
"path": "github.com/pkg/errors",
|
||||
@@ -27,24 +15,34 @@
|
||||
"revisionTime": "2018-03-11T21:45:15Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "xPKgXygsORkmXnLdtFaFmipYKaA=",
|
||||
"path": "github.com/spf13/cobra",
|
||||
"revision": "b78744579491c1ceeaaa3b40205e56b0591b93a3",
|
||||
"revisionTime": "2017-09-05T17:20:51Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "Q52Y7t0lEtk/wcDn5q7tS7B+jqs=",
|
||||
"checksumSHA1": "OJI0OgC5V8gZtfS1e0CDYMhkDNc=",
|
||||
"path": "github.com/spf13/pflag",
|
||||
"revision": "7aff26db30c1be810f9de5038ec5ef96ac41fd7c",
|
||||
"revisionTime": "2017-08-24T17:57:12Z"
|
||||
"revision": "3ebe029320b2676d667ae88da602a5f854788a8a",
|
||||
"revisionTime": "2018-06-01T13:25:42Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "DHNYKS5T54/XOqUsFFzdZMLEnVE=",
|
||||
"origin": "github.com/mikefarah/yaml",
|
||||
"path": "gopkg.in/yaml.v2",
|
||||
"checksumSHA1": "RwlkCZz8VFXAE4aHQQOSC0hLu5k=",
|
||||
"path": "gopkg.in/imdario/mergo.v0",
|
||||
"revision": "9316a62528ac99aaecb4e47eadd6dc8aa6533d58",
|
||||
"revisionTime": "2018-06-08T14:01:56Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "7wtGubs4v7+RZovtlmyT9KwA/gE=",
|
||||
"path": "gopkg.in/mikefarah/yaml.v2",
|
||||
"revision": "e175af14aaa1d0eff2ee04b691e4a4827a111416",
|
||||
"revisionTime": "2018-06-13T04:05:11Z",
|
||||
"tree": true
|
||||
"revisionTime": "2018-06-13T04:05:11Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "rL5r44ASTGubGW88gqQwlvVQshw=",
|
||||
"path": "gopkg.in/op/go-logging.v1",
|
||||
"revision": "b2cb9fa56473e98db8caba80237377e83fe44db5",
|
||||
"revisionTime": "2016-02-11T21:21:56Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "xsZjAbfLrXcMtY6fyQ8QC6EvJD0=",
|
||||
"path": "gopkg.in/spf13/cobra.v0",
|
||||
"revision": "ef82de70bb3f60c65fb8eebacbb2d122ef517385",
|
||||
"revisionTime": "2018-04-27T13:45:50Z"
|
||||
}
|
||||
],
|
||||
"rootPath": "github.com/mikefarah/yq"
|
||||
|
||||
@@ -16,7 +16,7 @@ var (
|
||||
// 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
|
||||
// such as "dev" (in development), "beta", "rc1", etc.
|
||||
VersionPrerelease = "beta"
|
||||
VersionPrerelease = ""
|
||||
)
|
||||
|
||||
// ProductName is the name of the product
|
||||
|
||||
@@ -3,13 +3,18 @@ package main
|
||||
import "testing"
|
||||
|
||||
func TestGetVersionDisplay(t *testing.T) {
|
||||
var expectedVersion = ProductName + " version " + Version
|
||||
if VersionPrerelease != "" {
|
||||
expectedVersion = expectedVersion + "-" + VersionPrerelease
|
||||
}
|
||||
expectedVersion = expectedVersion + "\n"
|
||||
tests := []struct {
|
||||
name string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Display Version",
|
||||
want: ProductName + " version " + Version + "-" + VersionPrerelease + "\n",
|
||||
want: expectedVersion,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
||||
65
yq.go
65
yq.go
@@ -11,10 +11,10 @@ import (
|
||||
"strings"
|
||||
|
||||
errors "github.com/pkg/errors"
|
||||
"gopkg.in/spf13/cobra.v0"
|
||||
|
||||
logging "github.com/op/go-logging"
|
||||
"github.com/spf13/cobra"
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
yaml "gopkg.in/mikefarah/yaml.v2"
|
||||
logging "gopkg.in/op/go-logging.v1"
|
||||
)
|
||||
|
||||
var trimOutput = true
|
||||
@@ -24,7 +24,7 @@ var outputToJSON = false
|
||||
var overwriteFlag = false
|
||||
var verbose = false
|
||||
var version = false
|
||||
var docIndex = 0
|
||||
var docIndex = "0"
|
||||
var log = logging.MustGetLogger("yq")
|
||||
|
||||
func main() {
|
||||
@@ -97,7 +97,7 @@ yq r things.yaml a.array[*].blah
|
||||
Long: "Outputs the value of the given path in the yaml file to STDOUT",
|
||||
RunE: readProperty,
|
||||
}
|
||||
cmdRead.PersistentFlags().IntVarP(&docIndex, "doc", "d", 0, "process document index number (0 based)")
|
||||
cmdRead.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based")
|
||||
return cmdRead
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ a.b.e:
|
||||
}
|
||||
cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
|
||||
cmdWrite.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
|
||||
cmdWrite.PersistentFlags().IntVarP(&docIndex, "doc", "d", 0, "process document index number (0 based)")
|
||||
cmdWrite.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
|
||||
return cmdWrite
|
||||
}
|
||||
|
||||
@@ -153,7 +153,7 @@ Outputs to STDOUT unless the inplace flag is used, in which case the file is upd
|
||||
RunE: deleteProperty,
|
||||
}
|
||||
cmdDelete.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
|
||||
cmdDelete.PersistentFlags().IntVarP(&docIndex, "doc", "d", 0, "process document index number (0 based)")
|
||||
cmdDelete.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
|
||||
return cmdDelete
|
||||
}
|
||||
|
||||
@@ -200,7 +200,7 @@ If overwrite flag is set then existing values will be overwritten using the valu
|
||||
}
|
||||
cmdMerge.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
|
||||
cmdMerge.PersistentFlags().BoolVarP(&overwriteFlag, "overwrite", "x", false, "update the yaml file by overwriting existing values")
|
||||
cmdMerge.PersistentFlags().IntVarP(&docIndex, "doc", "d", 0, "process document index number (0 based)")
|
||||
cmdMerge.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
|
||||
return cmdMerge
|
||||
}
|
||||
|
||||
@@ -226,7 +226,11 @@ func read(args []string) (interface{}, error) {
|
||||
path = args[1]
|
||||
}
|
||||
var generalData interface{}
|
||||
if err := readData(args[0], docIndex, &generalData); err != nil {
|
||||
var docIndexInt, errorParsingDocumentIndex = strconv.ParseInt(docIndex, 10, 32)
|
||||
if errorParsingDocumentIndex != nil {
|
||||
return nil, errors.Wrapf(errorParsingDocumentIndex, "Document index %v is not a integer", docIndex)
|
||||
}
|
||||
if err := readData(args[0], int(docIndexInt), &generalData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if path == "" {
|
||||
@@ -234,8 +238,7 @@ func read(args []string) (interface{}, error) {
|
||||
}
|
||||
|
||||
var paths = parsePath(path)
|
||||
value, err := recurse(generalData, paths[0], paths[1:])
|
||||
return value, err
|
||||
return recurse(generalData, paths[0], paths[1:])
|
||||
}
|
||||
|
||||
func newProperty(cmd *cobra.Command, args []string) error {
|
||||
@@ -276,6 +279,17 @@ func newYaml(args []string) (interface{}, error) {
|
||||
return dataBucket, nil
|
||||
}
|
||||
|
||||
func parseDocumentIndex() (bool, int, error) {
|
||||
if docIndex == "*" {
|
||||
return true, -1, nil
|
||||
}
|
||||
docIndexInt64, err := strconv.ParseInt(docIndex, 10, 32)
|
||||
if err != nil {
|
||||
return false, -1, errors.Wrapf(err, "Document index %v is not a integer or *", docIndex)
|
||||
}
|
||||
return false, int(docIndexInt64), nil
|
||||
}
|
||||
|
||||
type updateDataFn func(dataBucket interface{}, currentIndex int) (interface{}, error)
|
||||
|
||||
func mapYamlDecoder(updateData updateDataFn, encoder *yaml.Encoder) yamlDecoderFn {
|
||||
@@ -286,12 +300,17 @@ func mapYamlDecoder(updateData updateDataFn, encoder *yaml.Encoder) yamlDecoderF
|
||||
var errorUpdating error
|
||||
var currentIndex = 0
|
||||
|
||||
var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
|
||||
if errorParsingDocIndex != nil {
|
||||
return errorParsingDocIndex
|
||||
}
|
||||
|
||||
for {
|
||||
log.Debugf("Read doc %v", currentIndex)
|
||||
errorReading = decoder.Decode(&dataBucket)
|
||||
|
||||
if errorReading == io.EOF {
|
||||
if currentIndex < docIndex {
|
||||
if !updateAll && currentIndex < docIndexInt {
|
||||
return fmt.Errorf("Asked to process document %v but there are only %v document(s)", docIndex, currentIndex)
|
||||
}
|
||||
return nil
|
||||
@@ -318,8 +337,13 @@ func writeProperty(cmd *cobra.Command, args []string) error {
|
||||
if writeCommandsError != nil {
|
||||
return writeCommandsError
|
||||
}
|
||||
var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
|
||||
if errorParsingDocIndex != nil {
|
||||
return errorParsingDocIndex
|
||||
}
|
||||
|
||||
var updateData = func(dataBucket interface{}, currentIndex int) (interface{}, error) {
|
||||
if currentIndex == docIndex {
|
||||
if updateAll || currentIndex == docIndexInt {
|
||||
log.Debugf("Updating doc %v", currentIndex)
|
||||
for _, entry := range writeCommands {
|
||||
path := entry.Key.(string)
|
||||
@@ -365,8 +389,13 @@ func deleteProperty(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
var deletePath = args[1]
|
||||
var paths = parsePath(deletePath)
|
||||
var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
|
||||
if errorParsingDocIndex != nil {
|
||||
return errorParsingDocIndex
|
||||
}
|
||||
|
||||
var updateData = func(dataBucket interface{}, currentIndex int) (interface{}, error) {
|
||||
if currentIndex == docIndex {
|
||||
if updateAll || currentIndex == docIndexInt {
|
||||
log.Debugf("Deleting path in doc %v", currentIndex)
|
||||
return deleteChildValue(dataBucket, paths), nil
|
||||
}
|
||||
@@ -382,9 +411,13 @@ func mergeProperties(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
var input = args[0]
|
||||
var filesToMerge = args[1:]
|
||||
var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
|
||||
if errorParsingDocIndex != nil {
|
||||
return errorParsingDocIndex
|
||||
}
|
||||
|
||||
var updateData = func(dataBucket interface{}, currentIndex int) (interface{}, error) {
|
||||
if currentIndex == docIndex {
|
||||
if updateAll || currentIndex == docIndexInt {
|
||||
log.Debugf("Merging doc %v", currentIndex)
|
||||
var mergedData map[interface{}]interface{}
|
||||
if err := merge(&mergedData, dataBucket, overwriteFlag); err != nil {
|
||||
@@ -520,8 +553,6 @@ func readStream(filename string, yamlDecoder yamlDecoderFn) error {
|
||||
|
||||
func readData(filename string, indexToRead int, parsedData interface{}) error {
|
||||
return readStream(filename, func(decoder *yaml.Decoder) error {
|
||||
// naive implementation of document indexing, decodes all the yaml documents
|
||||
// before the docIndex and throws them away.
|
||||
for currentIndex := 0; currentIndex < indexToRead; currentIndex++ {
|
||||
errorSkipping := decoder.Decode(parsedData)
|
||||
if errorSkipping != nil {
|
||||
|
||||
@@ -29,10 +29,10 @@ func TestRead(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadMulti(t *testing.T) {
|
||||
docIndex = 1
|
||||
docIndex = "1"
|
||||
result, _ := read([]string{"examples/multiple_docs.yaml", "another.document"})
|
||||
assertResult(t, "here", result)
|
||||
docIndex = 0
|
||||
docIndex = "0"
|
||||
}
|
||||
|
||||
func TestReadArray(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user