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

Allow for passing stdin to child #106

Merged
merged 17 commits into from
Oct 6, 2023
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: 14 additions & 3 deletions src/lune/builtins/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
use dunce::canonicalize;
use mlua::prelude::*;
use os_str_bytes::RawOsString;
use tokio::io::AsyncWriteExt;

use crate::lune::{scheduler::Scheduler, util::TableBuilder};

Expand Down Expand Up @@ -199,17 +200,27 @@ async fn process_spawn(
async fn spawn_command(
program: String,
args: Option<Vec<String>>,
options: ProcessSpawnOptions,
mut options: ProcessSpawnOptions,
) -> LuaResult<(ExitStatus, Vec<u8>, Vec<u8>)> {
let inherit_stdio = options.inherit_stdio;
let stdin = options.stdin.take();

let child = options
let mut child = options
.into_command(program, args)
.stdin(Stdio::null())
.stdin(match stdin.is_some() {
true => Stdio::piped(),
false => Stdio::null(),
})
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;

// If the stdin option was provided, we write that to the child
if let Some(stdin) = stdin {
let mut child_stdin = child.stdin.take().unwrap();
child_stdin.write_all(&stdin).await.into_lua_err()?;
}

if inherit_stdio {
pipe_and_inherit_child_process_stdio(child).await
} else {
Expand Down
15 changes: 15 additions & 0 deletions src/lune/builtins/process/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct ProcessSpawnOptions {
pub(crate) envs: HashMap<String, String>,
pub(crate) shell: Option<String>,
pub(crate) inherit_stdio: bool,
pub(crate) stdin: Option<Vec<u8>>,
}

impl<'lua> FromLua<'lua> for ProcessSpawnOptions {
Expand Down Expand Up @@ -133,6 +134,20 @@ impl<'lua> FromLua<'lua> for ProcessSpawnOptions {
}
}

/*
If we have stdin contents, we need to pass those to the child process
*/
match value.get("stdin")? {
LuaValue::Nil => {}
LuaValue::String(s) => this.stdin = Some(s.as_bytes().to_vec()),
value => {
return Err(LuaError::RuntimeError(format!(
"Invalid type for option 'stdin' - expected 'string', got '{}'",
value.type_name()
)))
}
}

Ok(this)
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/process/spawn.luau
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,20 @@ assert(
echoResult.stdout == (echoMessage .. trailingAddition),
"Inheriting stdio did not return proper output"
)

-- Passing stdin strings should work

local stdinChild = process.spawn(not IS_WINDOWS and "xargs" or "powershell", {
"echo",
}, {
stdin = echoMessage .. (IS_WINDOWS and "\n\n" or ""),
})

local stdinChildOut = stdinChild.stdout
if IS_WINDOWS then
stdinChildOut = stdinChildOut:sub(#stdinChildOut - #echoMessage - 1, #stdinChildOut)
end
assert(
stdinChildOut == echoMessage .. trailingAddition,
"Stdin passing did not return proper output"
)
2 changes: 2 additions & 0 deletions types/process.luau
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ export type SpawnOptionsStdio = "inherit" | "default"
* `env` - Extra environment variables to give to the process
* `shell` - Whether to run in a shell or not - set to `true` to run using the default shell, or a string to run using a specific shell
* `stdio` - How to treat output and error streams from the child process - set to "inherit" to pass output and error streams to the current process
* `stdin` - Optional standard input to pass to spawned child process
]=]
export type SpawnOptions = {
cwd: string?,
env: { [string]: string }?,
shell: (boolean | string)?,
stdio: SpawnOptionsStdio?,
stdin: string?,
}

--[=[
Expand Down