Skip to content

Commit

Permalink
Update clap
Browse files Browse the repository at this point in the history
  • Loading branch information
quambene committed Apr 3, 2024
1 parent 7cda127 commit e502407
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
16 changes: 8 additions & 8 deletions src/cmd/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
14 changes: 7 additions & 7 deletions src/cmd/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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();

Expand All @@ -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");
Expand Down
18 changes: 9 additions & 9 deletions src/cmd/send_bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<&str>>();
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::<Vec<_>>();
BulkEmail::new(
sender,
&receivers,
Expand All @@ -42,15 +42,15 @@ 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);
}

if dry_run {
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,
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/simple_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
20 changes: 10 additions & 10 deletions src/email_builder/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ impl Message {
}

pub fn from_args(matches: &ArgMatches) -> Result<Self, anyhow::Error> {
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)),
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions src/email_builder/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ impl BulkReceiver {

pub fn from_args(matches: &ArgMatches) -> Result<Self, anyhow::Error> {
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);
}

Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/email_provider/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
)?;
Expand Down
2 changes: 1 addition & 1 deletion src/email_transmission/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'a> Client<'a> {
}

pub fn from_args(matches: &ArgMatches) -> Result<Self, anyhow::Error> {
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)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit e502407

Please sign in to comment.