mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Compare commits
5 Commits
key_ops
...
cswarth-do
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b887e23b3 | ||
|
|
a76b72e691 | ||
|
|
9509831cff | ||
|
|
e92180e89d | ||
|
|
a6ae33c3f1 |
4
.github/workflows/release.yml
vendored
4
.github/workflows/release.yml
vendored
@@ -68,7 +68,9 @@ jobs:
|
||||
|
||||
- name: Build and push image
|
||||
run: |
|
||||
IMAGE_VERSION="$(git describe --tags --abbrev=0)"
|
||||
IMAGE_V_VERSION="$(git describe --tags --abbrev=0)"
|
||||
IMAGE_VERSION=${IMAGE_V_VERSION:1}
|
||||
|
||||
SHORT_SHA1=$(git rev-parse --short HEAD)
|
||||
PLATFORMS="linux/amd64,linux/ppc64le,linux/arm64"
|
||||
echo "Building and pushing version ${IMAGE_VERSION} of image ${IMAGE_NAME}"
|
||||
|
||||
@@ -4,6 +4,7 @@ COPY scripts/devtools.sh /opt/devtools.sh
|
||||
|
||||
RUN set -e -x \
|
||||
&& /opt/devtools.sh
|
||||
ENV PATH=/go/bin:$PATH
|
||||
|
||||
# install mkdocs
|
||||
RUN set -ex \
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM mikefarah/yq:3
|
||||
FROM mikefarah/yq:4.2.0
|
||||
|
||||
COPY entrypoint.sh /entrypoint.sh
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ a: frog
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq eval 'select(. | documentIndex == 1)' sample.yml
|
||||
yq eval 'select(documentIndex == 1)' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
@@ -42,7 +42,7 @@ a: frog
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq eval '.a | ({"match": ., "doc": (. | documentIndex)})' sample.yml
|
||||
yq eval '.a | ({"match": ., "doc": documentIndex})' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
|
||||
## 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
|
||||
Set empty (default) quote style, note the usage of `...` to match keys too.
|
||||
Set empty (default) quote style, note the usage of `...` to match keys too. Note that there is a `--prettyPrint/-P` short flag for this.
|
||||
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
|
||||
@@ -49,7 +49,6 @@ var CreateMap = &OperationType{Type: "CREATE_MAP", NumArgs: 2, Precedence: 40, H
|
||||
var ShortPipe = &OperationType{Type: "SHORT_PIPE", NumArgs: 2, Precedence: 45, Handler: PipeOperator}
|
||||
|
||||
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 GetStyle = &OperationType{Type: "GET_STYLE", NumArgs: 0, Precedence: 50, Handler: GetStyleOperator}
|
||||
var GetTag = &OperationType{Type: "GET_TAG", NumArgs: 0, Precedence: 50, Handler: GetTagOperator}
|
||||
|
||||
@@ -17,7 +17,7 @@ var documentIndexScenarios = []expressionScenario{
|
||||
{
|
||||
description: "Filter by document index",
|
||||
document: "a: cat\n---\na: frog\n",
|
||||
expression: `select(. | documentIndex == 1)`,
|
||||
expression: `select(documentIndex == 1)`,
|
||||
expected: []string{
|
||||
"D1, P[], (doc)::a: frog\n",
|
||||
},
|
||||
@@ -25,7 +25,7 @@ var documentIndexScenarios = []expressionScenario{
|
||||
{
|
||||
description: "Print Document Index with matches",
|
||||
document: "a: cat\n---\na: frog\n",
|
||||
expression: `.a | ({"match": ., "doc": (. | documentIndex)})`,
|
||||
expression: `.a | ({"match": ., "doc": documentIndex})`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!map)::match: cat\ndoc: 0\n",
|
||||
"D0, P[], (!!map)::match: frog\ndoc: 1\n",
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
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,7 +182,6 @@ func initLexer() (*lex.Lexer, error) {
|
||||
lexer.Add([]byte(`,`), opToken(Union))
|
||||
lexer.Add([]byte(`:\s*`), opToken(CreateMap))
|
||||
lexer.Add([]byte(`length`), opToken(Length))
|
||||
lexer.Add([]byte(`keys`), opToken(Keys))
|
||||
lexer.Add([]byte(`sortKeys`), opToken(SortKeys))
|
||||
lexer.Add([]byte(`select`), opToken(Select))
|
||||
lexer.Add([]byte(`has`), opToken(Has))
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
{
|
||||
"a1": {
|
||||
"b": "cat",
|
||||
"c": "dog"
|
||||
},
|
||||
"a2": {
|
||||
"d": "cat",
|
||||
"e": "dog"
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
- increment version in version.go
|
||||
- increment version in snapcraft.yaml
|
||||
- increment version in github-action/Dockerfile
|
||||
- make sure local build passes
|
||||
- tag git with same version number
|
||||
- commit vX tag - this will trigger github actions
|
||||
- use github actions to publish docker and make github release
|
||||
- check github updated yq action in marketplace
|
||||
|
||||
- snapcraft
|
||||
- will auto create a candidate, test it works then promote
|
||||
|
||||
@@ -3,7 +3,12 @@
|
||||
set -o errexit
|
||||
set -o pipefail
|
||||
|
||||
./bin/golangci-lint run --timeout=5m
|
||||
if command -v golangci-lint &> /dev/null
|
||||
then
|
||||
golangci-lint run --timeout=5m
|
||||
else
|
||||
./bin/golangci-lint run --timeout=5m
|
||||
fi
|
||||
|
||||
# ./bin/golangci-lint \
|
||||
# --tests \
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
#!/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}
|
||||
@@ -1,21 +0,0 @@
|
||||
#!/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
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
#!/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