mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Removed redundant code, updated doc w.r.t reading json files
This commit is contained in:
parent
c2000a446b
commit
ec25886528
@ -35,7 +35,6 @@ Available Commands:
|
||||
new yaml n [--script/-s script_file] a.b.c newValueForC
|
||||
|
||||
Flags:
|
||||
-J, --fromjson[=false]: input as json
|
||||
-h, --help[=false]: help for yaml
|
||||
-j, --tojson[=false]: output as json
|
||||
-t, --trim[=true]: trim yaml output
|
||||
|
3
coverage.sh
Executable file
3
coverage.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
go test -coverprofile=coverage.out && go tool cover -html=coverage.out
|
@ -252,15 +252,15 @@
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#yaml2json" title="Yaml2json" class="md-nav__link">
|
||||
Yaml2json
|
||||
<a href="#yaml-to-json" title="Yaml to Json" class="md-nav__link">
|
||||
Yaml to Json
|
||||
</a>
|
||||
|
||||
</li>
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#json2yaml" title="json2yaml" class="md-nav__link">
|
||||
json2yaml
|
||||
<a href="#json-to-yaml" title="Json to Yaml" class="md-nav__link">
|
||||
Json to Yaml
|
||||
</a>
|
||||
|
||||
</li>
|
||||
@ -293,15 +293,15 @@
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#yaml2json" title="Yaml2json" class="md-nav__link">
|
||||
Yaml2json
|
||||
<a href="#yaml-to-json" title="Yaml to Json" class="md-nav__link">
|
||||
Yaml to Json
|
||||
</a>
|
||||
|
||||
</li>
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#json2yaml" title="json2yaml" class="md-nav__link">
|
||||
json2yaml
|
||||
<a href="#json-to-yaml" title="Json to Yaml" class="md-nav__link">
|
||||
Json to Yaml
|
||||
</a>
|
||||
|
||||
</li>
|
||||
@ -323,10 +323,39 @@
|
||||
|
||||
<h1>Convert</h1>
|
||||
|
||||
<h3 id="yaml2json">Yaml2json</h3>
|
||||
<h3 id="yaml-to-json">Yaml to Json</h3>
|
||||
<p>To convert output to json, use the --tojson (or -j) flag. This can be used with any command.</p>
|
||||
<h3 id="json2yaml">json2yaml</h3>
|
||||
<p>To read in json, use the --fromjson (or -J) flag. This can be used with any command.</p>
|
||||
<p>Given a sample.yaml file of:</p>
|
||||
<pre><code class="yaml">b:
|
||||
c: 2
|
||||
</code></pre>
|
||||
|
||||
<p>then</p>
|
||||
<pre><code class="bash">yaml r -j sample.yaml b.c
|
||||
</code></pre>
|
||||
|
||||
<p>will output</p>
|
||||
<pre><code class="json">{"b":{"c":2}}
|
||||
</code></pre>
|
||||
|
||||
<h3 id="json-to-yaml">Json to Yaml</h3>
|
||||
<p>To read in json, just pass in a json file instead of yaml, it will just work :)</p>
|
||||
<p>e.g given a json file</p>
|
||||
<pre><code class="json">{"a":"Easy! as one two three","b":{"c":2,"d":[3,4]}}
|
||||
</code></pre>
|
||||
|
||||
<p>then</p>
|
||||
<pre><code class="bash">yaml r sample.json
|
||||
</code></pre>
|
||||
|
||||
<p>will output</p>
|
||||
<pre><code class="yaml">a: Easy! as one two three
|
||||
b:
|
||||
c: 2
|
||||
d:
|
||||
- 3
|
||||
- 4
|
||||
</code></pre>
|
||||
|
||||
|
||||
|
||||
|
@ -328,7 +328,7 @@
|
||||
|
||||
|
||||
<h1 id="yaml">yaml</h1>
|
||||
<p>yaml is a lightweight and flexible command-line YAML processor</p>
|
||||
<p>yaml is a lightweight and portable command-line YAML processor</p>
|
||||
<p>The aim of the project is to be the <a href="https://github.com/stedolan/jq">jq</a> or sed of yaml files.</p>
|
||||
<h3 id="download-latest-binary"><a href="https://github.com/mikefarah/yaml/releases/latest">download latest binary</a></h3>
|
||||
<h3 id="get-the-source">get the source</h3>
|
||||
|
@ -2,12 +2,12 @@
|
||||
"docs": [
|
||||
{
|
||||
"location": "/",
|
||||
"text": "yaml\n\n\nyaml is a lightweight and flexible command-line YAML processor\n\n\nThe aim of the project is to be the \njq\n or sed of yaml files.\n\n\ndownload latest binary\n\n\nget the source\n\n\ngo get github.com/mikefarah/yaml\n\n\n\n\n.zip\n or \ntar.gz\n\n\nView on GitHub",
|
||||
"text": "yaml\n\n\nyaml 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\ndownload latest binary\n\n\nget the source\n\n\ngo get github.com/mikefarah/yaml\n\n\n\n\n.zip\n or \ntar.gz\n\n\nView on GitHub",
|
||||
"title": "Install"
|
||||
},
|
||||
{
|
||||
"location": "/#yaml",
|
||||
"text": "yaml is a lightweight and flexible command-line YAML processor The aim of the project is to be the jq or sed of yaml files.",
|
||||
"text": "yaml is a lightweight and portable command-line YAML processor The aim of the project is to be the jq or sed of yaml files.",
|
||||
"title": "yaml"
|
||||
},
|
||||
{
|
||||
@ -22,7 +22,7 @@
|
||||
},
|
||||
{
|
||||
"location": "/read/",
|
||||
"text": "yaml r \nyaml file\n \npath\n\n\n\n\n\nBasic\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.c\n\n\n\n\nwill output the value of '2'.\n\n\nFrom Stdin\n\n\nGiven a sample.yaml file of:\n\n\ncat sample.yaml | yaml r - b.c\n\n\n\n\nwill output the value of '2'.\n\n\nSplat\n\n\nGiven a sample.yaml file of:\n\n\n---\nbob:\n item1:\n cats: bananas\n item2:\n cats: apples\n\n\n\n\nthen\n\n\nyaml r sample.yaml bob.*.cats\n\n\n\n\nwill output\n\n\n- bananas\n- apples\n\n\n\n\nHandling '.' in the yaml key\n\n\nGiven a sample.yaml file of:\n\n\nb.x:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml \\\nb.x\\\n.c\n\n\n\n\nwill output the value of '2'.\n\n\nArrays\n\n\nYou can give an index to access a specific element:\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.e[1].name\n\n\n\n\nwill output 'sam'\n\n\nArray Splat\n\n\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.e[*].name\n\n\n\n\nwill output:\n\n\n- fred\n- sam",
|
||||
"text": "yaml r \nyaml_file|json_file\n \npath\n\n\n\n\n\nThis command can take a json file as input too, and will output yaml unless specified to export as json (-j)\n\n\nBasic\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.c\n\n\n\n\nwill output the value of '2'.\n\n\nFrom Stdin\n\n\nGiven a sample.yaml file of:\n\n\ncat sample.yaml | yaml r - b.c\n\n\n\n\nwill output the value of '2'.\n\n\nSplat\n\n\nGiven a sample.yaml file of:\n\n\n---\nbob:\n item1:\n cats: bananas\n item2:\n cats: apples\n\n\n\n\nthen\n\n\nyaml r sample.yaml bob.*.cats\n\n\n\n\nwill output\n\n\n- bananas\n- apples\n\n\n\n\nHandling '.' in the yaml key\n\n\nGiven a sample.yaml file of:\n\n\nb.x:\n c: 2\n\n\n\n\nthen\n\n\nyaml r sample.yaml \\\nb.x\\\n.c\n\n\n\n\nwill output the value of '2'.\n\n\nArrays\n\n\nYou can give an index to access a specific element:\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.e[1].name\n\n\n\n\nwill output 'sam'\n\n\nArray Splat\n\n\ne.g.: given a sample file of\n\n\nb:\n e:\n - name: fred\n value: 3\n - name: sam\n value: 4\n\n\n\n\nthen\n\n\nyaml r sample.yaml b.e[*].name\n\n\n\n\nwill output:\n\n\n- fred\n- sam",
|
||||
"title": "Read"
|
||||
},
|
||||
{
|
||||
@ -57,7 +57,7 @@
|
||||
},
|
||||
{
|
||||
"location": "/write/",
|
||||
"text": "yaml w \nyaml file\n \npath\n \nnew value\n\n\n\n\n\nTo Stdout\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.c cat\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n\n\n\n\nFrom STDIN\n\n\ncat sample.yaml | yaml w - b.c blah\n\n\n\n\nAdding new fields\n\n\nAny missing fields in the path will be created on the fly.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.d[0] \nnew thing\n\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n d:\n - new thing\n\n\n\n\nUpdating files in-place\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w -i sample.yaml b.c cat\n\n\n\n\nwill update the sample.yaml file so that the value of 'c' is cat.\n\n\nUpdating multiple values with a script\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n e:\n - name: Billy Bob\n\n\n\n\nand a script update_instructions.yaml of:\n\n\nb.c: 3\nb.e[0].name: Howdy Partner\n\n\n\n\nthen\n\n\nyaml w -s update_instructions.yaml sample.yaml\n\n\n\n\nwill output:\n\n\nb:\n c: 3\n e:\n - name: Howdy Partner\n\n\n\n\nAnd, of course, you can pipe the instructions in using '-':\n\n\ncat update_instructions.yaml | yaml w -s - sample.yaml",
|
||||
"text": "yaml w \nyaml_file|json_file\n \npath\n \nnew value\n\n\n\n\n\nThis command can take a json file as input too, and will output yaml unless specified to export as json (-j)\n\n\nTo Stdout\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.c cat\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n\n\n\n\nFrom STDIN\n\n\ncat sample.yaml | yaml w - b.c blah\n\n\n\n\nAdding new fields\n\n\nAny missing fields in the path will be created on the fly.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.d[0] \nnew thing\n\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n d:\n - new thing\n\n\n\n\nUpdating files in-place\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w -i sample.yaml b.c cat\n\n\n\n\nwill update the sample.yaml file so that the value of 'c' is cat.\n\n\nUpdating multiple values with a script\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n e:\n - name: Billy Bob\n\n\n\n\nand a script update_instructions.yaml of:\n\n\nb.c: 3\nb.e[0].name: Howdy Partner\n\n\n\n\nthen\n\n\nyaml w -s update_instructions.yaml sample.yaml\n\n\n\n\nwill output:\n\n\nb:\n c: 3\n e:\n - name: Howdy Partner\n\n\n\n\nAnd, of course, you can pipe the instructions in using '-':\n\n\ncat update_instructions.yaml | yaml w -s - sample.yaml",
|
||||
"title": "Write/Update"
|
||||
},
|
||||
{
|
||||
@ -102,18 +102,18 @@
|
||||
},
|
||||
{
|
||||
"location": "/convert/",
|
||||
"text": "Yaml2json\n\n\nTo convert output to json, use the --tojson (or -j) flag. This can be used with any command.\n\n\njson2yaml\n\n\nTo read in json, use the --fromjson (or -J) flag. This can be used with any command.",
|
||||
"text": "Yaml to Json\n\n\nTo convert output to json, use the --tojson (or -j) flag. This can be used with any command.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml r -j sample.yaml b.c\n\n\n\n\nwill output\n\n\n{\nb\n:{\nc\n:2}}\n\n\n\n\nJson to Yaml\n\n\nTo read in json, just pass in a json file instead of yaml, it will just work :)\n\n\ne.g given a json file\n\n\n{\na\n:\nEasy! as one two three\n,\nb\n:{\nc\n:2,\nd\n:[3,4]}}\n\n\n\n\nthen\n\n\nyaml r sample.json\n\n\n\n\nwill output\n\n\na: Easy! as one two three\nb:\n c: 2\n d:\n - 3\n - 4",
|
||||
"title": "Convert"
|
||||
},
|
||||
{
|
||||
"location": "/convert/#yaml2json",
|
||||
"text": "To convert output to json, use the --tojson (or -j) flag. This can be used with any command.",
|
||||
"title": "Yaml2json"
|
||||
"location": "/convert/#yaml-to-json",
|
||||
"text": "To convert output to json, use the --tojson (or -j) flag. This can be used with any command. Given a sample.yaml file of: b:\n c: 2 then yaml r -j sample.yaml b.c will output { b :{ c :2}}",
|
||||
"title": "Yaml to Json"
|
||||
},
|
||||
{
|
||||
"location": "/convert/#json2yaml",
|
||||
"text": "To read in json, use the --fromjson (or -J) flag. This can be used with any command.",
|
||||
"title": "json2yaml"
|
||||
"location": "/convert/#json-to-yaml",
|
||||
"text": "To read in json, just pass in a json file instead of yaml, it will just work :) e.g given a json file { a : Easy! as one two three , b :{ c :2, d :[3,4]}} then yaml r sample.json will output a: Easy! as one two three\nb:\n c: 2\n d:\n - 3\n - 4",
|
||||
"title": "Json to Yaml"
|
||||
}
|
||||
]
|
||||
}
|
@ -379,9 +379,10 @@
|
||||
|
||||
<h1>Read</h1>
|
||||
|
||||
<pre><code>yaml r <yaml file> <path>
|
||||
<pre><code>yaml r <yaml_file|json_file> <path>
|
||||
</code></pre>
|
||||
|
||||
<p>This command can take a json file as input too, and will output yaml unless specified to export as json (-j)</p>
|
||||
<h3 id="basic">Basic</h3>
|
||||
<p>Given a sample.yaml file of:</p>
|
||||
<pre><code class="yaml">b:
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/</loc>
|
||||
<lastmod>2017-04-13</lastmod>
|
||||
<lastmod>2017-04-19</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/read/</loc>
|
||||
<lastmod>2017-04-13</lastmod>
|
||||
<lastmod>2017-04-19</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
@ -20,7 +20,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/write/</loc>
|
||||
<lastmod>2017-04-13</lastmod>
|
||||
<lastmod>2017-04-19</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
@ -28,7 +28,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/create/</loc>
|
||||
<lastmod>2017-04-13</lastmod>
|
||||
<lastmod>2017-04-19</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
@ -36,7 +36,7 @@
|
||||
|
||||
<url>
|
||||
<loc>/convert/</loc>
|
||||
<lastmod>2017-04-13</lastmod>
|
||||
<lastmod>2017-04-19</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
|
||||
|
@ -365,9 +365,10 @@
|
||||
|
||||
<h1>Write/Update</h1>
|
||||
|
||||
<pre><code>yaml w <yaml file> <path> <new value>
|
||||
<pre><code>yaml w <yaml_file|json_file> <path> <new value>
|
||||
</code></pre>
|
||||
|
||||
<p>This command can take a json file as input too, and will output yaml unless specified to export as json (-j)</p>
|
||||
<h3 id="to-stdout">To Stdout</h3>
|
||||
<p>Given a sample.yaml file of:</p>
|
||||
<pre><code class="yaml">b:
|
||||
|
@ -5,19 +5,6 @@ import (
|
||||
"github.com/mikefarah/yaml/Godeps/_workspace/src/gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func fromJSONBytes(jsonBytes []byte, parsedData *map[interface{}]interface{}) {
|
||||
*parsedData = make(map[interface{}]interface{})
|
||||
var jsonData map[string]interface{}
|
||||
err := json.Unmarshal(jsonBytes, &jsonData)
|
||||
if err != nil {
|
||||
die("error parsing data: ", err)
|
||||
}
|
||||
|
||||
for key, value := range jsonData {
|
||||
(*parsedData)[key] = fromJSON(value)
|
||||
}
|
||||
}
|
||||
|
||||
func jsonToString(context interface{}) string {
|
||||
out, err := json.Marshal(toJSON(context))
|
||||
if err != nil {
|
||||
@ -26,27 +13,6 @@ func jsonToString(context interface{}) string {
|
||||
return string(out)
|
||||
}
|
||||
|
||||
func fromJSON(context interface{}) interface{} {
|
||||
switch context.(type) {
|
||||
case []interface{}:
|
||||
oldArray := context.([]interface{})
|
||||
newArray := make([]interface{}, len(oldArray))
|
||||
for index, value := range oldArray {
|
||||
newArray[index] = fromJSON(value)
|
||||
}
|
||||
return newArray
|
||||
case map[string]interface{}:
|
||||
oldMap := context.(map[string]interface{})
|
||||
newMap := make(map[interface{}]interface{})
|
||||
for key, value := range oldMap {
|
||||
newMap[key] = fromJSON(value)
|
||||
}
|
||||
return newMap
|
||||
default:
|
||||
return context
|
||||
}
|
||||
}
|
||||
|
||||
func toJSON(context interface{}) interface{} {
|
||||
switch context.(type) {
|
||||
case []interface{}:
|
||||
|
@ -1,35 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestJsonFromString(t *testing.T) {
|
||||
var data = parseJSONData(`
|
||||
{
|
||||
"b": {
|
||||
"c": 2
|
||||
}
|
||||
}
|
||||
`)
|
||||
assertResult(t, "map[b:map[c:2]]", fmt.Sprintf("%v", data))
|
||||
}
|
||||
|
||||
func TestJsonFromString_withArray(t *testing.T) {
|
||||
var data = parseJSONData(`
|
||||
{
|
||||
"b": [
|
||||
{ "c": 5 },
|
||||
{ "c": 6 }
|
||||
]
|
||||
}
|
||||
`)
|
||||
assertResult(t, "map[b:[map[c:5] map[c:6]]]", fmt.Sprintf("%v", data))
|
||||
}
|
||||
|
||||
func TestJsonToString(t *testing.T) {
|
||||
var data = parseData(`
|
||||
---
|
||||
@ -48,13 +22,3 @@ b:
|
||||
`)
|
||||
assertResult(t, "{\"b\":[{\"item\":\"one\"},{\"item\":\"two\"}]}", jsonToString(data))
|
||||
}
|
||||
|
||||
func parseJSONData(rawData string) map[string]interface{} {
|
||||
var parsedData map[string]interface{}
|
||||
err := json.Unmarshal([]byte(rawData), &parsedData)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing json: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return parsedData
|
||||
}
|
||||
|
@ -17,3 +17,8 @@ extra:
|
||||
link: 'https://github.com/mikefarah'
|
||||
- type: 'linkedin'
|
||||
link: 'https://www.linkedin.com/in/mike-farah-b5a75b2/'
|
||||
|
||||
markdown_extensions:
|
||||
- markdown_include.include:
|
||||
base_path: mkdocs
|
||||
- toc(permalink=true)
|
@ -1,5 +1,40 @@
|
||||
### Yaml2json
|
||||
### Yaml to Json
|
||||
To convert output to json, use the --tojson (or -j) flag. This can be used with any command.
|
||||
|
||||
### json2yaml
|
||||
To read in json, use the --fromjson (or -J) flag. This can be used with any command.
|
||||
Given a sample.yaml file of:
|
||||
```yaml
|
||||
b:
|
||||
c: 2
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yaml r -j sample.yaml b.c
|
||||
```
|
||||
|
||||
will output
|
||||
```json
|
||||
{"b":{"c":2}}
|
||||
```
|
||||
|
||||
### Json to Yaml
|
||||
To read in json, just pass in a json file instead of yaml, it will just work :)
|
||||
|
||||
e.g given a json file
|
||||
|
||||
```json
|
||||
{"a":"Easy! as one two three","b":{"c":2,"d":[3,4]}}
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yaml r sample.json
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
a: Easy! as one two three
|
||||
b:
|
||||
c: 2
|
||||
d:
|
||||
- 3
|
||||
- 4
|
||||
```
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
```
|
||||
yaml r <yaml file> <path>
|
||||
yaml r <yaml_file|json_file> <path>
|
||||
```
|
||||
|
||||
{!snippets/works_with_json.md!}
|
||||
|
||||
### Basic
|
||||
Given a sample.yaml file of:
|
||||
```yaml
|
||||
|
1
mkdocs/snippets/works_with_json.md
Normal file
1
mkdocs/snippets/works_with_json.md
Normal file
@ -0,0 +1 @@
|
||||
This command can take a json file as input too, and will output yaml unless specified to export as json (-j)
|
@ -1,6 +1,7 @@
|
||||
```
|
||||
yaml w <yaml file> <path> <new value>
|
||||
yaml w <yaml_file|json_file> <path> <new value>
|
||||
```
|
||||
{!snippets/works_with_json.md!}
|
||||
|
||||
### To Stdout
|
||||
Given a sample.yaml file of:
|
||||
|
8
yaml.go
8
yaml.go
@ -35,7 +35,6 @@ func main() {
|
||||
var rootCmd = &cobra.Command{Use: "yaml"}
|
||||
rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output")
|
||||
rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json")
|
||||
rootCmd.PersistentFlags().BoolVarP(&inputJSON, "fromjson", "J", false, "input as json")
|
||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode")
|
||||
rootCmd.AddCommand(cmdRead, cmdWrite, cmdNew)
|
||||
rootCmd.Execute()
|
||||
@ -247,12 +246,7 @@ func readData(filename string, parsedData interface{}, readAsJSON bool) {
|
||||
rawData = readFile(filename)
|
||||
}
|
||||
|
||||
var err interface{}
|
||||
if readAsJSON {
|
||||
fromJSONBytes([]byte(rawData), parsedData.(*map[interface{}]interface{}))
|
||||
} else {
|
||||
err = yaml.Unmarshal([]byte(rawData), parsedData)
|
||||
}
|
||||
err := yaml.Unmarshal([]byte(rawData), parsedData)
|
||||
if err != nil {
|
||||
die("error parsing data: ", err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user