From 3356808cbae0f230c598f9134e1c3ecc060fbcea Mon Sep 17 00:00:00 2001 From: pavlospt Date: Fri, 15 Mar 2024 14:47:38 +0200 Subject: [PATCH] Introduce features in rust-pgdatadiff-client --- rust-pgdatadiff-client/Cargo.toml | 9 ++- rust-pgdatadiff-client/src/main.rs | 103 ++++++++++++++++++----------- 2 files changed, 70 insertions(+), 42 deletions(-) diff --git a/rust-pgdatadiff-client/Cargo.toml b/rust-pgdatadiff-client/Cargo.toml index b182b6b..de21194 100644 --- a/rust-pgdatadiff-client/Cargo.toml +++ b/rust-pgdatadiff-client/Cargo.toml @@ -12,8 +12,13 @@ documentation = "https://docs.rs/rust-pgdatadiff-client" [dependencies] anyhow = "1.0.81" -clap = { version = "4.5.2", features = ["derive"] } +clap = { version = "4.5.2", features = ["derive"], optional = true } tokio = "1.36.0" env_logger = "0.11.3" rust-pgdatadiff = { version = "0.1.2", path = ".." } -inquire = "0.7.1" +inquire = { version = "0.7.1", optional = true } + +[features] +default = [] +with-inquire = ["dep:inquire"] +with-clap = ["dep:clap"] diff --git a/rust-pgdatadiff-client/src/main.rs b/rust-pgdatadiff-client/src/main.rs index 3d120be..db8c361 100644 --- a/rust-pgdatadiff-client/src/main.rs +++ b/rust-pgdatadiff-client/src/main.rs @@ -1,9 +1,14 @@ use anyhow::Result; -use clap::{Parser, Subcommand}; -use inquire::{Confirm, Text}; use rust_pgdatadiff::diff::diff_ops::Differ; use rust_pgdatadiff::diff::diff_payload::DiffPayload; +#[cfg(feature = "with-inquire")] +use inquire::{Confirm, Text}; + +#[cfg(feature = "with-clap")] +use clap::{Parser, Subcommand}; + +#[cfg(feature = "with-clap")] #[derive(Parser)] #[command(author, version, about, long_about = None)] #[command(propagate_version = true)] @@ -12,6 +17,7 @@ struct Cli { command: Commands, } +#[cfg(feature = "with-clap")] #[derive(Subcommand)] enum Commands { #[command(about = "Print the version")] @@ -48,45 +54,46 @@ enum Commands { }, } -#[tokio::main] -async fn main() -> Result<()> { - env_logger::init(); - - // let cli = Cli::parse(); - // match &cli.command { - // Commands::Version => { - // println!("Version: {}", env!("CARGO_PKG_VERSION")); - // Ok(()) - // } - // Commands::Diff { - // first_db, - // second_db, - // only_tables, - // only_sequences, - // only_count, - // chunk_size, - // max_connections, - // include_tables, - // exclude_tables, - // schema_name, - // } => { - // let payload = DiffPayload::new( - // first_db.clone(), - // second_db.clone(), - // *only_tables, - // *only_sequences, - // *only_count, - // *chunk_size, - // *max_connections, - // include_tables.to_vec(), - // exclude_tables.to_vec(), - // schema_name.clone(), - // ); - // let _ = Differ::diff_dbs(payload).await; - // Ok(()) - // } - // } +#[cfg(feature = "with-clap")] +async fn main_clap() -> Result<()> { + let cli = Cli::parse(); + match &cli.command { + Commands::Version => { + println!("Version: {}", env!("CARGO_PKG_VERSION")); + Ok(()) + } + Commands::Diff { + first_db, + second_db, + only_tables, + only_sequences, + only_count, + chunk_size, + max_connections, + include_tables, + exclude_tables, + schema_name, + } => { + let payload = DiffPayload::new( + first_db.clone(), + second_db.clone(), + *only_tables, + *only_sequences, + *only_count, + *chunk_size, + *max_connections, + include_tables.to_vec(), + exclude_tables.to_vec(), + schema_name.clone(), + ); + let _ = Differ::diff_dbs(payload).await; + Ok(()) + } + } +} +#[cfg(feature = "with-inquire")] +async fn main_inquire() -> Result<()> { let first_db = Text::new("First DB") .with_default("postgres://postgres:postgres@localhost:5438/example") .with_help_message("Enter the first database connection string") @@ -143,3 +150,19 @@ async fn main() -> Result<()> { let _ = Differ::diff_dbs(payload).await; Ok(()) } + +#[tokio::main] +async fn main() -> Result<()> { + env_logger::init(); + + #[cfg(feature = "with-clap")] + { + _ = main_clap().await; + } + #[cfg(feature = "with-inquire")] + { + _ = main_inquire().await; + } + + Ok(()) +}