diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 73f543c..9bb3c72 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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-'')' @@ -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 diff --git a/Cargo.lock b/Cargo.lock index fd003f6..db1f067 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -296,9 +296,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.105" +version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360" +checksum = "2cc66a619ed80bf7a0f6b17dd063a84b88f6dea1813737cf469aef1d081142c2" dependencies = [ "itoa", "ryu", @@ -371,9 +371,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.7.6" +version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c17e963a819c331dcacd7ab957d80bc2b9a9c1e71c804826d2f283dd65306542" +checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257" dependencies = [ "serde", "serde_spanned", @@ -392,9 +392,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.19.14" +version = "0.19.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8123f27e969974a3dfba720fdb560be359f57b44302d280ba72e76a74480e8a" +checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" dependencies = [ "indexmap", "serde", diff --git a/Cargo.toml b/Cargo.toml index 901ab9c..7a5f113 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 }" diff --git a/README.md b/README.md index 90d744d..bc9714d 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 diff --git a/test/circle.yml b/test/circle.yml new file mode 100644 index 0000000..5f97557 --- /dev/null +++ b/test/circle.yml @@ -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 diff --git a/test/yq.test.bats b/test/yq.test.bats index dc3e447..66f6217 100644 --- a/test/yq.test.bats +++ b/test/yq.test.bats @@ -56,4 +56,12 @@ run yq -i=toml '.dependencies.clap.features' -c < Cargo.toml echo "$output" && echo "$output" | grep '["cargo","derive"]' -} \ No newline at end of file +} + +@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" +} diff --git a/yq.rs b/yq.rs index daf21f4..267cb4f 100644 --- a/yq.rs +++ b/yq.rs @@ -72,7 +72,13 @@ impl Args { let mut docs: Vec = 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 @@ -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);