diff --git a/README.md b/README.md index 09d9d65e..84e4fd36 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/coverage.sh b/coverage.sh new file mode 100755 index 00000000..8140f3bc --- /dev/null +++ b/coverage.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +go test -coverprofile=coverage.out && go tool cover -html=coverage.out \ No newline at end of file diff --git a/docs/convert/index.html b/docs/convert/index.html index be73f134..20d4de99 100644 --- a/docs/convert/index.html +++ b/docs/convert/index.html @@ -252,15 +252,15 @@
To convert output to json, use the --tojson (or -j) flag. This can be used with any command.
-To read in json, use the --fromjson (or -J) flag. This can be used with any command.
+Given a sample.yaml file of:
+b:
+ c: 2
+
+
+then
+yaml r -j sample.yaml b.c
+
+
+will output
+{"b":{"c":2}}
+
+
+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
+b:
+ c: 2
+ d:
+ - 3
+ - 4
+
diff --git a/docs/index.html b/docs/index.html
index 300d12cd..94695066 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -328,7 +328,7 @@
yaml is a lightweight and flexible command-line YAML processor
+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.
yaml r <yaml file> <path>
+ yaml r <yaml_file|json_file> <path>
+This command can take a json file as input too, and will output yaml unless specified to export as json (-j)
Basic
Given a sample.yaml file of:
b:
diff --git a/docs/sitemap.xml b/docs/sitemap.xml
index 6516b614..d62befd4 100644
--- a/docs/sitemap.xml
+++ b/docs/sitemap.xml
@@ -4,7 +4,7 @@
/
- 2017-04-13
+ 2017-04-19
daily
@@ -12,7 +12,7 @@
/read/
- 2017-04-13
+ 2017-04-19
daily
@@ -20,7 +20,7 @@
/write/
- 2017-04-13
+ 2017-04-19
daily
@@ -28,7 +28,7 @@
/create/
- 2017-04-13
+ 2017-04-19
daily
@@ -36,7 +36,7 @@
/convert/
- 2017-04-13
+ 2017-04-19
daily
diff --git a/docs/write/index.html b/docs/write/index.html
index 89b7b15b..1d573c8a 100644
--- a/docs/write/index.html
+++ b/docs/write/index.html
@@ -365,9 +365,10 @@
Write/Update
- yaml w <yaml file> <path> <new value>
+ yaml w <yaml_file|json_file> <path> <new value>
+This command can take a json file as input too, and will output yaml unless specified to export as json (-j)
To Stdout
Given a sample.yaml file of:
b:
diff --git a/json_converter.go b/json_converter.go
index 893ca798..98887f53 100644
--- a/json_converter.go
+++ b/json_converter.go
@@ -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{}:
diff --git a/json_converter_test.go b/json_converter_test.go
index 984fd10c..0a1adfb2 100644
--- a/json_converter_test.go
+++ b/json_converter_test.go
@@ -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
-}
diff --git a/mkdocs.yml b/mkdocs.yml
index 7ba1b5ba..39c7c435 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -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)
\ No newline at end of file
diff --git a/mkdocs/convert.md b/mkdocs/convert.md
index 2f369c4e..0d92c709 100644
--- a/mkdocs/convert.md
+++ b/mkdocs/convert.md
@@ -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
+```
+
diff --git a/mkdocs/read.md b/mkdocs/read.md
index 220c5b9f..b9a4bbdd 100644
--- a/mkdocs/read.md
+++ b/mkdocs/read.md
@@ -1,7 +1,9 @@
```
-yaml r
+yaml r
```
+{!snippets/works_with_json.md!}
+
### Basic
Given a sample.yaml file of:
```yaml
diff --git a/mkdocs/snippets/works_with_json.md b/mkdocs/snippets/works_with_json.md
new file mode 100644
index 00000000..8786c045
--- /dev/null
+++ b/mkdocs/snippets/works_with_json.md
@@ -0,0 +1 @@
+This command can take a json file as input too, and will output yaml unless specified to export as json (-j)
diff --git a/mkdocs/write.md b/mkdocs/write.md
index 7a8c7692..4d51c1d9 100644
--- a/mkdocs/write.md
+++ b/mkdocs/write.md
@@ -1,6 +1,7 @@
```
-yaml w
+yaml w
```
+{!snippets/works_with_json.md!}
### To Stdout
Given a sample.yaml file of:
diff --git a/yaml.go b/yaml.go
index f90d47bf..af62ac70 100644
--- a/yaml.go
+++ b/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)
}