Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass command as first argument to shell #2061

Merged
merged 6 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,18 @@ file.
interpret `command` is the same shell that is used to evaluate recipe lines,
and can be changed with `set shell := […]`.

`command` is passed as the first argument, so if the command is `'echo $@'`,
the full command line, with the default shell command `shell -cu` and `args`
`'foo'` and `'bar'` will be:

```
'shell' '-cu' 'echo $@' 'echo $@' 'foo' 'bar'
```

This is so that `$@` works as expected, and `$1` refers to the first
argument. `$@` does not include the first positional argument, which is
expected to be the name of the program being run.

```just
# arguments can be variables
file := '/sys/class/power_supply/BAT0/status'
Expand All @@ -1362,9 +1374,10 @@ full := shell('echo $1', 'foo')
```

```just
# using python as the shell
# Using python as the shell. Since `python -c` sets `sys.argv[0]` to `'-c'`,
# the first "real" positional argument will be `sys.argv[2]`.
set shell := ["python3", "-c"]
olleh := shell('import sys; print(sys.argv[1][::-1]))', 'hello')
olleh := shell('import sys; print(sys.argv[2][::-1]))', 'hello')
```

#### Environment Variables
Expand Down
2 changes: 1 addition & 1 deletion src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
})
}

pub(crate) fn run_command(&self, command: &str, args: &[String]) -> Result<String, OutputError> {
pub(crate) fn run_command(&self, command: &str, args: &[&str]) -> Result<String, OutputError> {
let mut cmd = self.settings.shell_command(self.config);
cmd.arg(command);
cmd.args(args);
Expand Down
6 changes: 5 additions & 1 deletion src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,12 @@ fn sha256_file(evaluator: &Evaluator, path: &str) -> Result<String, String> {
}

fn shell(evaluator: &Evaluator, command: &str, args: &[String]) -> Result<String, String> {
let args = iter::once(command)
.chain(args.iter().map(String::as_str))
.collect::<Vec<&str>>();

evaluator
.run_command(command, args)
.run_command(command, &args)
.map_err(|output_error| output_error.to_string())
}

Expand Down
12 changes: 11 additions & 1 deletion tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,17 @@ fn shell_no_argument() {

#[test]
fn shell_minimal() {
assert_eval_eq("shell('echo $0 $1', 'justice', 'legs')", "justice legs");
assert_eval_eq("shell('echo $1 $2', 'justice', 'legs')", "justice legs");
}

#[test]
fn shell_args() {
assert_eval_eq("shell('echo $@', 'justice', 'legs')", "justice legs");
}

#[test]
fn shell_first_arg() {
assert_eval_eq("shell('echo $0')", "echo $0");
}

#[test]
Expand Down