Skip to content

Commit

Permalink
Update clap (#8)
Browse files Browse the repository at this point in the history
* Update dependency

* Update clap

* Update clap II

* Fix args

* Fix tests

* Add tests

* Fix tests
  • Loading branch information
quambene authored Apr 4, 2024
1 parent 6202728 commit 611fe53
Show file tree
Hide file tree
Showing 16 changed files with 383 additions and 252 deletions.
122 changes: 72 additions & 50 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9.34"
tokio = "1.37"
csv = "1.3"
clap = "2.33"
clap = { version = "4.5.4", features = ["cargo"] }
chrono = "0.4"
polars = { version = "0.32", features = ["dtype-u8"] }
connectorx = { version = "0.3.2", features = ["src_postgres", "dst_arrow2"] }
Expand Down
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::<String>(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::<String>(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
18 changes: 10 additions & 8 deletions src/cmd/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@ 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::<String>(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::<String>(arg::SSH_TUNNEL)
.map(|arg| arg.as_ref());
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::<String>(arg::FILE_TYPE) {
Some(file_type) => match file_type.as_ref() {
"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::<String>(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::<String>(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
Loading

0 comments on commit 611fe53

Please sign in to comment.