summaryrefslogtreecommitdiffstatshomepage
path: root/src/actions/command.rs
diff options
context:
space:
mode:
authorLibravatar sommerfeld <sommerfeld@sommerfeld.dev>2024-04-21 16:04:38 +0100
committerLibravatar sommerfeld <sommerfeld@sommerfeld.dev>2024-04-21 16:04:38 +0100
commit1ab6ecba6f509b7b76865d65c77ecebc51efd2d3 (patch)
treea9b92e15769d483560d5799569b14c985b9c3ea5 /src/actions/command.rs
downloadsentrum-1ab6ecba6f509b7b76865d65c77ecebc51efd2d3.tar.gz
sentrum-1ab6ecba6f509b7b76865d65c77ecebc51efd2d3.tar.bz2
sentrum-1ab6ecba6f509b7b76865d65c77ecebc51efd2d3.zip
Initial commitv0.1.0
Diffstat (limited to 'src/actions/command.rs')
-rw-r--r--src/actions/command.rs61
1 files changed, 61 insertions, 0 deletions
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<String>,
+ #[serde(default)]
+ clear_parent_env: bool,
+ #[serde(default)]
+ envs: HashMap<String, String>,
+ working_dir: Option<String>,
+}
+
+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<Self> {
+ 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(())
+ }
+}