Skip to content

Commit 3cc1bbd

Browse files
authored
Merge pull request #631 from jkirsteins/janis.kirsteins/jq-env-fallback
fix(config): propagate --jq to nested pup calls via PUP_FILTER env
2 parents 81e8d8e + 65b586e commit 3cc1bbd

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

docs/EXTENSIONS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Extensions receive pup's auth credentials via environment variables. This means
110110
| `DD_SITE` | Always | Datadog site (e.g., `datadoghq.com`) |
111111
| `DD_ORG` | Org is specified | Named org session |
112112
| `PUP_OUTPUT` | Always | Output format (`json`, `table`, `yaml`, `csv`, `tsv`) |
113+
| `PUP_FILTER` | `--jq` flag is set | The jq expression, verbatim |
113114
| `PUP_AUTO_APPROVE` | `--yes` flag or agent mode | `true` |
114115
| `PUP_READ_ONLY` | Read-only mode | `true` |
115116
| `PUP_AGENT_MODE` | Agent mode | `true` |
@@ -118,7 +119,7 @@ Pup refreshes the OAuth2 token if needed before passing it to the extension, so
118119

119120
Variables not active in the current session are explicitly removed from the child environment to prevent stale credentials from leaking through the parent shell.
120121

121-
The `PUP_OUTPUT`, `PUP_AGENT_MODE`, `PUP_READ_ONLY`, and `PUP_AUTO_APPROVE` variables are read back by a child `pup` process. So if your extension shells out to `pup` (see below), those nested calls automatically inherit the format and mode the user selected on the parent command.
122+
The `PUP_OUTPUT`, `PUP_FILTER`, `PUP_AGENT_MODE`, `PUP_READ_ONLY`, and `PUP_AUTO_APPROVE` variables are read back by a child `pup` process. So if your extension shells out to `pup` (see below), those nested calls automatically inherit the format, `--jq` filter, and mode the user selected on the parent command. An extension that prints its own JSON directly — rather than delegating to a nested `pup` call — still needs to read `PUP_FILTER` itself and apply the jq expression if it wants to honor `--jq`.
122123

123124
### Example: using auth in a Python extension
124125

src/config.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ impl Config {
199199
|| env_bool("DD_CLI_READ_ONLY")
200200
|| env_bool("PUP_READ_ONLY")
201201
|| file_cfg.read_only.unwrap_or(false),
202-
jq: None, // set by caller from --jq flag
202+
// `PUP_FILTER` is injected into extension subprocesses (like
203+
// `PUP_OUTPUT` above) so a child `pup` call inherits the parent's
204+
// --jq expression; an explicit --jq flag still overrides it.
205+
jq: env_or("PUP_FILTER", None),
203206
};
204207

205208
Ok(cfg)
@@ -1665,6 +1668,7 @@ mod tests {
16651668
"DD_AUTO_APPROVE",
16661669
"DD_CLI_AUTO_APPROVE",
16671670
"PUP_AUTO_APPROVE",
1671+
"PUP_FILTER",
16681672
] {
16691673
std::env::remove_var(var);
16701674
}
@@ -1681,6 +1685,15 @@ mod tests {
16811685
std::env::remove_var("DD_OUTPUT");
16821686
std::env::remove_var("PUP_OUTPUT");
16831687

1688+
// PUP_FILTER drives cfg.jq so a nested `pup` call spawned by an extension
1689+
// (e.g. `pup api` invoked from inside a `pup-<ext>` process) inherits the
1690+
// outer --jq expression, the same way it inherits --output.
1691+
assert_eq!(Config::from_env().unwrap().jq, None);
1692+
std::env::set_var("PUP_FILTER", ".[] | .id");
1693+
let cfg = Config::from_env().unwrap();
1694+
assert_eq!(cfg.jq.as_deref(), Some(".[] | .id"));
1695+
std::env::remove_var("PUP_FILTER");
1696+
16841697
// PUP_READ_ONLY / PUP_AUTO_APPROVE flip the mode flags.
16851698
std::env::set_var("PUP_READ_ONLY", "true");
16861699
std::env::set_var("PUP_AUTO_APPROVE", "true");

0 commit comments

Comments
 (0)