Skip to content

Commit

Permalink
feat(exec): add support for notification templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed May 27, 2021
1 parent 8485632 commit 706a4bd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 41 deletions.
44 changes: 9 additions & 35 deletions src/commands/exec.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -65,64 +65,38 @@ impl Exec {
(Config::default(), format!("<No Config File Path>"))
};

let cmd_line = if let Some(task) = &self.task {
let task = if let Some(task) = &self.task {
config
.tasks()
.get(task)
.ok_or(format!(
"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)?
.working_dir(config.working_dir())?
.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()
Expand Down
14 changes: 10 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand All @@ -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() {
Expand Down
8 changes: 6 additions & 2 deletions src/config/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!("<unknonw short command>")
}
}
pub fn depends_on(&self) -> &Vec<String> {
&self.depends_on
Expand Down

0 comments on commit 706a4bd

Please sign in to comment.