Skip to content

Commit

Permalink
feat: improve cli interface
Browse files Browse the repository at this point in the history
These are still technically integration tests. They should be moved

Bitch, you gave me the fucking clap
  • Loading branch information
jasonribble committed Jul 20, 2024
1 parent d151a67 commit 2f67770
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 25 deletions.
116 changes: 115 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.86"
async-trait = "0.1.80"
clap = { version = "4.5.9", features = ["derive"] }
dotenvy = "0.15.0"
regex = "1.5.4"
sqlx = { version = "0.7.4", features = ["runtime-tokio-native-tls", "postgres"] }
Expand Down
56 changes: 56 additions & 0 deletions src/commander.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use clap::{Args, Parser, Subcommand};

#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}

#[derive(Subcommand)]
pub enum Commands {
/// Create a contact
Create(CreateCommand),

/// Edit a contact by ID
Edit(EditCommand),
}

#[derive(Args)]
pub struct CreateCommand {
#[arg(short, long, value_name = "First Name")]
pub first_name: Option<String>,

#[arg(short, long, value_name = "Last Name")]
pub last_name: Option<String>,

#[arg(short, long, value_name = "Display Name")]
pub display_name: Option<String>,

#[arg(short, long)]
pub email: Option<String>,

#[arg(short, long, value_name = "Phone")]
pub phone_number: Option<String>,
}

#[derive(Args, Debug)]
pub struct EditCommand {
/// ID of contact to edit
id: i64,

#[arg(short, long, value_name = "First Name")]
first_name: Option<String>,

#[arg(short, long, value_name = "Last Name")]
last_name: Option<String>,

#[arg(short, long, value_name = "Display Name")]
display_name: Option<String>,

#[arg(short, long)]
email: Option<String>,

#[arg(short, long, value_name = "Phone")]
phone_number: Option<String>,
}
2 changes: 0 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub enum AppError {
DatabaseError(sqlx::Error),
InvalidEmail(String),
InvalidPhoneNumber(String),
InvalidArguments,
}

impl fmt::Display for AppError {
Expand All @@ -14,7 +13,6 @@ impl fmt::Display for AppError {
Self::DatabaseError(error) => write!(f, "Database error: {error}"),
Self::InvalidEmail(email) => write!(f, "{email} is invalid."),
Self::InvalidPhoneNumber(phone) => write!(f, "{phone} is invalid."),
Self::InvalidArguments => write!(f, "Invalid argument"),
}
}
}
Expand Down
Loading

0 comments on commit 2f67770

Please sign in to comment.