From 4c9c8de0c9f1f03f44ae60c088f506c1e4a51d26 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 24 May 2024 14:12:16 +0200 Subject: option to show shell command output --- src/check.rs | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/check.rs b/src/check.rs index a979e96..cbcc839 100644 --- a/src/check.rs +++ b/src/check.rs @@ -15,8 +15,16 @@ use tokio::{ pub enum Check { Systemd(String), Pacman(String), - Http { title: Option, url: String }, - Shell { title: String, command: String }, + Http { + title: Option, + url: String, + }, + Shell { + title: String, + command: String, + #[serde(default)] + output: bool, + }, } pub async fn check_loop(config: Arc, i: usize) { @@ -94,17 +102,31 @@ impl Check { Ok(s) } } - Check::Shell { command, .. } => { + Check::Shell { + command, output, .. + } => { let args = shlex::split(&command).ok_or(anyhow!("command syntax invalid"))?; let status = Command::new(args.get(0).ok_or(anyhow!("argv0 missing"))?) .args(&args[1..]) - .status() + .output() .await; - match status { - Ok(status) if status.success() => Ok(Default::default()), - Ok(status) => bail!("failed with code {}", status.code().unwrap_or(1)), - Err(e) => bail!("command failed to execute: {e}"), + if *output { + match status { + Ok(status) if status.status.success() => { + Ok(String::from_utf8_lossy(&status.stdout).to_string()) + } + Ok(status) => bail!("{}", String::from_utf8_lossy(&status.stdout)), + Err(e) => bail!("command failed to execute: {e}"), + } + } else { + match status { + Ok(status) if status.status.success() => Ok(Default::default()), + Ok(status) => { + bail!("failed with code {}", status.status.code().unwrap_or(1)) + } + Err(e) => bail!("command failed to execute: {e}"), + } } } Check::Http { url, .. } => { -- cgit v1.2.3-70-g09d2