Skip to content

Commit

Permalink
Introduce features in rust-pgdatadiff-client
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlospt committed Mar 15, 2024
1 parent c1957df commit 40fd768
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 45 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,20 @@ _The benchmarks below are based on DBs with 5 tables and 1M rows each. The resul

In case you want to use this as a client you can install it through `cargo`:

Client supports two features that allow you to choose between `Clap` or `Inquire` for running it.

[![asciicast](https://asciinema.org/a/647065.svg)](https://asciinema.org/a/647065)

## Clap

```shell
cargo install rust-pgdatadiff-client --features with-clap
```

## Inquire

```shell
cargo install rust-pgdatadiff-client
cargo install rust-pgdatadiff-client // or with `--features with-inquire`
```

# Installation (Library)
Expand All @@ -48,6 +60,14 @@ rust-pgdatadiff = "0.1.0"

# Usage (Client)

## Clap

```shell
cargo run --features with-clap diff \
"postgresql://localhost:5438?dbname=example&user=postgres&password=postgres" \
"postgresql://localhost:5439?dbname=example&user=postgres&password=postgres"
```

```
Usage: rust-pgdatadiff-client diff [OPTIONS] <FIRST_DB> <SECOND_DB>
Expand Down
11 changes: 8 additions & 3 deletions rust-pgdatadiff-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-pgdatadiff-client"
version = "0.1.2"
version = "0.1.5"
edition = "2021"
license = "MIT"
description = "Rust client for comparing two PostgreSQL databases"
Expand All @@ -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"]
with-inquire = ["dep:inquire"]
with-clap = ["dep:clap"]
113 changes: 72 additions & 41 deletions rust-pgdatadiff-client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use anyhow::Result;
use clap::{Parser, Subcommand};

#[cfg(not(feature = "with-clap"))]
use inquire::{Confirm, Text};

#[cfg(feature = "with-clap")]
use clap::{Parser, Subcommand};
use rust_pgdatadiff::diff::diff_ops::Differ;
use rust_pgdatadiff::diff::diff_payload::DiffPayload;

#[cfg(feature = "with-clap")]
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
Expand All @@ -12,6 +17,7 @@ struct Cli {
command: Commands,
}

#[cfg(feature = "with-clap")]
#[derive(Subcommand)]
enum Commands {
#[command(about = "Print the version")]
Expand Down Expand Up @@ -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(not(feature = "with-clap"))]
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")
Expand Down Expand Up @@ -136,10 +143,34 @@ async fn main() -> Result<()> {
only_count,
chunk_size.parse::<i64>().unwrap(),
max_connections.parse::<i64>().unwrap(),
include_tables.trim().split_whitespace().into_iter().collect(),
exclude_tables.trim().split_whitespace().into_iter().collect(),
include_tables

Check failure on line 146 in rust-pgdatadiff-client/src/main.rs

View workflow job for this annotation

GitHub Actions / Cargo format & Clippy

useless conversion to the same type: `std::str::SplitWhitespace<'_>`
.trim()

Check failure on line 147 in rust-pgdatadiff-client/src/main.rs

View workflow job for this annotation

GitHub Actions / Cargo format & Clippy

found call to `str::trim` before `str::split_whitespace`
.split_whitespace()
.into_iter()
.collect(),
exclude_tables

Check failure on line 151 in rust-pgdatadiff-client/src/main.rs

View workflow job for this annotation

GitHub Actions / Cargo format & Clippy

useless conversion to the same type: `std::str::SplitWhitespace<'_>`
.trim()

Check failure on line 152 in rust-pgdatadiff-client/src/main.rs

View workflow job for this annotation

GitHub Actions / Cargo format & Clippy

found call to `str::trim` before `str::split_whitespace`
.split_whitespace()
.into_iter()
.collect(),
schema_name,
);
let _ = Differ::diff_dbs(payload).await;
Ok(())
}

#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();

#[cfg(feature = "with-clap")]
{
_ = main_clap().await;
}
#[cfg(not(feature = "with-clap"))]
{
_ = main_inquire().await;
}

Ok(())
}

0 comments on commit 40fd768

Please sign in to comment.