From 1ab6ecba6f509b7b76865d65c77ecebc51efd2d3 Mon Sep 17 00:00:00 2001 From: sommerfeld Date: Sun, 21 Apr 2024 16:04:38 +0100 Subject: Initial commit --- src/actions/command.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/actions/command.rs (limited to 'src/actions/command.rs') diff --git a/src/actions/command.rs b/src/actions/command.rs new file mode 100644 index 0000000..28d951a --- /dev/null +++ b/src/actions/command.rs @@ -0,0 +1,61 @@ +use std::collections::HashMap; +use std::process::Command; + +use super::Action; +use crate::message::MessageConfig; +use crate::message::MessageParams; +use anyhow::Result; +use async_trait::async_trait; +use serde::Deserialize; + +#[derive(Deserialize, Debug)] +pub struct CommandConfig { + cmd: String, + #[serde(default)] + args: Vec, + #[serde(default)] + clear_parent_env: bool, + #[serde(default)] + envs: HashMap, + working_dir: Option, +} + +pub struct CommandAction<'a> { + message_config: &'a MessageConfig, + cmd_config: &'a CommandConfig, +} + +impl<'a> CommandAction<'a> { + pub fn new(message_config: &'a MessageConfig, cmd_config: &'a CommandConfig) -> Result { + Ok(Self { + message_config, + cmd_config, + }) + } +} + +#[async_trait] +impl Action<'_> for CommandAction<'_> { + async fn run(&self, params: Option<&MessageParams<'_, '_>>) -> Result<()> { + let mut cmd = Command::new(&self.cmd_config.cmd); + for arg in self.cmd_config.args.iter() { + cmd.arg(if let Some(p) = params { + self.message_config.replace_template_params(arg, p)? + } else { + arg.clone() + }); + } + + if self.cmd_config.clear_parent_env { + cmd.env_clear(); + } + cmd.envs(&self.cmd_config.envs); + + if let Some(working_dir) = &self.cmd_config.working_dir { + cmd.current_dir(working_dir); + } + + cmd.status()?; + Ok(()) + } +} -- cgit v1.2.3-70-g09d2