Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yaml merge key handling #11

Merged
merged 10 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,9 @@ jobs:
[[ "${{ matrix.name }}" == windows-* ]] && ext=".exe"
bin="target/${{ matrix.target }}/release/yq${ext}"
strip "$bin" || true

#version=$(cat VERSION)
dst="yq-${{ matrix.target }}"
mkdir "$dst"

mkdir -p "target/release"
cp "$bin" "target/release/" # workaround for cargo-deb silliness with targets

cp "$bin" "$dst/"
#cp -r README.md LICENSE completions yq.1 "$dst/"
cp README.md LICENSE "$dst/"

- name: Archive (tar)
if: '! startsWith(matrix.name, ''windows-'')'
Expand Down Expand Up @@ -184,7 +176,6 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
generate_release_notes: true
#draft: true
fail_on_unmatched_files: true
files: |
yq-*.tar.xz
Expand Down
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
[profile.release]
lto = true
panic = "abort"
strip = "symbols"
#strip = "symbols"

[package.metadata.binstall]
pkg-url = "{ repo }/releases/download/{ version }/yq-{ target }{ archive-suffix }"
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ cargo binstall whyq
- arbitrary `jq` usage on yaml/toml input with same syntax (args passed along to `jq` with json converted input)
- drop-in replacement to [python-yq](https://kislyuk.github.io/yq/) (e.g. provides: yq)
- handles multidoc **yaml** input (vector of documents returned when multiple docs found)
- handles [yaml merge keys](https://yaml.org/type/merge.html) and expands yaml tags (via `serde_yaml`)
- handles **toml** input (from [Table](https://docs.rs/toml/latest/toml/#parsing-toml))
- unpacks yaml tags (input is [singleton mapped](https://docs.rs/serde_yaml/latest/serde_yaml/with/singleton_map/index.html) [recursively](https://docs.rs/serde_yaml/latest/serde_yaml/with/singleton_map_recursive/index.html))
- allows converting `jq` output to YAML (`-y`) or TOML (`-t`)
- uses <1MB in your CI image
- <1MB in binary size (for your small cloud CI images)

## YAML Input
Use as [jq](https://jqlang.github.io/jq/tutorial/) either via stdin:
Expand Down Expand Up @@ -129,4 +129,5 @@ If you pass on `-r` for raw output, then this will not be parseable as json.
- Only YAML/TOML input/output is supported (no XML).
- Shells out to `jq` (only supports what your jq version supports).
- Does not provide rich `-h` or `--help` output (assumes you can use `jq --help` or `man jq`).
- Does not preserve [YAML tags](https://yaml.org/spec/1.2-old/spec.html#id2764295) (input is [singleton mapped](https://docs.rs/serde_yaml/latest/serde_yaml/with/singleton_map/index.html) [recursively](https://docs.rs/serde_yaml/latest/serde_yaml/with/singleton_map_recursive/index.html)).
- Does not preserve [YAML tags](https://yaml.org/spec/1.2-old/spec.html#id2764295) (input is [singleton mapped](https://docs.rs/serde_yaml/latest/serde_yaml/with/singleton_map/index.html) [recursively](https://docs.rs/serde_yaml/latest/serde_yaml/with/singleton_map_recursive/index.html) and then [apply_merged](https://docs.rs/serde_yaml/latest/serde_yaml/value/enum.Value.html#method.apply_merge) before `jq`)
- Does [not support duplicate keys](https://github.com/clux/whyq/issues/14) in the input document
58 changes: 58 additions & 0 deletions test/circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
version: 2.1

definitions:
steps:
- step: &build_binary
name: Build binary
command: chmod a+w . && cargo build --release
- step: &versions
name: Version information
command: rustc --version; cargo --version; rustup --version

filters:
on_tag: &on_tag
tags:
only: /v[0-9]+(\.[0-9]+)*/
branches:
ignore: /.*/
on_every_commit: &on_every_commit
tags:
only: /.*/

jobs:
build:
docker:
- image: clux/muslrust:stable
resource_class: xlarge
environment:
IMAGE_NAME: whyq
steps:
- checkout
- run: *versions
- run: *build_binary
- run: echo versions

release:
docker:
- image: clux/muslrust:stable
resource_class: xlarge
steps:
- checkout
- run: *versions
- run: *build_binary
- upload:
source: "target/x86_64-unknown-linux-musl/release/${IMAGE_NAME}"
binary_name: "${IMAGE_NAME}"
version: "${CIRCLE_TAG}"
arch: "x86_64-unknown-linux-musl"

workflows:
version: 2
my_flow:
jobs:
- build:
filters:
<<: *on_every_commit
- release:
filters:
<<: *on_tag
10 changes: 9 additions & 1 deletion test/yq.test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@

run yq -i=toml '.dependencies.clap.features' -c < Cargo.toml
echo "$output" && echo "$output" | grep '["cargo","derive"]'
}
}

@test "yaml_merge" {
run yq '.workflows.my_flow.jobs[0].build' -c < test/circle.yml
echo "$output" && echo "$output" | grep '{"filters":{"tags":{"only":"/.*/"}}}'

run yq '.jobs.build.steps[1].run.name' -r < test/circle.yml
echo "$output" && echo "$output" | grep "Version information"
}
11 changes: 8 additions & 3 deletions yq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ impl Args {

let mut docs: Vec<serde_json::Value> = vec![];
for doc in yaml_de {
docs.push(singleton_map_recursive::deserialize(doc)?);
let json_value: serde_json::Value = {
let mut yaml_doc: serde_yaml::Value = singleton_map_recursive::deserialize(doc)?;
yaml_doc.apply_merge()?;
let yaml_ser = serde_yaml::to_string(&yaml_doc)?;
serde_yaml::from_str(&yaml_ser)?
};
docs.push(json_value);
}
debug!("found {} documents", docs.len());
// if there is 1 or 0 documents, do not return as nested documents
Expand All @@ -95,8 +101,7 @@ impl Args {
Self::try_parse_from(["cmd", "-h"])?;
std::process::exit(2);
}
let data = std::fs::read_to_string(f)?;
data
std::fs::read_to_string(f)?
} else {
Self::try_parse_from(["cmd", "-h"])?;
std::process::exit(2);
Expand Down
Loading