From 4c8546f81eabb6b8bb72a67770bc2093f9c4800e Mon Sep 17 00:00:00 2001 From: EETagent Date: Sun, 30 Oct 2022 10:50:58 +0100 Subject: [PATCH] feat: init recovery CLI tool --- Cargo.toml | 2 +- recovery/Cargo.toml | 25 ++++++++++++++++++++ recovery/src/main.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 recovery/Cargo.toml create mode 100644 recovery/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index d159131..09361d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" publish = false [workspace] -members = [".", "api", "core", "entity", "migration"] +members = [".", "api", "core", "entity", "migration", "recovery"] [dependencies] portfolio-api = { path = "api" } diff --git a/recovery/Cargo.toml b/recovery/Cargo.toml new file mode 100644 index 0000000..d2ecc52 --- /dev/null +++ b/recovery/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "portfolio-recovery" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +url = "^2.3" +clap = { version = "^4.0", features = ["cargo"] } + +portfolio-entity = { path = "../entity" } + +[dependencies.tokio] +version = "^1.21" +features = [ + "macros", +] + +[dependencies.sea-orm] +version = "^0.10" +features = [ + "sqlx-sqlite", + # TODO: Migrate to rustls for better compatibility with various OS + "runtime-tokio-native-tls" +] diff --git a/recovery/src/main.rs b/recovery/src/main.rs new file mode 100644 index 0000000..aa831b5 --- /dev/null +++ b/recovery/src/main.rs @@ -0,0 +1,55 @@ +use std::path::PathBuf; +use url::Url; + +use clap::{arg, command, value_parser}; +use sea_orm::{Database, DatabaseConnection}; + +use sea_orm::*; +use ::entity::candidate::Entity as Candidate; +use ::entity::parent::Entity as Parent; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let clap = command!() + .arg(arg!([name] "Path to the db .sql backup")) + .arg( + arg!( + -d --database "Path to the database SQL backup file" + ) + .required(true) + .value_parser(value_parser!(PathBuf)), + ) + .arg( + arg!( + -p --portfolio "Path to the portfolio root" + ) + .required(true) + .value_parser(value_parser!(PathBuf)), + ) + .arg( + arg!( + -k --key "AGE private key for decryption" + ) + .required(true), + ) + .get_matches(); + + let mut sqlite_url = Url::from_file_path(clap.get_one::("DATABASE").unwrap()).unwrap(); + sqlite_url.set_scheme("sqlite").unwrap(); + + let db: DatabaseConnection = Database::connect(sqlite_url.as_str()).await?; + + let entries = Candidate::find() + .join_rev( + JoinType::InnerJoin, + Parent::belongs_to(Candidate) + .from(::entity::parent::Column::Application) + .to(::entity::candidate::Column::Application) + .into(), + ) + .all(&db) + .await?; + + Ok(()) + +}