Skip to content

Commit

Permalink
Support -L modules
Browse files Browse the repository at this point in the history
Signed-off-by: clux <sszynrae@gmail.com>
  • Loading branch information
clux committed Sep 18, 2023
1 parent 12de361 commit 8ed1a6d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions test/modules/k.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module {
"name": "k"
};

def gvk:
"\(.apiVersion).\(.kind)"
;
5 changes: 5 additions & 0 deletions test/yq.test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
8 changes: 8 additions & 0 deletions yq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
}

impl Args {
Expand All @@ -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
}

Expand Down

0 comments on commit 8ed1a6d

Please sign in to comment.