From f300cb1202ecdf5581b70e3226426c9c60d14581 Mon Sep 17 00:00:00 2001 From: Colin Date: Sat, 29 Apr 2023 09:35:07 +0000 Subject: [PATCH] mx-sanebot: factor out a helper when invoking processes --- pkgs/mx-sanebot/src/msg_handler.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pkgs/mx-sanebot/src/msg_handler.rs b/pkgs/mx-sanebot/src/msg_handler.rs index 22cb93f2..c8c36931 100644 --- a/pkgs/mx-sanebot/src/msg_handler.rs +++ b/pkgs/mx-sanebot/src/msg_handler.rs @@ -186,27 +186,28 @@ impl From for Request { } } +fn exec_stdout(program: &str, args: &[&str]) -> Option { + process::Command::new(program) + .args(args) + .output() + .ok() + .and_then(|output| + str::from_utf8(&output.stdout).ok().map(ToOwned::to_owned) + ) +} + impl Request { fn evaluate(self) -> Response { match self { Request::Help => Response::Help, Request::Bt => Response::Bt( - process::Command::new("sane-bt-show") - .output() - .ok() - .and_then(|output| - str::from_utf8(&output.stdout).ok().map(ToOwned::to_owned) - ).unwrap_or_else(|| + exec_stdout("sane-bt-show", &[]) + .unwrap_or_else(|| "failed to retrieve torrent status".to_owned()) ), Request::BtSearch(phrase) => Response::BtSearch( - process::Command::new("sane-bt-search") - .args([&*phrase]) - .output() - .ok() - .and_then(|output| - str::from_utf8(&output.stdout).ok().map(ToOwned::to_owned) - ).unwrap_or_else(|| + exec_stdout("sane-bt-search", &[&*phrase]) + .unwrap_or_else(|| "failed to complete torrent search".to_owned()) ), }