mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d75edfb67 | ||
|
|
037314af8a | ||
|
|
2a283b4ef7 | ||
|
|
7ef556f22b |
11
README.md
11
README.md
@@ -3,6 +3,12 @@ yaml is a lightweight and flexible command-line YAML processor
|
|||||||
|
|
||||||
The aim of the project is to be the [jq](https://github.com/stedolan/jq) or sed of yaml files.
|
The aim of the project is to be the [jq](https://github.com/stedolan/jq) or sed of yaml files.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
[Download latest binary](https://github.com/mikefarah/yaml/releases/latest) or alternatively:
|
||||||
|
```
|
||||||
|
go get github.com/mikefarah/yaml
|
||||||
|
```
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
- Written in portable go, so you can download a lovely dependency free binary
|
- Written in portable go, so you can download a lovely dependency free binary
|
||||||
- Deep read a yaml file with a given path
|
- Deep read a yaml file with a given path
|
||||||
@@ -11,11 +17,6 @@ The aim of the project is to be the [jq](https://github.com/stedolan/jq) or sed
|
|||||||
- Convert from json to yaml
|
- Convert from json to yaml
|
||||||
- Convert from yaml to json
|
- Convert from yaml to json
|
||||||
|
|
||||||
[Download latest binary](https://github.com/mikefarah/yaml/releases/latest) or alternatively:
|
|
||||||
```
|
|
||||||
go get github.com/mikefarah/yaml
|
|
||||||
```
|
|
||||||
|
|
||||||
## Read examples
|
## Read examples
|
||||||
```
|
```
|
||||||
yaml r <yaml file> <path>
|
yaml r <yaml file> <path>
|
||||||
|
|||||||
@@ -9,11 +9,25 @@ func write(context map[interface{}]interface{}, head string, tail []string, valu
|
|||||||
if len(tail) == 0 {
|
if len(tail) == 0 {
|
||||||
context[head] = value
|
context[head] = value
|
||||||
} else {
|
} else {
|
||||||
// e.g. if updating a.b.c, we need to get the 'b' map...
|
// e.g. if updating a.b.c, we need to get the 'b', this could be a map or an array
|
||||||
toUpdate := readMap(context, head, tail[0:len(tail)-1]).(map[interface{}]interface{})
|
var parent = readMap(context, head, tail[0:len(tail)-1])
|
||||||
// and then set the 'c' key.
|
switch parent.(type) {
|
||||||
key := (tail[len(tail)-1])
|
case map[interface{}]interface{}:
|
||||||
toUpdate[key] = value
|
toUpdate := parent.(map[interface{}]interface{})
|
||||||
|
// b is a map, update the key 'c' to the supplied value
|
||||||
|
key := (tail[len(tail)-1])
|
||||||
|
toUpdate[key] = value
|
||||||
|
case []interface{}:
|
||||||
|
toUpdate := parent.([]interface{})
|
||||||
|
// b is an array, update it at index 'c' to the supplied value
|
||||||
|
rawIndex := (tail[len(tail)-1])
|
||||||
|
index, err := strconv.ParseInt(rawIndex, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
die("Error accessing array: %v", err)
|
||||||
|
}
|
||||||
|
toUpdate[index] = value
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +72,7 @@ func recurse(value interface{}, head string, tail []string) interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func readArray(array []interface{}, head int64, tail []string) interface{} {
|
func readArray(array []interface{}, head int64, tail []string) interface{} {
|
||||||
if head > int64(len(array)) {
|
if head >= int64(len(array)) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -83,6 +83,17 @@ b:
|
|||||||
assertResult(t, nil, readMap(data, "b", []string{"d", "3"}))
|
assertResult(t, nil, readMap(data, "b", []string{"d", "3"}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadMap_with_array_out_of_bounds_by_1(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
---
|
||||||
|
b:
|
||||||
|
d:
|
||||||
|
- 3
|
||||||
|
- 4
|
||||||
|
`)
|
||||||
|
assertResult(t, nil, readMap(data, "b", []string{"d", "2"}))
|
||||||
|
}
|
||||||
|
|
||||||
func TestReadMap_with_array_splat(t *testing.T) {
|
func TestReadMap_with_array_splat(t *testing.T) {
|
||||||
var data = parseData(`
|
var data = parseData(`
|
||||||
e:
|
e:
|
||||||
@@ -108,6 +119,18 @@ b:
|
|||||||
assertResult(t, "4", b["c"].(string))
|
assertResult(t, "4", b["c"].(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWrite_array(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
b:
|
||||||
|
- aa
|
||||||
|
`)
|
||||||
|
|
||||||
|
write(data, "b", []string{"0"}, "bb")
|
||||||
|
|
||||||
|
b := data["b"].([]interface{})
|
||||||
|
assertResult(t, "bb", b[0].(string))
|
||||||
|
}
|
||||||
|
|
||||||
func TestWrite_with_no_tail(t *testing.T) {
|
func TestWrite_with_no_tail(t *testing.T) {
|
||||||
var data = parseData(`
|
var data = parseData(`
|
||||||
b:
|
b:
|
||||||
|
|||||||
Reference in New Issue
Block a user