diff --git a/README.md b/README.md index 74a58e9..b6a86b6 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ cargo binstall whyq ## Features -- arbitrary `jq` usage on yaml/toml/json input with the same syntax (args passed along to `jq` with json converted input) +- arbitrary `jq` usage on yaml/toml/json input with the same syntax (same filter syntax, supports modules) by leveraging `jq` as an intermediate format - 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`) @@ -121,6 +121,17 @@ Escaping keys with slashes etc in them: yq -y '.updates[] | select(.["package-ecosystem"] == "cargo") | .groups' .github/dependabot.yml ``` +Using helpers from `jq` [modules](https://jqlang.github.io/jq/manual/): + +```sh +$ yq 'include "k"; .[] | gvk' -r -L$PWD/test/modules < test/deploy.yaml +v1.ServiceAccount +rbac.authorization.k8s.io/v1.ClusterRole +rbac.authorization.k8s.io/v1.ClusterRoleBinding +v1.Service +apps/v1.Deployment +``` + ## Output Caveats Output formatting such as `-y` for YAML or `-t` for TOML will require the output from `jq` to be parseable json. diff --git a/test/modules/k.jq b/test/modules/k.jq new file mode 100644 index 0000000..ccaf7cf --- /dev/null +++ b/test/modules/k.jq @@ -0,0 +1,7 @@ +module { + "name": "k" +}; + +def gvk: + "\(.apiVersion).\(.kind)" +; diff --git a/test/yq.test.bats b/test/yq.test.bats index c57f416..b88df9a 100644 --- a/test/yq.test.bats +++ b/test/yq.test.bats @@ -82,3 +82,8 @@ run yq --input=json ".ingredients | keys" -c < test/guacamole.json echo "$output" && echo "$output" | grep '["avocado","coriander","cumin","garlic","lime","onions","pepper","salt","tomatoes"]' } + +@test "jq_modules" { + run yq 'include "k"; . | gvk' -r -L$PWD/test/modules < test/grafana.yaml + echo "$output" && echo "$output" | grep 'apps/v1.Deployment' +} diff --git a/yq.rs b/yq.rs index bac597d..75d8770 100644 --- a/yq.rs +++ b/yq.rs @@ -97,6 +97,10 @@ struct Args { /// that the jq -r output is deserializable into the desired output format. #[arg(short = 'j', long, default_value = "false")] join_output: bool, + + /// Search jq modules from the directory + #[arg(short = 'L', default_value = "None")] + modules: Option, } impl Args { @@ -111,6 +115,10 @@ impl Args { if self.join_output { args.push("-j".into()); } + if let Some(dir) = &self.modules { + args.push("-L".into()); + args.push(format!("{}", dir.display())); + } args }