From 34d0e8f40df4ec67718de673645bd443a8674375 Mon Sep 17 00:00:00 2001 From: clux Date: Tue, 19 Sep 2023 09:58:42 +0100 Subject: [PATCH 1/3] Allow not passing a query string (for unfiltered) `cat json | yq` worked with python-yq by defaulting the filter to '.' to be able to hotswap we should allow this also Signed-off-by: clux --- test/yq.test.bats | 2 +- yq.rs | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/test/yq.test.bats b/test/yq.test.bats index b88df9a..a179a54 100644 --- a/test/yq.test.bats +++ b/test/yq.test.bats @@ -47,7 +47,7 @@ skip # ci is fun fi run yq - [ "$status" -eq 2 ] + [ "$status" -eq 1 ] } @test "toml" { diff --git a/yq.rs b/yq.rs index 0109ed6..da8e25d 100644 --- a/yq.rs +++ b/yq.rs @@ -69,8 +69,10 @@ struct Args { in_place: bool, /// Query to be sent to jq (see https://jqlang.github.io/jq/manual/) + /// + /// Default "." #[arg()] - jq_query: String, + jq_query: Option, /// Optional file to read (instead of stdin) in the chosen --input format #[arg()] @@ -105,7 +107,7 @@ struct Args { impl Args { fn jq_args(&self) -> Vec { - let mut args = vec![self.jq_query.clone()]; + let mut args = vec![self.jq_query.clone().unwrap_or_else(|| ".".into())]; if self.compact_output { args.push("-c".into()); } @@ -292,7 +294,7 @@ mod test { fn file_input_both_outputs() -> Result<()> { init_env_tracing_stderr()?; let mut args = Args { - jq_query: ".[2].metadata".into(), + jq_query: Some(".[2].metadata".into()), compact_output: true, output: Output::Jq, file: Some("test/deploy.yaml".into()), From 297538a72ce834c7fee6f259f8e67d08736f6bfe Mon Sep 17 00:00:00 2001 From: clux Date: Tue, 19 Sep 2023 10:01:45 +0100 Subject: [PATCH 2/3] just make the param optional, jq also supports it Signed-off-by: clux --- yq.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/yq.rs b/yq.rs index da8e25d..e47964c 100644 --- a/yq.rs +++ b/yq.rs @@ -107,7 +107,10 @@ struct Args { impl Args { fn jq_args(&self) -> Vec { - let mut args = vec![self.jq_query.clone().unwrap_or_else(|| ".".into())]; + let mut args = vec![]; + if let Some(query) = &self.jq_query { + args.push(query.into()) + } if self.compact_output { args.push("-c".into()); } From d1bbc702036e27fa38667cc4e443edffe030ab33 Mon Sep 17 00:00:00 2001 From: clux Date: Tue, 19 Sep 2023 10:15:12 +0100 Subject: [PATCH 3/3] paramless tests Signed-off-by: clux --- test/yq.test.bats | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/yq.test.bats b/test/yq.test.bats index a179a54..fe61dbf 100644 --- a/test/yq.test.bats +++ b/test/yq.test.bats @@ -87,3 +87,10 @@ run yq 'include "k"; . | gvk' -r -L$PWD/test/modules < test/grafana.yaml echo "$output" && echo "$output" | grep 'apps/v1.Deployment' } + +@test "paramless" { + run yq -y <<< '["foo"]' + echo "$output" && echo "$output" | grep '\- foo' + run yq <<< '"bar"' + echo "$output" && echo "$output" | grep '"bar"' +}