mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Compare commits
16 Commits
new-merge2
...
3.4.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6afc2e9189 | ||
|
|
996ee0b433 | ||
|
|
bb9cb0c60e | ||
|
|
a125495eec | ||
|
|
7fa2835e13 | ||
|
|
e0f5cb3c59 | ||
|
|
87550b7fe5 | ||
|
|
5554301c29 | ||
|
|
3b0aaac626 | ||
|
|
65cb472604 | ||
|
|
fbba38c9b7 | ||
|
|
e5948c4f16 | ||
|
|
4eaadf98d0 | ||
|
|
eedbb0a99f | ||
|
|
7dabc57b65 | ||
|
|
fcd3a90f67 |
@@ -12,7 +12,7 @@ RUN CGO_ENABLED=0 make local build
|
||||
|
||||
# Choose alpine as a base image to make this useful for CI, as many
|
||||
# CI tools expect an interactive shell inside the container
|
||||
FROM alpine:3.8 as production
|
||||
FROM alpine:3.12 as production
|
||||
|
||||
COPY --from=builder /go/src/mikefarah/yq/yq /usr/bin/yq
|
||||
RUN chmod +x /usr/bin/yq
|
||||
|
||||
83
README.md
83
README.md
@@ -7,9 +7,6 @@ a lightweight and portable command-line YAML processor
|
||||
|
||||
The aim of the project is to be the [jq](https://github.com/stedolan/jq) or sed of yaml files.
|
||||
|
||||
## New version!
|
||||
V3 is officially out - if you've been using v2 and want/need to upgrade, checkout the [upgrade guide](https://mikefarah.gitbook.io/yq/upgrading-from-v2).
|
||||
|
||||
## Install
|
||||
|
||||
### [Download the latest binary](https://github.com/mikefarah/yq/releases/latest)
|
||||
@@ -19,21 +16,6 @@ V3 is officially out - if you've been using v2 and want/need to upgrade, checkou
|
||||
brew install yq
|
||||
```
|
||||
|
||||
### Windows:
|
||||
```
|
||||
choco install yq
|
||||
```
|
||||
Supported by @chillum (https://chocolatey.org/packages/yq)
|
||||
|
||||
### Alpine Linux
|
||||
- Enable community repo by adding ```$MIRROR/alpine/v$VERSION/community``` to ```/etc/apk/repositories```
|
||||
- Update database index with ```apk update```
|
||||
- Install yq with ```apk add yq```
|
||||
|
||||
Supported by Tuan Hoang
|
||||
https://pkgs.alpinelinux.org/package/edge/community/x86/yq
|
||||
|
||||
|
||||
### Ubuntu and other Linux distros supporting `snap` packages:
|
||||
```
|
||||
snap install yq
|
||||
@@ -57,29 +39,27 @@ sudo mv /etc/myfile.tmp /etc/myfile
|
||||
rm /etc/myfile.tmp
|
||||
```
|
||||
|
||||
### On Ubuntu 16.04 or higher from Debian package:
|
||||
```sh
|
||||
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys CC86BB64
|
||||
sudo add-apt-repository ppa:rmescandon/yq
|
||||
sudo apt update
|
||||
sudo apt install yq -y
|
||||
```
|
||||
Supported by @rmescandon (https://launchpad.net/~rmescandon/+archive/ubuntu/yq)
|
||||
### wget
|
||||
|
||||
### Go Get:
|
||||
```
|
||||
GO111MODULE=on go get github.com/mikefarah/yq/v3
|
||||
Use wget to download the pre-compiled binaries:
|
||||
|
||||
```bash
|
||||
wget https://github.com/mikefarah/yq/releases/download/{VERSION}/{BINARY} -O /usr/bin/yq &&\
|
||||
chmod +x /usr/bin/yq
|
||||
```
|
||||
|
||||
## Run with Docker
|
||||
For instance, VERSION=3.4.0 and BINARY=yq_linux_amd64
|
||||
|
||||
Oneshot use:
|
||||
|
||||
### Run with Docker
|
||||
|
||||
#### Oneshot use:
|
||||
|
||||
```bash
|
||||
docker run --rm -v "${PWD}":/workdir mikefarah/yq yq [flags] <command> FILE...
|
||||
```
|
||||
|
||||
Run commands interactively:
|
||||
#### Run commands interactively:
|
||||
|
||||
```bash
|
||||
docker run --rm -it -v "${PWD}":/workdir mikefarah/yq sh
|
||||
@@ -93,6 +73,39 @@ yq() {
|
||||
}
|
||||
```
|
||||
|
||||
### Go Get:
|
||||
```
|
||||
GO111MODULE=on go get github.com/mikefarah/yq/v3
|
||||
```
|
||||
|
||||
## Community Supported Installation methods
|
||||
As these are supported by the community :heart: - however, they may be out of date with the officially supported releases.
|
||||
|
||||
|
||||
### Windows:
|
||||
```
|
||||
choco install yq
|
||||
```
|
||||
Supported by @chillum (https://chocolatey.org/packages/yq)
|
||||
|
||||
### Alpine Linux
|
||||
- Enable edge/community repo by adding ```$MIRROR/alpine/edge/community``` to ```/etc/apk/repositories```
|
||||
- Update database index with ```apk update```
|
||||
- Install yq with ```apk add yq```
|
||||
|
||||
Supported by Tuan Hoang
|
||||
https://pkgs.alpinelinux.org/package/edge/community/x86/yq
|
||||
|
||||
|
||||
### On Ubuntu 16.04 or higher from Debian package:
|
||||
```sh
|
||||
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys CC86BB64
|
||||
sudo add-apt-repository ppa:rmescandon/yq
|
||||
sudo apt update
|
||||
sudo apt install yq -y
|
||||
```
|
||||
Supported by @rmescandon (https://launchpad.net/~rmescandon/+archive/ubuntu/yq)
|
||||
|
||||
## Features
|
||||
- Written in portable go, so you can download a lovely dependency free binary
|
||||
- [Colorize the output](https://mikefarah.gitbook.io/yq/usage/output-format#colorize-output)
|
||||
@@ -145,5 +158,9 @@ Flags:
|
||||
Use "yq [command] --help" for more information about a command.
|
||||
```
|
||||
|
||||
## Known Issues
|
||||
## Upgrade from V2
|
||||
If you've been using v2 and want/need to upgrade, checkout the [upgrade guide](https://mikefarah.gitbook.io/yq/upgrading-from-v2).
|
||||
|
||||
## Known Issues / Missing Features
|
||||
- `yq` attempts to preserve comment positions and whitespace as much as possible, but it does not handle all scenarios (see https://github.com/go-yaml/yaml/tree/v3 for details)
|
||||
- You cannot (yet) select multiple paths/keys from the yaml to be printed out (https://github.com/mikefarah/yq/issues/287)
|
||||
|
||||
@@ -339,6 +339,19 @@ func TestMergeOverwriteArraysCmd(t *testing.T) {
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
func TestMergeUpdateArraysCmd(t *testing.T) {
|
||||
cmd := getRootCommand()
|
||||
result := test.RunCmd(cmd, "merge -x --arrays=update ../examples/sample_array.yaml ../examples/sample_array_2.yaml")
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `- 4
|
||||
- 5
|
||||
- 3
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
func TestMergeCmd_Multi(t *testing.T) {
|
||||
cmd := getRootCommand()
|
||||
result := test.RunCmd(cmd, "merge -d1 ../examples/multiple_docs_small.yaml ../examples/data1.yaml")
|
||||
|
||||
102
cmd/read_test.go
102
cmd/read_test.go
@@ -98,8 +98,8 @@ func TestReadOutputJsonNonStringKeysCmd(t *testing.T) {
|
||||
|
||||
content := `
|
||||
true: true
|
||||
5:
|
||||
null:
|
||||
5:
|
||||
null:
|
||||
0.1: deeply
|
||||
false: things`
|
||||
filename := test.WriteTempYamlFile(content)
|
||||
@@ -111,7 +111,7 @@ true: true
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `{"5":{"null":{"0.1":"deeply","false":"things"}},"true":true}
|
||||
expectedOutput := `{"true":true,"5":{"null":{"0.1":"deeply","false":"things"}}}
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
@@ -214,7 +214,7 @@ func TestReadArrayLengthCmd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadArrayLengthDeepCmd(t *testing.T) {
|
||||
content := `holder:
|
||||
content := `holder:
|
||||
- things
|
||||
- whatever
|
||||
`
|
||||
@@ -230,12 +230,12 @@ func TestReadArrayLengthDeepCmd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadArrayLengthDeepMultipleCmd(t *testing.T) {
|
||||
content := `holderA:
|
||||
content := `holderA:
|
||||
- things
|
||||
- whatever
|
||||
skipMe:
|
||||
- yep
|
||||
holderB:
|
||||
holderB:
|
||||
- other things
|
||||
- cool
|
||||
`
|
||||
@@ -294,10 +294,10 @@ func TestReadCollectArrayCmd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadArrayLengthDeepMultipleWithPathCmd(t *testing.T) {
|
||||
content := `holderA:
|
||||
content := `holderA:
|
||||
- things
|
||||
- whatever
|
||||
holderB:
|
||||
holderB:
|
||||
- other things
|
||||
- cool
|
||||
`
|
||||
@@ -328,7 +328,7 @@ dog: bark
|
||||
}
|
||||
|
||||
func TestReadObjectLengthDeepCmd(t *testing.T) {
|
||||
content := `holder:
|
||||
content := `holder:
|
||||
cat: meow
|
||||
dog: bark
|
||||
`
|
||||
@@ -344,10 +344,10 @@ func TestReadObjectLengthDeepCmd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadObjectLengthDeepMultipleCmd(t *testing.T) {
|
||||
content := `holderA:
|
||||
content := `holderA:
|
||||
cat: meow
|
||||
dog: bark
|
||||
holderB:
|
||||
holderB:
|
||||
elephant: meow
|
||||
zebra: bark
|
||||
`
|
||||
@@ -363,10 +363,10 @@ holderB:
|
||||
}
|
||||
|
||||
func TestReadObjectLengthDeepMultipleWithPathsCmd(t *testing.T) {
|
||||
content := `holderA:
|
||||
content := `holderA:
|
||||
cat: meow
|
||||
dog: bark
|
||||
holderB:
|
||||
holderB:
|
||||
elephant: meow
|
||||
zebra: bark
|
||||
`
|
||||
@@ -422,7 +422,7 @@ func TestReadSingleQuotedStringCmd(t *testing.T) {
|
||||
|
||||
func TestReadQuotedMultinlineStringCmd(t *testing.T) {
|
||||
content := `test: |
|
||||
abcdefg
|
||||
abcdefg
|
||||
hijklmno
|
||||
`
|
||||
filename := test.WriteTempYamlFile(content)
|
||||
@@ -433,7 +433,7 @@ func TestReadQuotedMultinlineStringCmd(t *testing.T) {
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `abcdefg
|
||||
expectedOutput := `abcdefg
|
||||
hijklmno
|
||||
|
||||
`
|
||||
@@ -442,7 +442,7 @@ hijklmno
|
||||
|
||||
func TestReadQuotedMultinlineNoNewLineStringCmd(t *testing.T) {
|
||||
content := `test: |-
|
||||
abcdefg
|
||||
abcdefg
|
||||
hijklmno
|
||||
`
|
||||
filename := test.WriteTempYamlFile(content)
|
||||
@@ -453,7 +453,7 @@ func TestReadQuotedMultinlineNoNewLineStringCmd(t *testing.T) {
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `abcdefg
|
||||
expectedOutput := `abcdefg
|
||||
hijklmno
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
@@ -583,7 +583,7 @@ func TestReadMergeAnchorsExplodeJsonCmd(t *testing.T) {
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `{"bar":{"b":2,"c":"oldbar","thing":"coconut"},"foo":{"a":"original","thing":"coolasdf","thirsty":"yep"},"foobar":{"a":"original","c":3,"thing":"ice","thirsty":"yep","thirty":"well beyond"},"foobarList":{"a":"original","b":2,"c":"newbar","thing":"coconut","thirsty":"yep"}}
|
||||
expectedOutput := `{"foo":{"a":"original","thing":"coolasdf","thirsty":"yep"},"bar":{"b":2,"thing":"coconut","c":"oldbar"},"foobarList":{"c":"newbar","b":2,"thing":"coconut","a":"original","thirsty":"yep"},"foobar":{"thirty":"well beyond","thing":"ice","c":3,"a":"original","thirsty":"yep"}}
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
@@ -617,6 +617,44 @@ pointer: *value-pointer`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
func TestReadMergeAnchorsExplodeMissingCmd(t *testing.T) {
|
||||
content := `a:
|
||||
<<: &anchor
|
||||
c: d
|
||||
e: f
|
||||
`
|
||||
filename := test.WriteTempYamlFile(content)
|
||||
defer test.RemoveTempYamlFile(filename)
|
||||
|
||||
cmd := getRootCommand()
|
||||
result := test.RunCmd(cmd, fmt.Sprintf("read -X %s", filename))
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `a:
|
||||
c: d
|
||||
e: f
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
func TestReadMergeAnchorsExplodeKeyCmd(t *testing.T) {
|
||||
content := `name: &nameField Mike
|
||||
*nameField: Great Guy`
|
||||
filename := test.WriteTempYamlFile(content)
|
||||
defer test.RemoveTempYamlFile(filename)
|
||||
|
||||
cmd := getRootCommand()
|
||||
result := test.RunCmd(cmd, fmt.Sprintf("read -X %s", filename))
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `name: Mike
|
||||
Mike: Great Guy
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
func TestReadMergeAnchorsExplodeSimpleArrayCmd(t *testing.T) {
|
||||
content := `- things`
|
||||
filename := test.WriteTempYamlFile(content)
|
||||
@@ -872,9 +910,9 @@ func TestReadEmptyContentCmd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadEmptyNodesPrintPathCmd(t *testing.T) {
|
||||
content := `map:
|
||||
content := `map:
|
||||
that: {}
|
||||
array:
|
||||
array:
|
||||
great: []
|
||||
null:
|
||||
indeed: ~`
|
||||
@@ -924,6 +962,7 @@ b:
|
||||
value: 3
|
||||
- name: sam
|
||||
value: 4
|
||||
ab: must appear last
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
@@ -963,6 +1002,7 @@ b:
|
||||
value: 3
|
||||
- name: sam
|
||||
value: 4
|
||||
ab: must appear last
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
@@ -1231,7 +1271,7 @@ func TestReadBadDataCmd(t *testing.T) {
|
||||
|
||||
func TestReadDeepFromRootCmd(t *testing.T) {
|
||||
content := `state:
|
||||
country:
|
||||
country:
|
||||
city: foo
|
||||
`
|
||||
filename := test.WriteTempYamlFile(content)
|
||||
@@ -1401,3 +1441,23 @@ func TestReadFindValueDeepObjectCmd(t *testing.T) {
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
func TestReadKeepsKeyOrderInJson(t *testing.T) {
|
||||
const content = `{
|
||||
"z": "One",
|
||||
"a": 1,
|
||||
"w": ["a", "r"],
|
||||
"u": {"d": "o", "0": 11.5},
|
||||
}`
|
||||
filename := test.WriteTempYamlFile(content)
|
||||
defer test.RemoveTempYamlFile(filename)
|
||||
cmd := getRootCommand()
|
||||
result := test.RunCmd(cmd, fmt.Sprintf("r -j %s", filename))
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
|
||||
expectedOutput := `{"z":"One","a":1,"w":["a","r"],"u":{"d":"o","0":11.5}}
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ func New() *cobra.Command {
|
||||
return nil
|
||||
},
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
cmd.SetOut(cmd.OutOrStdout())
|
||||
var format = logging.MustStringFormatter(
|
||||
`%{color}%{time:15:04:05} %{shortfunc} [%{level:.4s}]%{color:reset} %{message}`,
|
||||
)
|
||||
|
||||
21
cmd/utils.go
21
cmd/utils.go
@@ -163,6 +163,9 @@ func setIfNotThere(node *yaml.Node, key string, value *yaml.Node) {
|
||||
}
|
||||
|
||||
func applyAlias(node *yaml.Node, alias *yaml.Node) {
|
||||
if alias == nil {
|
||||
return
|
||||
}
|
||||
for index := 0; index < len(alias.Content); index = index + 2 {
|
||||
keyNode := alias.Content[index]
|
||||
log.Debugf("applying alias key %v", keyNode.Value)
|
||||
@@ -185,12 +188,14 @@ func explodeNode(node *yaml.Node) error {
|
||||
return nil
|
||||
case yaml.AliasNode:
|
||||
log.Debugf("its an alias!")
|
||||
node.Kind = node.Alias.Kind
|
||||
node.Style = node.Alias.Style
|
||||
node.Tag = node.Alias.Tag
|
||||
node.Content = node.Alias.Content
|
||||
node.Value = node.Alias.Value
|
||||
node.Alias = nil
|
||||
if node.Alias != nil {
|
||||
node.Kind = node.Alias.Kind
|
||||
node.Style = node.Alias.Style
|
||||
node.Tag = node.Alias.Tag
|
||||
node.Content = node.Alias.Content
|
||||
node.Value = node.Alias.Value
|
||||
node.Alias = nil
|
||||
}
|
||||
return nil
|
||||
case yaml.MappingNode:
|
||||
for index := 0; index < len(node.Content); index = index + 2 {
|
||||
@@ -202,6 +207,10 @@ func explodeNode(node *yaml.Node) error {
|
||||
if errorInContent != nil {
|
||||
return errorInContent
|
||||
}
|
||||
errorInContent = explodeNode(keyNode)
|
||||
if errorInContent != nil {
|
||||
return errorInContent
|
||||
}
|
||||
} else {
|
||||
if valueNode.Kind == yaml.SequenceNode {
|
||||
log.Debugf("an alias merge list!")
|
||||
|
||||
@@ -15,7 +15,7 @@ yq v - # reads from stdin
|
||||
`,
|
||||
RunE: validateProperty,
|
||||
SilenceUsage: true,
|
||||
SilenceErrors: true,
|
||||
SilenceErrors: false,
|
||||
}
|
||||
cmdRead.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
|
||||
return cmdRead
|
||||
|
||||
@@ -11,7 +11,7 @@ var (
|
||||
GitDescribe string
|
||||
|
||||
// Version is main version number that is being run at the moment.
|
||||
Version = "3.3.4"
|
||||
Version = "3.4.1"
|
||||
|
||||
// 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
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"a":"Easy! as one two three","b":{"c":2,"d":[3,4],"e":[{"name":"fred","value":3},{"name":"sam","value":4}]}}
|
||||
{"a":"Easy! as one two three","b":{"c":2,"d":[3,4],"e":[{"name":"fred","value":3},{"name":"sam","value":4}]},"ab":"must appear last"}
|
||||
|
||||
@@ -3,6 +3,7 @@ package yqlib
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
yaml "gopkg.in/yaml.v3"
|
||||
@@ -87,7 +88,7 @@ func NewJsonEncoder(destination io.Writer, prettyPrint bool, indent int) Encoder
|
||||
}
|
||||
|
||||
func (je *jsonEncoder) Encode(node *yaml.Node) error {
|
||||
var dataBucket interface{}
|
||||
var dataBucket orderedMap
|
||||
// firstly, convert all map keys to strings
|
||||
mapKeysToStrings(node)
|
||||
errorDecoding := node.Decode(&dataBucket)
|
||||
@@ -96,3 +97,153 @@ func (je *jsonEncoder) Encode(node *yaml.Node) error {
|
||||
}
|
||||
return je.encoder.Encode(dataBucket)
|
||||
}
|
||||
|
||||
// orderedMap allows to marshal and unmarshal JSON and YAML values keeping the
|
||||
// order of keys and values in a map or an object.
|
||||
type orderedMap struct {
|
||||
// if this is an object, kv != nil. If this is not an object, kv == nil.
|
||||
kv []orderedMapKV
|
||||
altVal interface{}
|
||||
}
|
||||
|
||||
type orderedMapKV struct {
|
||||
K string
|
||||
V orderedMap
|
||||
}
|
||||
|
||||
func (o *orderedMap) UnmarshalJSON(data []byte) error {
|
||||
switch data[0] {
|
||||
case '{':
|
||||
// initialise so that even if the object is empty it is not nil
|
||||
o.kv = []orderedMapKV{}
|
||||
|
||||
// create decoder
|
||||
dec := json.NewDecoder(bytes.NewReader(data))
|
||||
_, err := dec.Token() // open object
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// cycle through k/v
|
||||
var tok json.Token
|
||||
for tok, err = dec.Token(); err != io.EOF; tok, err = dec.Token() {
|
||||
// we can expect two types: string or Delim. Delim automatically means
|
||||
// that it is the closing bracket of the object, whereas string means
|
||||
// that there is another key.
|
||||
if _, ok := tok.(json.Delim); ok {
|
||||
break
|
||||
}
|
||||
kv := orderedMapKV{
|
||||
K: tok.(string),
|
||||
}
|
||||
if err := dec.Decode(&kv.V); err != nil {
|
||||
return err
|
||||
}
|
||||
o.kv = append(o.kv, kv)
|
||||
}
|
||||
// unexpected error
|
||||
if err != nil && err != io.EOF {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
case '[':
|
||||
var arr []orderedMap
|
||||
return json.Unmarshal(data, &arr)
|
||||
}
|
||||
|
||||
return json.Unmarshal(data, &o.altVal)
|
||||
}
|
||||
|
||||
func (o orderedMap) MarshalJSON() ([]byte, error) {
|
||||
if o.kv == nil {
|
||||
return json.Marshal(o.altVal)
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
enc := json.NewEncoder(buf)
|
||||
buf.WriteByte('{')
|
||||
for idx, el := range o.kv {
|
||||
if err := enc.Encode(el.K); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf.WriteByte(':')
|
||||
if err := enc.Encode(el.V); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if idx != len(o.kv)-1 {
|
||||
buf.WriteByte(',')
|
||||
}
|
||||
}
|
||||
buf.WriteByte('}')
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (o *orderedMap) UnmarshalYAML(node *yaml.Node) error {
|
||||
switch node.Kind {
|
||||
case yaml.DocumentNode:
|
||||
if len(node.Content) == 0 {
|
||||
return nil
|
||||
}
|
||||
return o.UnmarshalYAML(node.Content[0])
|
||||
case yaml.AliasNode:
|
||||
return o.UnmarshalYAML(node.Alias)
|
||||
case yaml.ScalarNode:
|
||||
return node.Decode(&o.altVal)
|
||||
case yaml.MappingNode:
|
||||
// set kv to non-nil
|
||||
o.kv = []orderedMapKV{}
|
||||
for i := 0; i < len(node.Content); i += 2 {
|
||||
var key string
|
||||
var val orderedMap
|
||||
if err := node.Content[i].Decode(&key); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := node.Content[i+1].Decode(&val); err != nil {
|
||||
return err
|
||||
}
|
||||
o.kv = append(o.kv, orderedMapKV{
|
||||
K: key,
|
||||
V: val,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
case yaml.SequenceNode:
|
||||
var res []orderedMap
|
||||
if err := node.Decode(&res); err != nil {
|
||||
return err
|
||||
}
|
||||
o.altVal = res
|
||||
o.kv = nil
|
||||
return nil
|
||||
case 0:
|
||||
// null
|
||||
o.kv = nil
|
||||
o.altVal = nil
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("orderedMap: invalid yaml node")
|
||||
}
|
||||
}
|
||||
|
||||
func (o *orderedMap) MarshalYAML() (interface{}, error) {
|
||||
// fast path: kv is nil, use altVal
|
||||
if o.kv == nil {
|
||||
return o.altVal, nil
|
||||
}
|
||||
content := make([]*yaml.Node, 0, len(o.kv)*2)
|
||||
for _, val := range o.kv {
|
||||
n := new(yaml.Node)
|
||||
if err := n.Encode(val.V); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content = append(content, &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: "!!str",
|
||||
Value: val.K,
|
||||
}, n)
|
||||
}
|
||||
return &yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Tag: "!!map",
|
||||
Content: content,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
- tag git with same version number
|
||||
- make sure local build passes
|
||||
- push tag to git
|
||||
- 3.4.0, v3
|
||||
- git push --tags
|
||||
- make local xcompile (builds binaries for all platforms)
|
||||
|
||||
|
||||
@@ -32,5 +32,5 @@ upload() {
|
||||
done < <(find ./build -mindepth 1 -maxdepth 1 -print0)
|
||||
}
|
||||
|
||||
release
|
||||
# release
|
||||
upload
|
||||
|
||||
@@ -11,3 +11,4 @@ CGO_ENABLED=0 gox -ldflags "${LDFLAGS}" -os=linux -output="build/yq_{{.OS}}_{{.
|
||||
cd build
|
||||
rhash -r -a . -P -o checksums
|
||||
|
||||
rhash --list-hashes > checksums_hashes_order
|
||||
@@ -1,5 +1,5 @@
|
||||
name: yq
|
||||
version: '3.3.4'
|
||||
version: '3.4.1'
|
||||
summary: A lightweight and portable command-line YAML processor
|
||||
description: |
|
||||
The aim of the project is to be the jq or sed of yaml files.
|
||||
|
||||
Reference in New Issue
Block a user