diff --git a/src/main.rs b/src/main.rs index 09e7552..c1e03b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod password; mod repl; mod skey; mod structs; +mod utils; use rpassword::prompt_password; use rustyline::Editor; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..7871ab4 --- /dev/null +++ b/src/utils.rs @@ -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 { + 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())), + } +}