Removed redundant code, updated doc w.r.t reading json files

This commit is contained in:
Mike Farah 2017-04-19 15:45:45 +10:00
parent c2000a446b
commit ec25886528
16 changed files with 114 additions and 113 deletions

View File

@ -35,7 +35,6 @@ Available Commands:
new yaml n [--script/-s script_file] a.b.c newValueForC new yaml n [--script/-s script_file] a.b.c newValueForC
Flags: Flags:
-J, --fromjson[=false]: input as json
-h, --help[=false]: help for yaml -h, --help[=false]: help for yaml
-j, --tojson[=false]: output as json -j, --tojson[=false]: output as json
-t, --trim[=true]: trim yaml output -t, --trim[=true]: trim yaml output

3
coverage.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
go test -coverprofile=coverage.out && go tool cover -html=coverage.out

View File

@ -252,15 +252,15 @@
<ul class="md-nav__list" data-md-scrollfix> <ul class="md-nav__list" data-md-scrollfix>
<li class="md-nav__item"> <li class="md-nav__item">
<a href="#yaml2json" title="Yaml2json" class="md-nav__link"> <a href="#yaml-to-json" title="Yaml to Json" class="md-nav__link">
Yaml2json Yaml to Json
</a> </a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
<a href="#json2yaml" title="json2yaml" class="md-nav__link"> <a href="#json-to-yaml" title="Json to Yaml" class="md-nav__link">
json2yaml Json to Yaml
</a> </a>
</li> </li>
@ -293,15 +293,15 @@
<ul class="md-nav__list" data-md-scrollfix> <ul class="md-nav__list" data-md-scrollfix>
<li class="md-nav__item"> <li class="md-nav__item">
<a href="#yaml2json" title="Yaml2json" class="md-nav__link"> <a href="#yaml-to-json" title="Yaml to Json" class="md-nav__link">
Yaml2json Yaml to Json
</a> </a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
<a href="#json2yaml" title="json2yaml" class="md-nav__link"> <a href="#json-to-yaml" title="Json to Yaml" class="md-nav__link">
json2yaml Json to Yaml
</a> </a>
</li> </li>
@ -323,10 +323,39 @@
<h1>Convert</h1> <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> <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>Given a sample.yaml file of:</p>
<p>To read in json, use the --fromjson (or -J) flag. This can be used with any command.</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">{&quot;b&quot;:{&quot;c&quot;: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">{&quot;a&quot;:&quot;Easy! as one two three&quot;,&quot;b&quot;:{&quot;c&quot;:2,&quot;d&quot;:[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>

View File

@ -328,7 +328,7 @@
<h1 id="yaml">yaml</h1> <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> <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="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> <h3 id="get-the-source">get the source</h3>

View File

@ -2,12 +2,12 @@
"docs": [ "docs": [
{ {
"location": "/", "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" "title": "Install"
}, },
{ {
"location": "/#yaml", "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" "title": "yaml"
}, },
{ {
@ -22,7 +22,7 @@
}, },
{ {
"location": "/read/", "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" "title": "Read"
}, },
{ {
@ -57,7 +57,7 @@
}, },
{ {
"location": "/write/", "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" "title": "Write/Update"
}, },
{ {
@ -102,18 +102,18 @@
}, },
{ {
"location": "/convert/", "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" "title": "Convert"
}, },
{ {
"location": "/convert/#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.", "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": "Yaml2json" "title": "Yaml to Json"
}, },
{ {
"location": "/convert/#json2yaml", "location": "/convert/#json-to-yaml",
"text": "To read in json, use the --fromjson (or -J) flag. This can be used with any command.", "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": "json2yaml" "title": "Json to Yaml"
} }
] ]
} }

View File

@ -379,9 +379,10 @@
<h1>Read</h1> <h1>Read</h1>
<pre><code>yaml r &lt;yaml file&gt; &lt;path&gt; <pre><code>yaml r &lt;yaml_file|json_file&gt; &lt;path&gt;
</code></pre> </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> <h3 id="basic">Basic</h3>
<p>Given a sample.yaml file of:</p> <p>Given a sample.yaml file of:</p>
<pre><code class="yaml">b: <pre><code class="yaml">b:

View File

@ -4,7 +4,7 @@
<url> <url>
<loc>/</loc> <loc>/</loc>
<lastmod>2017-04-13</lastmod> <lastmod>2017-04-19</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -12,7 +12,7 @@
<url> <url>
<loc>/read/</loc> <loc>/read/</loc>
<lastmod>2017-04-13</lastmod> <lastmod>2017-04-19</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -20,7 +20,7 @@
<url> <url>
<loc>/write/</loc> <loc>/write/</loc>
<lastmod>2017-04-13</lastmod> <lastmod>2017-04-19</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -28,7 +28,7 @@
<url> <url>
<loc>/create/</loc> <loc>/create/</loc>
<lastmod>2017-04-13</lastmod> <lastmod>2017-04-19</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -36,7 +36,7 @@
<url> <url>
<loc>/convert/</loc> <loc>/convert/</loc>
<lastmod>2017-04-13</lastmod> <lastmod>2017-04-19</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>

View File

@ -365,9 +365,10 @@
<h1>Write/Update</h1> <h1>Write/Update</h1>
<pre><code>yaml w &lt;yaml file&gt; &lt;path&gt; &lt;new value&gt; <pre><code>yaml w &lt;yaml_file|json_file&gt; &lt;path&gt; &lt;new value&gt;
</code></pre> </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> <h3 id="to-stdout">To Stdout</h3>
<p>Given a sample.yaml file of:</p> <p>Given a sample.yaml file of:</p>
<pre><code class="yaml">b: <pre><code class="yaml">b:

View File

@ -5,19 +5,6 @@ import (
"github.com/mikefarah/yaml/Godeps/_workspace/src/gopkg.in/yaml.v2" "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 { func jsonToString(context interface{}) string {
out, err := json.Marshal(toJSON(context)) out, err := json.Marshal(toJSON(context))
if err != nil { if err != nil {
@ -26,27 +13,6 @@ func jsonToString(context interface{}) string {
return string(out) 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{} { func toJSON(context interface{}) interface{} {
switch context.(type) { switch context.(type) {
case []interface{}: case []interface{}:

View File

@ -1,35 +1,9 @@
package main package main
import ( import (
"encoding/json"
"fmt"
"os"
"testing" "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) { func TestJsonToString(t *testing.T) {
var data = parseData(` var data = parseData(`
--- ---
@ -48,13 +22,3 @@ b:
`) `)
assertResult(t, "{\"b\":[{\"item\":\"one\"},{\"item\":\"two\"}]}", jsonToString(data)) 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
}

View File

@ -17,3 +17,8 @@ extra:
link: 'https://github.com/mikefarah' link: 'https://github.com/mikefarah'
- type: 'linkedin' - type: 'linkedin'
link: 'https://www.linkedin.com/in/mike-farah-b5a75b2/' link: 'https://www.linkedin.com/in/mike-farah-b5a75b2/'
markdown_extensions:
- markdown_include.include:
base_path: mkdocs
- toc(permalink=true)

View File

@ -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. To convert output to json, use the --tojson (or -j) flag. This can be used with any command.
### json2yaml Given a sample.yaml file of:
To read in json, use the --fromjson (or -J) flag. This can be used with any command. ```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
```

View File

@ -1,7 +1,9 @@
``` ```
yaml r <yaml file> <path> yaml r <yaml_file|json_file> <path>
``` ```
{!snippets/works_with_json.md!}
### Basic ### Basic
Given a sample.yaml file of: Given a sample.yaml file of:
```yaml ```yaml

View 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)

View File

@ -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 ### To Stdout
Given a sample.yaml file of: Given a sample.yaml file of:

View File

@ -35,7 +35,6 @@ func main() {
var rootCmd = &cobra.Command{Use: "yaml"} var rootCmd = &cobra.Command{Use: "yaml"}
rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output") rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output")
rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json") 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.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode")
rootCmd.AddCommand(cmdRead, cmdWrite, cmdNew) rootCmd.AddCommand(cmdRead, cmdWrite, cmdNew)
rootCmd.Execute() rootCmd.Execute()
@ -247,12 +246,7 @@ func readData(filename string, parsedData interface{}, readAsJSON bool) {
rawData = readFile(filename) rawData = readFile(filename)
} }
var err interface{} err := yaml.Unmarshal([]byte(rawData), parsedData)
if readAsJSON {
fromJSONBytes([]byte(rawData), parsedData.(*map[interface{}]interface{}))
} else {
err = yaml.Unmarshal([]byte(rawData), parsedData)
}
if err != nil { if err != nil {
die("error parsing data: ", err) die("error parsing data: ", err)
} }