Add a function to call commands with str as input and return the output as String.

This commit is contained in:
Oleksandr Kozachuk
2022-12-15 20:23:19 +01:00
parent 4dcfc95ad8
commit 56dac08bdf
2 changed files with 17 additions and 0 deletions
+1
View File
@@ -7,6 +7,7 @@ mod password;
mod repl;
mod skey;
mod structs;
mod utils;
use rpassword::prompt_password;
use rustyline::Editor;
+16
View File
@@ -0,0 +1,16 @@
use std::io;
use std::io::{Read, Write};
use std::process::{Command, Stdio};
pub fn call_cmd_with_input(cmd: &str, args: Vec<&str>, input: &str) -> io::Result<String> {
let mut cmd = Command::new(cmd).args(args).stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?;
let stdin = cmd.stdin.as_mut().unwrap();
stdin.write_all(input.as_bytes())?;
let stdout = cmd.stdout.as_mut().unwrap();
let mut output = Vec::new();
stdout.read_to_end(&mut output)?;
match String::from_utf8(output) {
Ok(x) => Ok(x),
Err(err) => Err(io::Error::new(io::ErrorKind::InvalidData, err.utf8_error())),
}
}