cinderella

[unmaintained] simple CI engine
Log | Files | Refs | README | LICENSE

commit 07c0ed6fe1f6e7a66713ba881ab23f0a7a84f10a
parent 1d3a95c2932ac3bd1485995850e100cfb45dfc5b
Author: Stefan Koch <programming@stefan-koch.name>
Date:   Sun, 22 Sep 2019 14:14:19 +0200

implement a nullmailer that does nothing

Diffstat:
Msrc/lib.rs | 7+++----
Msrc/mail.rs | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
2 files changed, 64 insertions(+), 24 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs @@ -74,10 +74,9 @@ pub fn run(repo_ptr: &RepoPointer) { // configured, send a mail let res = execution::execute(&pipelines, &variables, &mut io::stdout()); - if let Some(config) = config.email { - if let ExecutionResult::Error(msg) = res { - mail::send_mail(&format!("Build failed: {}", msg), &config); - } + if let ExecutionResult::Error(msg) = res { + let mailer = mail::build_mailer(&config.email); + mailer.send_mail(&format!("Build failed: {}", msg)); } } else { println!("No Cinderella configuration found"); diff --git a/src/mail.rs b/src/mail.rs @@ -5,25 +5,66 @@ use lettre::smtp::ConnectionReuseParameters; use crate::config; -pub fn send_mail(text: &str, config: &config::Email) { - let email = Email::builder() - .to(config.to.to_string()) - .from(config.from.to_string()) - .subject("Build failed") - .text(text) - .build() - .unwrap(); - - let mut mailer = SmtpClient::new_simple(&config.server).unwrap() - .credentials(Credentials::new( - config.user.to_string(), - config.password.to_string())) - .smtp_utf8(true) - .authentication_mechanism(Mechanism::Plain) - .connection_reuse(ConnectionReuseParameters::ReuseUnlimited).transport(); - - if let Err(_err) = mailer.send(email.into()) { - // TODO: Add logging here - panic!("Sending of mail failed"); +pub trait Mailer { + fn send_mail(&self, text: &str); +} + +struct NullMailer; + +struct SmtpMailer { + pub server: String, + pub user: String, + pub password: String, + pub from: String, + pub to: String, +} + +impl SmtpMailer { + pub fn from_config(config: &config::Email) -> SmtpMailer { + SmtpMailer { + server: config.server.clone(), + user: config.user.clone(), + password: config.password.clone(), + from: config.from.clone(), + to: config.to.clone(), + } + } +} + +impl Mailer for NullMailer { + fn send_mail(&self, _text: &str) { + + } +} + +impl Mailer for SmtpMailer { + fn send_mail(&self, text: &str) { + let email = Email::builder() + .to(self.to.to_string()) + .from(self.from.to_string()) + .subject("Build failed") + .text(text) + .build() + .unwrap(); + + let mut mailer = SmtpClient::new_simple(&self.server).unwrap() + .credentials(Credentials::new( + self.user.to_string(), + self.password.to_string())) + .smtp_utf8(true) + .authentication_mechanism(Mechanism::Plain) + .connection_reuse(ConnectionReuseParameters::ReuseUnlimited).transport(); + + if let Err(_err) = mailer.send(email.into()) { + // TODO: Add logging here + panic!("Sending of mail failed"); + } + } +} + +pub fn build_mailer(config: &Option<config::Email>) -> Box<dyn Mailer> { + match config { + None => Box::new(NullMailer), + Some(config) => Box::new(SmtpMailer::from_config(&config)), } }