diff --git a/bot-coreutils/src/lib.rs b/bot-coreutils/src/lib.rs index c53855b..96d0007 100644 --- a/bot-coreutils/src/lib.rs +++ b/bot-coreutils/src/lib.rs @@ -3,5 +3,6 @@ mod tests; pub mod process; pub mod shuffle; +pub mod string; /// Utilities to quickly check strings that represent urls pub mod url; diff --git a/bot-coreutils/src/process.rs b/bot-coreutils/src/process.rs index f9baa49..dcf70fd 100644 --- a/bot-coreutils/src/process.rs +++ b/bot-coreutils/src/process.rs @@ -1,26 +1,19 @@ use std::io; -use std::process::Stdio; -use tokio::io::AsyncReadExt; use tokio::process::Command; /// Asynchronously runs a given command and returns the output pub async fn run_command_async(command: &str, args: &[&str]) -> io::Result { log::trace!("Running command '{}' with args {:?}", command, args); - let cmd = Command::new(command) - .args(args) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn()?; - let mut stderr = String::new(); - let mut output = String::new(); + let process_output: std::process::Output = Command::new(command).args(args).output().await?; - cmd.stderr.unwrap().read_to_string(&mut stderr).await?; + log::trace!("Reading from stderr..."); + let stderr = String::from_utf8_lossy(&process_output.stderr[..]); + let stdout = String::from_utf8_lossy(&process_output.stdout[..]); if stderr.len() != 0 { log::debug!("STDERR of command {}: {}", command, stderr); } - cmd.stdout.unwrap().read_to_string(&mut output).await?; - log::trace!("Command output is {}", output); + log::trace!("Command output is {}", stdout); - Ok(output) + Ok(stdout.to_string()) } diff --git a/bot-coreutils/src/string.rs b/bot-coreutils/src/string.rs new file mode 100644 index 0000000..96b0d7a --- /dev/null +++ b/bot-coreutils/src/string.rs @@ -0,0 +1,5 @@ +/// Enquotes a string in a safe way +pub fn enquote(value: S) -> String { + let value = value.to_string(); + format!("\"{}\"", value.replace("\"", "\\\"")) +} diff --git a/bot-coreutils/src/tests/mod.rs b/bot-coreutils/src/tests/mod.rs index 26d5466..d6b09ac 100644 --- a/bot-coreutils/src/tests/mod.rs +++ b/bot-coreutils/src/tests/mod.rs @@ -1,2 +1,5 @@ #[cfg(test)] -mod url_tests; \ No newline at end of file +mod url_tests; + +#[cfg(test)] +mod string_tests; diff --git a/bot-coreutils/src/tests/string_tests.rs b/bot-coreutils/src/tests/string_tests.rs new file mode 100644 index 0000000..0fde0b7 --- /dev/null +++ b/bot-coreutils/src/tests/string_tests.rs @@ -0,0 +1,8 @@ +use crate::string::enquote; + +#[test] +fn test_enquote() { + assert_eq!(enquote("hello"), r#""hello""#); + assert_eq!(enquote(r#"hello "there""#), r#""hello \"there\"""#); + assert_eq!(enquote(""), r#""""#); +} diff --git a/src/providers/music/youtube_dl.rs b/src/providers/music/youtube_dl.rs index a673cf1..a491131 100644 --- a/src/providers/music/youtube_dl.rs +++ b/src/providers/music/youtube_dl.rs @@ -10,6 +10,7 @@ use bot_coreutils::process::run_command_async; use crate::providers::music::queue::Song; use crate::providers::music::responses::{PlaylistEntry, VideoInformation}; use crate::utils::error::BotResult; +use bot_coreutils::string::enquote; static THREAD_LIMIT: u8 = 64; @@ -44,7 +45,7 @@ pub(crate) async fn search_video_information(query: String) -> BotResult BotResult { - let result = run_command_async( - "qalc", - &["-m", "1000", format!("\"{}\"", &*expression).as_str()], - ) - .await?; + let result = run_command_async("qalc", &["-m", "1000", enquote(expression).as_str()]).await?; Ok(result) }