Skip to content

Commit bb6cd2c

Browse files
authored
Merge pull request #17 from clux/modules
Support `-L` modules
2 parents 12de361 + db40095 commit bb6cd2c

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ cargo binstall whyq
2222

2323
## Features
2424

25-
- arbitrary `jq` usage on yaml/toml/json input with the same syntax (args passed along to `jq` with json converted input)
25+
- arbitrary `jq` usage on yaml/toml/json input with the same syntax (same filter syntax, supports modules) by leveraging `jq` as an intermediate format
2626
- drop-in replacement to [python-yq](https://kislyuk.github.io/yq/) (e.g. provides: yq)
2727
- handles multidoc **yaml** input (vector of documents returned when multiple docs found)
2828
- 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:
121121
yq -y '.updates[] | select(.["package-ecosystem"] == "cargo") | .groups' .github/dependabot.yml
122122
```
123123

124+
Using helpers from `jq` [modules](https://jqlang.github.io/jq/manual/):
125+
126+
```sh
127+
$ yq 'include "k"; .[] | gvk' -r -L$PWD/test/modules < test/deploy.yaml
128+
v1.ServiceAccount
129+
rbac.authorization.k8s.io/v1.ClusterRole
130+
rbac.authorization.k8s.io/v1.ClusterRoleBinding
131+
v1.Service
132+
apps/v1.Deployment
133+
```
134+
124135
## Output Caveats
125136

126137
Output formatting such as `-y` for YAML or `-t` for TOML will require the output from `jq` to be parseable json.

test/modules/k.jq

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module {
2+
"name": "k"
3+
};
4+
5+
def gvk:
6+
"\(.apiVersion).\(.kind)"
7+
;

test/yq.test.bats

+5
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,8 @@
8282
run yq --input=json ".ingredients | keys" -c < test/guacamole.json
8383
echo "$output" && echo "$output" | grep '["avocado","coriander","cumin","garlic","lime","onions","pepper","salt","tomatoes"]'
8484
}
85+
86+
@test "jq_modules" {
87+
run yq 'include "k"; . | gvk' -r -L$PWD/test/modules < test/grafana.yaml
88+
echo "$output" && echo "$output" | grep 'apps/v1.Deployment'
89+
}

yq.rs

+8
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ struct Args {
9797
/// that the jq -r output is deserializable into the desired output format.
9898
#[arg(short = 'j', long, default_value = "false")]
9999
join_output: bool,
100+
101+
/// Search jq modules from the directory
102+
#[arg(short = 'L', default_value = "None")]
103+
modules: Option<PathBuf>,
100104
}
101105

102106
impl Args {
@@ -111,6 +115,10 @@ impl Args {
111115
if self.join_output {
112116
args.push("-j".into());
113117
}
118+
if let Some(dir) = &self.modules {
119+
args.push("-L".into());
120+
args.push(format!("{}", dir.display()));
121+
}
114122
args
115123
}
116124

0 commit comments

Comments
 (0)