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 10 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
15 changes: 13 additions & 2 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 @@ -202,14 +203,24 @@ async fn spawn_command(
options: ProcessSpawnOptions,
) -> LuaResult<(ExitStatus, Vec<u8>, Vec<u8>)> {
let inherit_stdio = options.inherit_stdio;
let stdin = options.stdin;

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<&'static [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().leak())),
value => {
return Err(LuaError::RuntimeError(format!(
"Invalid type for option 'stdin' - expected 'string', got '{}'",
value.type_name()
)))
}
}

Ok(this)
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/process/spawn.luau
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,18 @@ assert(
echoResult.stdout == (echoMessage .. "\n"), -- Note that echo adds a newline
"Inheriting stdio did not return proper output"
)

-- Passing stdin strings should work

local isWindows = process.os == "windows"
CompeyDev marked this conversation as resolved.
Show resolved Hide resolved

local stdinChild = process.spawn((((not isWindows) and "xargs") or "echo"), {
((not isWindows) and "echo") or nil,
}, {
stdin = echoMessage,
})

assert(
stdinChild.stdout == (echoMessage .. "\n"), -- Note that echo adds a newline
"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