Skip to content

Commit

Permalink
Add the ability to add argument to the interpreter
Browse files Browse the repository at this point in the history
When you have a script that you want to run with a custom interpreter you can
now add arguments to the interpreter.
  • Loading branch information
chmouel committed Dec 11, 2024
1 parent 75dea2d commit 13f8a08
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

![image](https://github.com/chmouel/raffi/assets/98980/04d6af0f-2a80-47d5-a2ec-95443a629305)

*(This uses my Fuzzel configuration, see below for more details)*
> [!NOTE]
> This uses my Fuzzel configuration, see below for more details
## Description

Expand Down Expand Up @@ -177,7 +178,20 @@ hello_script:
import os
print("hello world and show me your env")
print(os.environ)
description: "Hello Script"
description: "Hello Python script"
icon: "script"
```

If you want to add some specific arguments to the interpreter, you can do it like this:

```yaml
hello_script:
binary: sh
args: ["-xv"]
script: |
echo "hello world and show me your env"
env
description: "Hello debug"
icon: "script"
```

Expand Down
31 changes: 22 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ fn is_valid_config(mc: &mut RaffiConfig, args: &Args) -> bool {
return false;
}
mc.binary = Some(args.default_script_shell.clone());
if let Some(binary_args) = &mc.args {
mc.binary = Some(format!(
"{} {}",
mc.binary.as_deref().unwrap_or_default(),
binary_args.join(" ")
));
}
} else if let Some(binary) = &mc.binary {
if !find_binary(binary) {
return false;
Expand Down Expand Up @@ -247,6 +254,10 @@ fn make_fuzzel_input(rafficonfigs: &[RaffiConfig], no_icons: bool) -> Result<Str

/// Execute the chosen command or script.
fn execute_chosen_command(mc: &RaffiConfig, args: &Args, interpreter: &str) -> Result<()> {
let interpreter_parts: Vec<&str> = interpreter.split_whitespace().collect();
let interpreter_cmd = interpreter_parts.get(0).context("No interpreter found")?;

Check failure on line 258 in src/main.rs

View workflow job for this annotation

GitHub Actions / Lints

accessing first element with `interpreter_parts.get(0)`
let interpreter_args = &interpreter_parts[1..];

if args.print_only {
if let Some(script) = &mc.script {
println!("#!/usr/bin/env {}\n{}", interpreter, script);
Expand All @@ -264,17 +275,19 @@ fn execute_chosen_command(mc: &RaffiConfig, args: &Args, interpreter: &str) -> R
tempfile::NamedTempFile::new().context("Failed to create temp script file")?;
writeln!(temp_script, "#!/usr/bin/env {}\n{}", interpreter, script)
.context("Failed to write to temp script file")?;
let mut child = Command::new("/usr/bin/env")
.arg(interpreter)
.arg(temp_script.path())
.spawn()
.context("cannot launch script")?;
let mut command = Command::new("/usr/bin/env");
command
.arg(interpreter_cmd)
.args(interpreter_args)
.arg(temp_script.path());
let mut child = command.spawn().context("cannot launch script")?;
child.wait().context("cannot wait for child")?;
} else {
let mut child = Command::new(mc.binary.as_deref().context("Binary not found")?)
.args(mc.args.as_deref().unwrap_or(&[]))
.spawn()
.context("cannot launch binary")?;
let mut command = Command::new(mc.binary.as_deref().context("Binary not found")?);
if let Some(binary_args) = &mc.args {
command.args(binary_args);
}
let mut child = command.spawn().context("cannot launch binary")?;
child.wait().context("cannot wait for child")?;
}
Ok(())
Expand Down

0 comments on commit 13f8a08

Please sign in to comment.