diff --git a/src/arg.rs b/src/arg.rs index 94445a4..bc3c2e6 100644 --- a/src/arg.rs +++ b/src/arg.rs @@ -40,8 +40,8 @@ pub mod val { pub const AWS: &str = "aws"; } -pub fn value<'a>(name: &str, matches: &'a ArgMatches<'a>) -> Result<&'a str, anyhow::Error> { - match matches.value_of(name) { +pub fn value<'a>(name: &str, matches: &'a ArgMatches) -> Result<&'a str, anyhow::Error> { + match matches.get_one::<&str>(name) { Some(query) => Ok(query), None => Err(anyhow!("Missing value for argument '{}'", name)), } diff --git a/src/cmd/connect.rs b/src/cmd/connect.rs index 9938bf5..9b92a30 100644 --- a/src/cmd/connect.rs +++ b/src/cmd/connect.rs @@ -8,12 +8,12 @@ use anyhow::{anyhow, Result}; use clap::ArgMatches; pub fn connect(matches: &ArgMatches) -> Result<(), anyhow::Error> { - if matches.is_present(arg::VERBOSE) { + if matches.contains_id(arg::VERBOSE) { println!("matches: {:#?}", matches); } - if matches.is_present(cmd::CONNECT) { - match matches.value_of(cmd::CONNECT) { + if matches.contains_id(cmd::CONNECT) { + match matches.get_one::<&str>(cmd::CONNECT) { Some(connection) => match connection.to_lowercase().as_str() { val::SMTP => { let _client = SmtpClient::new()?; diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 8c9bd91..0dc8656 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -4,7 +4,7 @@ use clap::ArgMatches; use std::{env, io}; pub fn init(matches: &ArgMatches) -> Result<(), anyhow::Error> { - if matches.is_present(arg::VERBOSE) { + if matches.contains_id(arg::VERBOSE) { println!("matches: {:#?}", matches); } diff --git a/src/cmd/query.rs b/src/cmd/query.rs index 6e99f94..0c99fe0 100644 --- a/src/cmd/query.rs +++ b/src/cmd/query.rs @@ -8,29 +8,29 @@ use clap::ArgMatches; use std::path::Path; pub fn query(matches: &ArgMatches) -> Result<(), anyhow::Error> { - if matches.is_present(arg::VERBOSE) { + if matches.contains_id(arg::VERBOSE) { println!("matches: {:#?}", matches); } - if matches.is_present(cmd::QUERY) { - match matches.value_of(cmd::QUERY) { + if matches.contains_id(cmd::QUERY) { + match matches.get_one::<&str>(cmd::QUERY) { Some(query) => { let now = Utc::now(); let conn_vars = ConnVars::from_env()?; - let ssh_tunnel = matches.value_of(arg::SSH_TUNNEL); + let ssh_tunnel = matches.get_one::<&str>(arg::SSH_TUNNEL).map(|arg| *arg); let connection = DbConnection::new(&conn_vars, ssh_tunnel)?; let mut df_query = sources::query_postgres(&connection, query)?; - if matches.is_present(arg::DISPLAY) { + if matches.contains_id(arg::DISPLAY) { println!("Display query result: {}", df_query); } - if matches.is_present(arg::SAVE) { + if matches.contains_id(arg::SAVE) { let save_dir = Path::new(arg::value(arg::SAVE_DIR, matches)?); // If argument 'FILE_TYPE' is not present the default value 'csv' will be used - match matches.value_of(arg::FILE_TYPE) { - Some(file_type) => match file_type { + match matches.get_one::<&str>(arg::FILE_TYPE) { + Some(file_type) => match *file_type { "csv" => { sources::write_csv(&mut df_query, save_dir, now)?; } diff --git a/src/cmd/read.rs b/src/cmd/read.rs index 47725d7..827fb31 100644 --- a/src/cmd/read.rs +++ b/src/cmd/read.rs @@ -4,17 +4,17 @@ use clap::ArgMatches; use std::path::PathBuf; pub fn read(matches: &ArgMatches) -> Result<(), anyhow::Error> { - if matches.is_present(arg::VERBOSE) { + if matches.contains_id(arg::VERBOSE) { println!("matches: {:#?}", matches); } - if matches.is_present(cmd::READ) { - match matches.value_of(cmd::READ) { + if matches.contains_id(cmd::READ) { + match matches.get_one::<&str>(cmd::READ) { Some(csv_file) => { let path = PathBuf::from(csv_file); let csv = sources::read_csv(&path)?; - if matches.is_present(arg::DISPLAY) { + if matches.contains_id(arg::DISPLAY) { println!("Display csv file: {}", csv); } diff --git a/src/cmd/send.rs b/src/cmd/send.rs index ac4cdef..f7b51c4 100644 --- a/src/cmd/send.rs +++ b/src/cmd/send.rs @@ -11,22 +11,22 @@ use clap::ArgMatches; use std::{io, path::Path, time::SystemTime}; pub fn send(matches: &ArgMatches) -> Result<(), anyhow::Error> { - if matches.is_present(arg::VERBOSE) { + if matches.contains_id(arg::VERBOSE) { println!("matches: {:#?}", matches); } let now = SystemTime::now(); - let dry_run = matches.is_present(arg::DRY_RUN); - let is_archived = matches.is_present(arg::ARCHIVE); + let dry_run = matches.contains_id(arg::DRY_RUN); + let is_archived = matches.contains_id(arg::ARCHIVE); let archive_dir = Path::new(arg::value(arg::ARCHIVE_DIR, matches)?); let sender = Sender(arg::value(arg::SENDER, matches)?); let receiver = Receiver(arg::value(arg::RECEIVER, matches)?); let message = Message::from_args(matches)?; - let attachment = matches.value_of(arg::ATTACHMENT).map(Path::new); + let attachment = matches.get_one::<&str>(arg::ATTACHMENT).map(Path::new); let mime_format = MimeFormat::new(sender, receiver, &message, attachment, now)?; let email = Email::new(sender, receiver, &message, &mime_format)?; - if matches.is_present(arg::DISPLAY) { + if matches.contains_id(arg::DISPLAY) { println!("Display email: {:#?}", email); } @@ -40,7 +40,7 @@ pub fn send(matches: &ArgMatches) -> Result<(), anyhow::Error> { println!("Sending email to 1 receiver ..."); - if matches.is_present(arg::ASSUME_YES) { + if matches.contains_id(arg::ASSUME_YES) { let sent_email = client.send(&email)?; sent_email.display_status(); @@ -62,7 +62,7 @@ pub fn send(matches: &ArgMatches) -> Result<(), anyhow::Error> { } } - if matches.is_present(arg::DRY_RUN) { + if matches.contains_id(arg::DRY_RUN) { println!("Email sent (dry run)"); } else { println!("Email sent"); diff --git a/src/cmd/send_bulk.rs b/src/cmd/send_bulk.rs index 8146951..a97ffb3 100644 --- a/src/cmd/send_bulk.rs +++ b/src/cmd/send_bulk.rs @@ -11,21 +11,21 @@ use clap::ArgMatches; use std::{io, path::Path}; pub fn send_bulk(matches: &ArgMatches) -> Result<(), anyhow::Error> { - if matches.is_present(arg::VERBOSE) { + if matches.contains_id(arg::VERBOSE) { println!("matches: {:#?}", matches); } - let dry_run = matches.is_present(arg::DRY_RUN); - let is_archived = matches.is_present(arg::ARCHIVE); + let dry_run = matches.contains_id(arg::DRY_RUN); + let is_archived = matches.contains_id(arg::ARCHIVE); let archive_dir = Path::new(arg::value(arg::ARCHIVE_DIR, matches)?); let sender = Sender(arg::value(arg::SENDER, matches)?); let receivers = BulkReceiver::from_args(matches)?; let message = Message::from_args(matches)?; - let attachment = matches.value_of(arg::ATTACHMENT).map(Path::new); + let attachment = matches.get_one::<&str>(arg::ATTACHMENT).map(Path::new); - let bulk_email = if matches.is_present(arg::PERSONALIZE) { - if let Some(personalized_columns) = matches.values_of(arg::PERSONALIZE) { - let personalized_columns = personalized_columns.collect::>(); + let bulk_email = if matches.contains_id(arg::PERSONALIZE) { + if let Some(personalized_columns) = matches.get_many::<&str>(arg::PERSONALIZE) { + let personalized_columns = personalized_columns.map(|arg| *arg).collect::>(); BulkEmail::new( sender, &receivers, @@ -42,7 +42,7 @@ pub fn send_bulk(matches: &ArgMatches) -> Result<(), anyhow::Error> { let client = Client::from_args(matches)?; let eml_formatter = EmlFormatter::new(archive_dir)?; - if matches.is_present(arg::DISPLAY) { + if matches.contains_id(arg::DISPLAY) { println!("Display emails: {:#?}", bulk_email); } @@ -50,7 +50,7 @@ pub fn send_bulk(matches: &ArgMatches) -> Result<(), anyhow::Error> { println!("Dry run: {}", format_green("activated")); } - if matches.is_present(arg::ASSUME_YES) { + if matches.contains_id(arg::ASSUME_YES) { process_emails( &client, &eml_formatter, diff --git a/src/cmd/simple_query.rs b/src/cmd/simple_query.rs index c9426cc..209def8 100644 --- a/src/cmd/simple_query.rs +++ b/src/cmd/simple_query.rs @@ -4,13 +4,13 @@ use clap::ArgMatches; use postgres::{Client, NoTls, SimpleQueryMessage}; pub fn simple_query(matches: &ArgMatches) -> Result<(), anyhow::Error> { - if matches.is_present(arg::VERBOSE) { + if matches.contains_id(arg::VERBOSE) { println!("matches: {:#?}", matches); } - if matches.is_present(cmd::SIMPLE_QUERY) { + if matches.contains_id(cmd::SIMPLE_QUERY) { let conn_vars = ConnVars::from_env()?; - let simple_query = match matches.value_of(cmd::SIMPLE_QUERY) { + let simple_query = match matches.get_one::<&str>(cmd::SIMPLE_QUERY) { Some(query) => query, None => { return Err(anyhow!( diff --git a/src/email_builder/message.rs b/src/email_builder/message.rs index 35a1e2d..a7c8c05 100644 --- a/src/email_builder/message.rs +++ b/src/email_builder/message.rs @@ -41,13 +41,13 @@ impl Message { } pub fn from_args(matches: &ArgMatches) -> Result { - if matches.is_present(arg::SUBJECT) && matches.is_present(arg::CONTENT) { + if matches.contains_id(arg::SUBJECT) && matches.contains_id(arg::CONTENT) { match ( - matches.value_of(arg::SUBJECT), - matches.value_of(arg::CONTENT), + matches.get_one::<&str>(arg::SUBJECT), + matches.get_one::<&str>(arg::CONTENT), ) { (Some(subject), Some(content)) => { - let message = Message::new(subject, Some(content), None); + let message = Message::new(*subject, Some(content), None); Ok(message) } (Some(_), None) => Err(anyhow!("Missing value for argument '{}'", arg::CONTENT)), @@ -58,22 +58,22 @@ impl Message { arg::CONTENT )), } - } else if matches.is_present(arg::MESSAGE_FILE) { + } else if matches.contains_id(arg::MESSAGE_FILE) { let message_file = arg::value(arg::MESSAGE_FILE, matches)?; let message_path = Path::new(message_file); let message = Message::read_yaml(message_path)?; - if matches.is_present(arg::DISPLAY) { + if matches.contains_id(arg::DISPLAY) { println!("Display message file: {:#?}", message); } Ok(message) - } else if matches.is_present(arg::SUBJECT) - && (matches.is_present(arg::TEXT_FILE) || matches.is_present(arg::HTML_FILE)) + } else if matches.contains_id(arg::SUBJECT) + && (matches.contains_id(arg::TEXT_FILE) || matches.contains_id(arg::HTML_FILE)) { let subject = arg::value(arg::SUBJECT, matches)?; - let text_path = matches.value_of(arg::TEXT_FILE).map(Path::new); - let html_path = matches.value_of(arg::HTML_FILE).map(Path::new); + let text_path = matches.get_one::<&str>(arg::TEXT_FILE).map(Path::new); + let html_path = matches.get_one::<&str>(arg::HTML_FILE).map(Path::new); let text = if let Some(path) = text_path { Some(utils::read_file(path)?) } else { diff --git a/src/email_builder/receiver.rs b/src/email_builder/receiver.rs index 9b79173..71235e7 100644 --- a/src/email_builder/receiver.rs +++ b/src/email_builder/receiver.rs @@ -36,17 +36,17 @@ impl BulkReceiver { pub fn from_args(matches: &ArgMatches) -> Result { let column_name = arg::value(arg::RECEIVER_COLUMN, matches)?; - let receiver_query = matches.value_of(arg::RECEIVER_QUERY); - let receiver_path = matches.value_of(arg::RECEIVER_FILE).map(Path::new); + let receiver_query = matches.get_one::<&str>(arg::RECEIVER_QUERY); + let receiver_path = matches.get_one::<&str>(arg::RECEIVER_FILE).map(Path::new); match (receiver_query, receiver_path) { (Some(query), None) => { let conn_vars = ConnVars::from_env()?; - let ssh_tunnel = matches.value_of(arg::SSH_TUNNEL); + let ssh_tunnel = matches.get_one::<&str>(arg::SSH_TUNNEL).map(|arg| *arg); let connection = DbConnection::new(&conn_vars, ssh_tunnel)?; let df_receiver = sources::query_postgres(&connection, query)?; - if matches.is_present(arg::DISPLAY) { + if matches.contains_id(arg::DISPLAY) { println!("Display query result: {}", df_receiver); } @@ -58,7 +58,7 @@ impl BulkReceiver { (None, Some(path)) => { let df_receiver = sources::read_csv(path)?; - if matches.is_present(arg::DISPLAY) { + if matches.contains_id(arg::DISPLAY) { println!("Display csv file: {}", df_receiver); } diff --git a/src/email_provider/aws.rs b/src/email_provider/aws.rs index 1643c42..ce1dc59 100644 --- a/src/email_provider/aws.rs +++ b/src/email_provider/aws.rs @@ -28,7 +28,7 @@ impl AwsSesClient { let region_name = region.name().to_string(); // Check if AWS access keys are set in environment - if matches.is_present(arg::DRY_RUN) { + if matches.contains_id(arg::DRY_RUN) { AwsSesClient::get_credentials(&provider).context( "Missing environment variable 'AWS_ACCESS_KEY_ID' and/or 'AWS_SECRET_ACCESS_KEY'", )?; diff --git a/src/email_transmission/client.rs b/src/email_transmission/client.rs index 547fe3d..81cbfee 100644 --- a/src/email_transmission/client.rs +++ b/src/email_transmission/client.rs @@ -42,7 +42,7 @@ impl<'a> Client<'a> { } pub fn from_args(matches: &ArgMatches) -> Result { - if matches.is_present(arg::DRY_RUN) { + if matches.contains_id(arg::DRY_RUN) { let client = MockClient; return Ok(Client::new(TransmissionType::Dry, Box::new(client))); } diff --git a/src/main.rs b/src/main.rs index f5683cb..9ff63f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ fn main() -> Result<(), anyhow::Error> { let app = app(); let matches = app.get_matches(); - if matches.is_present(arg::VERBOSE) { + if matches.contains_id(arg::VERBOSE) { println!("matches: {:#?}", matches); }