From 706a4bd08590f50d8b32575ca5f2178e54fe5437 Mon Sep 17 00:00:00 2001 From: Joxit Date: Sun, 9 May 2021 22:16:13 +0200 Subject: [PATCH] feat(exec): add support for notification templates --- src/commands/exec.rs | 44 +++++++++----------------------------------- src/config/mod.rs | 14 ++++++++++---- src/config/task.rs | 8 ++++++-- 3 files changed, 25 insertions(+), 41 deletions(-) diff --git a/src/commands/exec.rs b/src/commands/exec.rs index 6e20a86..f0961b4 100644 --- a/src/commands/exec.rs +++ b/src/commands/exec.rs @@ -1,4 +1,4 @@ -use crate::config::{Config, WhenNotify}; +use crate::config::Config; use crate::utils::traits::CommandConfig; use libc::{fork, signal}; use libc::{SIGHUP, SIG_IGN}; @@ -65,7 +65,7 @@ impl Exec { (Config::default(), format!("")) }; - let cmd_line = if let Some(task) = &self.task { + let task = if let Some(task) = &self.task { config .tasks() .get(task) @@ -73,16 +73,15 @@ impl Exec { "The task `{}` does not exist in your config file `{}`", task, path ))? - .full_command() + .clone() } else { - self.command.join(" ") + crate::config::Task::new(format!(""), vec![self.command.join(" ")], vec![], None) }; - - let first_cmd = cmd_line.splitn(2, " ").next().unwrap(); + let cmd_line = task.full_command(); let mut child = Command::new("sh") .arg("-c") - .arg(cmd_line.clone()) + .arg(&cmd_line) .stdin(self.stdin()) .stdout_opt(config.stdout(), !self.background)? .stderr_opt(config.stderr(), !self.background)? @@ -90,39 +89,14 @@ impl Exec { .spawn() .map_err(|msg| format!("Can't run command `{}`: {}", cmd_line, msg))?; - if let Ok(exit) = child.wait() { - let msg = format!("Command `{}` ended with status code {}", &first_cmd, exit); - self.notify(&config, msg, WhenNotify::End); - } else { - let msg = format!("Command `{}` ended with failure", &first_cmd); - self.notify(&config, msg, WhenNotify::End); + let exit = child.wait().unwrap(); + if let Some(notification) = config.notification() { + notification.notify_task_end(&task, exit); } Ok(()) } - fn notify(&self, config: &Config, msg: String, when: WhenNotify) { - if let Some(notification) = config.notification() { - if *notification.when() == WhenNotify::Never - || (*notification.when() != WhenNotify::Always && *notification.when() != when) - { - return; - } - if let Some(slack) = notification.slack() { - if let Some(when_slack) = slack.when() { - if *when_slack == WhenNotify::Never - || (*when_slack != WhenNotify::Always && *when_slack != when) - { - return; - } - } - if let Err(e) = crate::notification::post_slack(&slack, msg.as_str()) { - eprintln!("Can't use slac notification: {}", e); - } - } - } - } - fn stdin(&self) -> Stdio { if self.background { Stdio::null() diff --git a/src/config/mod.rs b/src/config/mod.rs index 0f0de41..4dfc9e5 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -127,7 +127,7 @@ impl Notification { } pub fn notify_task_end(&self, task: &Task, status_code: std::process::ExitStatus) { - if !self.when.should_notify(&WhenNotify::TaskEnd) { + if !self.when().should_notify(&WhenNotify::TaskEnd) { return; } if let Some(slack) = self.slack() { @@ -136,10 +136,16 @@ impl Notification { return; } } + let short_cmd = task.short_command(); + let id = if task.id().len() > 0 { + task.id() + } else { + &short_cmd + }; let msg = crate::notification::replace_templates(self.messages().task_end()); - let msg = msg.replace("{task.id}", task.id()); + let msg = msg.replace("{task.id}", id); let msg = msg.replace("{task.full_cmd}", &task.full_command()); - let msg = msg.replace("{task.short_cmd}", &task.short_command()); + let msg = msg.replace("{task.short_cmd}", &short_cmd); let msg = msg.replace("{task.status_code}", &format!("{}", status_code)); if let Err(e) = crate::notification::post_slack(&slack, msg.as_str()) { eprintln!("Can't use slack notification: {}", e); @@ -148,7 +154,7 @@ impl Notification { } pub fn notify_all_tasks_end(&self, success: i32, failures: i32, failed: bool) { - if !self.when.should_notify(&WhenNotify::End) { + if !self.when().should_notify(&WhenNotify::End) { return; } if let Some(slack) = self.slack() { diff --git a/src/config/task.rs b/src/config/task.rs index 41db22f..f4b8901 100644 --- a/src/config/task.rs +++ b/src/config/task.rs @@ -35,8 +35,12 @@ impl Task { self.commands().join(" && ") } pub fn short_command(&self) -> String { - let cmd = self.commands().first().unwrap_or(&format!("")); - cmd.splitn(2, " ").next().unwrap().to_string() + if self.commands().len() > 0 { + let cmd = self.commands().first().unwrap(); + cmd.splitn(2, " ").next().unwrap().to_string() + } else { + format!("") + } } pub fn depends_on(&self) -> &Vec { &self.depends_on