Browse Source

Use bytes for command stdin

master
Adrian Heine 3 years ago
parent
commit
fb23353453
  1. 20
      src/command_runner.rs
  2. 4
      src/symbols/cron.rs
  3. 4
      src/symbols/git/checkout.rs

20
src/command_runner.rs

@ -33,17 +33,17 @@ pub fn get_output(output: Output) -> Result<Vec<u8>, Box<dyn Error>> {
#[async_trait(?Send)]
pub trait CommandRunner {
async fn run(&self, program: &str, args: &[&OsStr], stdin: &str) -> IoResult<Output>;
async fn run(&self, program: &str, args: &[&OsStr], stdin: &[u8]) -> IoResult<Output>;
async fn run_with_args(&self, program: &str, args: &[&OsStr]) -> IoResult<Output> {
self.run(program, args, "").await
self.run(program, args, b"").await
}
async fn get_output(&self, program: &str, args: &[&OsStr]) -> Result<Vec<u8>, Box<dyn Error>> {
let output = self.run_with_args(program, args).await?;
get_output(output)
}
async fn run_successfully(&self, program: &str, args: &[&OsStr]) -> Result<(), Box<dyn Error>> {
is_success(self.run(program, args, "").await)?;
is_success(self.run(program, args, b"").await)?;
Ok(())
}
async fn get_stderr(&self, program: &str, args: &[&OsStr]) -> Result<Vec<u8>, Box<dyn Error>> {
@ -56,7 +56,7 @@ pub struct StdCommandRunner;
#[async_trait(?Send)]
impl CommandRunner for StdCommandRunner {
async fn run(&self, program: &str, args: &[&OsStr], input: &str) -> IoResult<Output> {
async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult<Output> {
//println!("{} {:?}", program, args);
let mut child = Command::new(program)
.args(args)
@ -67,7 +67,7 @@ impl CommandRunner for StdCommandRunner {
.expect("Failed to spawn child process");
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
stdin
.write_all(input.as_bytes())
.write_all(input)
.await
.expect("Failed to write to stdin");
let res = child.wait_with_output().await;
@ -122,7 +122,7 @@ impl Drop for TempSetEnv<'_> {
#[async_trait(?Send)]
impl<U: AsRef<str>, C: CommandRunner> CommandRunner for SetuidCommandRunner<'_, U, C> {
async fn run(&self, program: &str, args: &[&OsStr], input: &str) -> IoResult<Output> {
async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult<Output> {
let uid = get_user_by_name(self.user_name.as_ref())
.expect("User does not exist")
.uid();
@ -140,7 +140,7 @@ impl<U: AsRef<str>, C: CommandRunner> CommandRunner for SetuidCommandRunner<'_,
.expect("Failed to spawn child process");
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
stdin
.write_all(input.as_bytes())
.write_all(input)
.await
.expect("Failed to write to stdin");
let res = child.wait_with_output().await;
@ -179,7 +179,7 @@ impl<'a, C> CommandRunner for SuCommandRunner<'a, C>
where
C: 'a + CommandRunner,
{
async fn run(&self, program: &str, args: &[&OsStr], input: &str) -> IoResult<Output> {
async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult<Output> {
let raw_new_args = [self.user_name, "-s", "/usr/bin/env", "--", program];
let mut new_args: Vec<&OsStr> = raw_new_args.iter().map(AsRef::as_ref).collect();
new_args.extend_from_slice(args);
@ -201,8 +201,8 @@ mod test {
run(async {
let args = args!["1"];
let start = Instant::now();
let res = c.run("sleep", args, "").fuse();
let ps = c.run("ps", args![], "").fuse();
let res = c.run("sleep", args, b"").fuse();
let ps = c.run("ps", args![], b"").fuse();
futures_util::pin_mut!(res, ps);
loop {
futures_util::select! {

4
src/symbols/cron.rs

@ -21,13 +21,13 @@ impl<'r, U, R> Cron<'r, String, U, R> {
}
#[async_trait(?Send)]
impl<C: AsRef<str>, U: AsRef<str>, R: CommandRunner> Symbol for Cron<'_, C, U, R> {
impl<C: AsRef<[u8]>, U: AsRef<str>, R: CommandRunner> Symbol for Cron<'_, C, U, R> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
let tab = self
.command_runner
.get_output("crontab", args!["-l", "-u", self.user.as_ref()])
.await?;
Ok(tab == self.content.as_ref().as_bytes())
Ok(tab == self.content.as_ref())
}
async fn execute(&self) -> Result<(), Box<dyn Error>> {

4
src/symbols/git/checkout.rs

@ -110,9 +110,9 @@ mod test {
}
#[async_trait(?Send)]
impl CommandRunner for DummyCommandRunner {
async fn run(&self, program: &str, args: &[&OsStr], stdin: &str) -> IoResult<Output> {
async fn run(&self, program: &str, args: &[&OsStr], stdin: &[u8]) -> IoResult<Output> {
assert_eq!(program, "git");
assert_eq!(stdin, "");
assert_eq!(stdin, b"");
sleep(Duration::from_millis(50)).await;
self
.args

Loading…
Cancel
Save