mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Compare commits
1 Commits
cswarth-do
...
key_ops
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f46c60f74d |
4
.github/workflows/release.yml
vendored
4
.github/workflows/release.yml
vendored
@@ -68,9 +68,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Build and push image
|
- name: Build and push image
|
||||||
run: |
|
run: |
|
||||||
IMAGE_V_VERSION="$(git describe --tags --abbrev=0)"
|
IMAGE_VERSION="$(git describe --tags --abbrev=0)"
|
||||||
IMAGE_VERSION=${IMAGE_V_VERSION:1}
|
|
||||||
|
|
||||||
SHORT_SHA1=$(git rev-parse --short HEAD)
|
SHORT_SHA1=$(git rev-parse --short HEAD)
|
||||||
PLATFORMS="linux/amd64,linux/ppc64le,linux/arm64"
|
PLATFORMS="linux/amd64,linux/ppc64le,linux/arm64"
|
||||||
echo "Building and pushing version ${IMAGE_VERSION} of image ${IMAGE_NAME}"
|
echo "Building and pushing version ${IMAGE_VERSION} of image ${IMAGE_NAME}"
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ COPY scripts/devtools.sh /opt/devtools.sh
|
|||||||
|
|
||||||
RUN set -e -x \
|
RUN set -e -x \
|
||||||
&& /opt/devtools.sh
|
&& /opt/devtools.sh
|
||||||
ENV PATH=/go/bin:$PATH
|
|
||||||
|
|
||||||
# install mkdocs
|
# install mkdocs
|
||||||
RUN set -ex \
|
RUN set -ex \
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM mikefarah/yq:4.2.0
|
FROM mikefarah/yq:3
|
||||||
|
|
||||||
COPY entrypoint.sh /entrypoint.sh
|
COPY entrypoint.sh /entrypoint.sh
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ a: frog
|
|||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
yq eval 'select(documentIndex == 1)' sample.yml
|
yq eval 'select(. | documentIndex == 1)' sample.yml
|
||||||
```
|
```
|
||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
@@ -42,7 +42,7 @@ a: frog
|
|||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
yq eval '.a | ({"match": ., "doc": documentIndex})' sample.yml
|
yq eval '.a | ({"match": ., "doc": (. | documentIndex)})' sample.yml
|
||||||
```
|
```
|
||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
38
pkg/yqlib/doc/Keys.md
Normal file
38
pkg/yqlib/doc/Keys.md
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
|
||||||
|
## Get Keys
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
a:
|
||||||
|
b: cat
|
||||||
|
c: dog
|
||||||
|
d: frog
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq eval '.a | keys' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
3
|
||||||
|
```
|
||||||
|
|
||||||
|
## Set key style
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
a:
|
||||||
|
b: cat
|
||||||
|
c: dog
|
||||||
|
d: frog
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq eval '(.a | keys) style = 'single'' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
a:
|
||||||
|
b: cat
|
||||||
|
c: dog
|
||||||
|
d: frog
|
||||||
|
```
|
||||||
|
|
||||||
@@ -146,7 +146,7 @@ will output
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Pretty print
|
## Pretty print
|
||||||
Set empty (default) quote style, note the usage of `...` to match keys too. Note that there is a `--prettyPrint/-P` short flag for this.
|
Set empty (default) quote style, note the usage of `...` to match keys too.
|
||||||
|
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ var CreateMap = &OperationType{Type: "CREATE_MAP", NumArgs: 2, Precedence: 40, H
|
|||||||
var ShortPipe = &OperationType{Type: "SHORT_PIPE", NumArgs: 2, Precedence: 45, Handler: PipeOperator}
|
var ShortPipe = &OperationType{Type: "SHORT_PIPE", NumArgs: 2, Precedence: 45, Handler: PipeOperator}
|
||||||
|
|
||||||
var Length = &OperationType{Type: "LENGTH", NumArgs: 0, Precedence: 50, Handler: LengthOperator}
|
var Length = &OperationType{Type: "LENGTH", NumArgs: 0, Precedence: 50, Handler: LengthOperator}
|
||||||
|
var Keys = &OperationType{Type: "KEYS", NumArgs: 0, Precedence: 50, Handler: KeysOperator}
|
||||||
var Collect = &OperationType{Type: "COLLECT", NumArgs: 0, Precedence: 50, Handler: CollectOperator}
|
var Collect = &OperationType{Type: "COLLECT", NumArgs: 0, Precedence: 50, Handler: CollectOperator}
|
||||||
var GetStyle = &OperationType{Type: "GET_STYLE", NumArgs: 0, Precedence: 50, Handler: GetStyleOperator}
|
var GetStyle = &OperationType{Type: "GET_STYLE", NumArgs: 0, Precedence: 50, Handler: GetStyleOperator}
|
||||||
var GetTag = &OperationType{Type: "GET_TAG", NumArgs: 0, Precedence: 50, Handler: GetTagOperator}
|
var GetTag = &OperationType{Type: "GET_TAG", NumArgs: 0, Precedence: 50, Handler: GetTagOperator}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ var documentIndexScenarios = []expressionScenario{
|
|||||||
{
|
{
|
||||||
description: "Filter by document index",
|
description: "Filter by document index",
|
||||||
document: "a: cat\n---\na: frog\n",
|
document: "a: cat\n---\na: frog\n",
|
||||||
expression: `select(documentIndex == 1)`,
|
expression: `select(. | documentIndex == 1)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D1, P[], (doc)::a: frog\n",
|
"D1, P[], (doc)::a: frog\n",
|
||||||
},
|
},
|
||||||
@@ -25,7 +25,7 @@ var documentIndexScenarios = []expressionScenario{
|
|||||||
{
|
{
|
||||||
description: "Print Document Index with matches",
|
description: "Print Document Index with matches",
|
||||||
document: "a: cat\n---\na: frog\n",
|
document: "a: cat\n---\na: frog\n",
|
||||||
expression: `.a | ({"match": ., "doc": documentIndex})`,
|
expression: `.a | ({"match": ., "doc": (. | documentIndex)})`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!map)::match: cat\ndoc: 0\n",
|
"D0, P[], (!!map)::match: cat\ndoc: 0\n",
|
||||||
"D0, P[], (!!map)::match: frog\ndoc: 1\n",
|
"D0, P[], (!!map)::match: frog\ndoc: 1\n",
|
||||||
|
|||||||
34
pkg/yqlib/operator_keys.go
Normal file
34
pkg/yqlib/operator_keys.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package yqlib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"container/list"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
yaml "gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func KeysOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
||||||
|
log.Debugf("-- KeyOperation")
|
||||||
|
var results = list.New()
|
||||||
|
|
||||||
|
for el := matchMap.Front(); el != nil; el = el.Next() {
|
||||||
|
candidate := el.Value.(*CandidateNode)
|
||||||
|
targetNode := UnwrapDoc(candidate.Node)
|
||||||
|
|
||||||
|
switch targetNode.Kind {
|
||||||
|
case yaml.MappingNode:
|
||||||
|
node := &yaml.Node{Kind: yaml.SequenceNode, Value: fmt.Sprintf("%v", length), Tag: "!!int"}
|
||||||
|
|
||||||
|
case yaml.SequenceNode:
|
||||||
|
length = len(targetNode.Content)
|
||||||
|
default:
|
||||||
|
length = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
node := &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%v", length), Tag: "!!int"}
|
||||||
|
lengthCand := &CandidateNode{Node: node, Document: candidate.Document, Path: candidate.Path}
|
||||||
|
results.PushBack(lengthCand)
|
||||||
|
}
|
||||||
|
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
88
pkg/yqlib/operator_keys_test.go
Normal file
88
pkg/yqlib/operator_keys_test.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package yqlib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var keyOperatorScenarios = []expressionScenario{
|
||||||
|
{
|
||||||
|
description: "Get Keys of map",
|
||||||
|
document: `{a: {b: cat, c: dog, d: frog}}`,
|
||||||
|
expression: `.a | keys`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[a], (!!seq)::- b\n- c\n- d\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `{a1: {b: cat, c: dog, d: frog}, a2: {e: cat, f: dog, g: frog}}`,
|
||||||
|
expression: `(.a1, .a2) | keys`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[a1], (!!seq)::- b\n- c\n- d\n",
|
||||||
|
"D0, P[a2], (!!seq)::- e\n- f\n- g\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Get Keys of array",
|
||||||
|
document: `{a: [0,1,2]}`,
|
||||||
|
expression: `.a | keys`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[a], (!!seq)::0\n",
|
||||||
|
"D0, P[a], (!!int)::1\n",
|
||||||
|
"D0, P[a], (!!int)::2\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Set key style",
|
||||||
|
document: `{a: {b: cat, c: dog, d: frog}}`,
|
||||||
|
expression: `(.a | keys | ..) style = 'single'`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!doc)::{a: {'b': cat, 'c': dog, 'd': frog}}\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Set key alias",
|
||||||
|
document: `{a: {b: cat, c: dog, d: frog}}`,
|
||||||
|
expression: `(.a | keys | .. | select(.=="b")) alias = 'boo'`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!doc)::{a: {*meow: cat, c: dog, d: frog}}\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Set key alias",
|
||||||
|
document: `{a: {b: cat, c: dog, d: frog}}`,
|
||||||
|
expression: `(.a | key("b")) alias = 'boo'`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!doc)::{a: {*meow: cat, c: dog, d: frog}}\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `{a: [0,1,2]}`,
|
||||||
|
expression: `(.a | keys) style = 'single'`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[a], (!!int)::0\n",
|
||||||
|
"D0, P[a], (!!int)::1\n",
|
||||||
|
"D0, P[a], (!!int)::2\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `a: cat`,
|
||||||
|
expression: `.a | keys`,
|
||||||
|
expected: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `{}`,
|
||||||
|
expression: `.a | keys`,
|
||||||
|
expected: []string{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKeyOperatorScenarios(t *testing.T) {
|
||||||
|
for _, tt := range keyOperatorScenarios {
|
||||||
|
testScenario(t, &tt)
|
||||||
|
}
|
||||||
|
documentScenarios(t, "Keys", keyOperatorScenarios)
|
||||||
|
}
|
||||||
@@ -182,6 +182,7 @@ func initLexer() (*lex.Lexer, error) {
|
|||||||
lexer.Add([]byte(`,`), opToken(Union))
|
lexer.Add([]byte(`,`), opToken(Union))
|
||||||
lexer.Add([]byte(`:\s*`), opToken(CreateMap))
|
lexer.Add([]byte(`:\s*`), opToken(CreateMap))
|
||||||
lexer.Add([]byte(`length`), opToken(Length))
|
lexer.Add([]byte(`length`), opToken(Length))
|
||||||
|
lexer.Add([]byte(`keys`), opToken(Keys))
|
||||||
lexer.Add([]byte(`sortKeys`), opToken(SortKeys))
|
lexer.Add([]byte(`sortKeys`), opToken(SortKeys))
|
||||||
lexer.Add([]byte(`select`), opToken(Select))
|
lexer.Add([]byte(`select`), opToken(Select))
|
||||||
lexer.Add([]byte(`has`), opToken(Has))
|
lexer.Add([]byte(`has`), opToken(Has))
|
||||||
|
|||||||
10
pkg/yqlib/test.yml
Normal file
10
pkg/yqlib/test.yml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"a1": {
|
||||||
|
"b": "cat",
|
||||||
|
"c": "dog"
|
||||||
|
},
|
||||||
|
"a2": {
|
||||||
|
"d": "cat",
|
||||||
|
"e": "dog"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
- increment version in version.go
|
- increment version in version.go
|
||||||
- increment version in snapcraft.yaml
|
- increment version in snapcraft.yaml
|
||||||
- increment version in github-action/Dockerfile
|
|
||||||
- make sure local build passes
|
- make sure local build passes
|
||||||
- tag git with same version number
|
- tag git with same version number
|
||||||
- commit vX tag - this will trigger github actions
|
- commit vX tag - this will trigger github actions
|
||||||
- use github actions to publish docker and make github release
|
- use github actions to publish docker and make github release
|
||||||
- check github updated yq action in marketplace
|
|
||||||
|
|
||||||
- snapcraft
|
- snapcraft
|
||||||
- will auto create a candidate, test it works then promote
|
- will auto create a candidate, test it works then promote
|
||||||
|
|||||||
@@ -3,12 +3,7 @@
|
|||||||
set -o errexit
|
set -o errexit
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
|
|
||||||
if command -v golangci-lint &> /dev/null
|
./bin/golangci-lint run --timeout=5m
|
||||||
then
|
|
||||||
golangci-lint run --timeout=5m
|
|
||||||
else
|
|
||||||
./bin/golangci-lint run --timeout=5m
|
|
||||||
fi
|
|
||||||
|
|
||||||
# ./bin/golangci-lint \
|
# ./bin/golangci-lint \
|
||||||
# --tests \
|
# --tests \
|
||||||
|
|||||||
12
scripts/publish-docker.sh
Executable file
12
scripts/publish-docker.sh
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -ex
|
||||||
|
VERSION="$(git describe --tags --abbrev=0)"
|
||||||
|
docker build \
|
||||||
|
--target production \
|
||||||
|
--build-arg VERSION=${VERSION} \
|
||||||
|
-t mikefarah/yq:latest \
|
||||||
|
-t mikefarah/yq:${VERSION} \
|
||||||
|
-t mikefarah/yq:4 \
|
||||||
|
.
|
||||||
|
|
||||||
|
trivy image mikefarah/yq:${VERSION}
|
||||||
21
scripts/release.sh
Executable file
21
scripts/release.sh
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -ex
|
||||||
|
GITHUB_TOKEN="${GITHUB_TOKEN:?missing required input \'GITHUB_TOKEN\'}"
|
||||||
|
|
||||||
|
CURRENT="$(git describe --tags --abbrev=0)"
|
||||||
|
PREVIOUS="$(git describe --tags --abbrev=0 --always "${CURRENT}"^)"
|
||||||
|
OWNER="mikefarah"
|
||||||
|
REPO="yq"
|
||||||
|
|
||||||
|
release() {
|
||||||
|
github-release release \
|
||||||
|
--user "$OWNER" \
|
||||||
|
--draft \
|
||||||
|
--repo "$REPO" \
|
||||||
|
--tag "$CURRENT"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
release
|
||||||
|
|
||||||
28
scripts/upload.sh
Executable file
28
scripts/upload.sh
Executable file
@@ -0,0 +1,28 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -ex
|
||||||
|
GITHUB_TOKEN="${GITHUB_TOKEN:?missing required input \'GITHUB_TOKEN\'}"
|
||||||
|
|
||||||
|
CURRENT="$(git describe --tags --abbrev=0)"
|
||||||
|
PREVIOUS="$(git describe --tags --abbrev=0 --always "${CURRENT}"^)"
|
||||||
|
OWNER="mikefarah"
|
||||||
|
REPO="yq"
|
||||||
|
|
||||||
|
upload() {
|
||||||
|
mkdir -p ./build-done
|
||||||
|
while IFS= read -r -d $'\0'; do
|
||||||
|
file=$REPLY
|
||||||
|
BINARY=$(basename "${file}")
|
||||||
|
echo "--> ${BINARY}"
|
||||||
|
github-release upload \
|
||||||
|
--replace \
|
||||||
|
--user "$OWNER" \
|
||||||
|
--repo "$REPO" \
|
||||||
|
--tag "$CURRENT" \
|
||||||
|
--name "${BINARY}" \
|
||||||
|
--file "$file"
|
||||||
|
mv "$file" "./build-done/${BINARY}"
|
||||||
|
done < <(find ./build -mindepth 1 -maxdepth 1 -print0)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
upload
|
||||||
Reference in New Issue
Block a user