From c56203bf5721c8ff46a21a8ac47c4026092bf409 Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 6 May 2024 01:09:14 +0200 Subject: [PATCH] chore: moved cli tasks under cli dir also they're optional, turn off the feature flag if u dont want them --- Cargo.toml | 3 +- src/cli/mod.rs | 70 +++++++++++++++++++++++++++++++++++++ src/main.rs | 93 ++++++++++++-------------------------------------- 3 files changed, 93 insertions(+), 73 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d7c4b87..039cd4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ time = { version = "0.3", features = ["serde"], optional = true } async-recursion = "1.1" [features] -default = ["migrations"] +default = ["migrations", "cli"] +cli = [] migrations = ["dep:sea-orm-migration"] mastodon = ["dep:mastodon-async-entities", "dep:time"] diff --git a/src/cli/mod.rs b/src/cli/mod.rs index e107431..ecc576e 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -15,3 +15,73 @@ pub use register::*; mod update; pub use update::*; + +#[derive(Debug, Clone, clap::Subcommand)] +pub enum CliCommand { + /// generate fake user, note and activity + Faker{ + /// how many fake statuses to insert for root user + count: u64, + }, + + /// fetch a single AP object + Fetch { + /// object id, or uri, to fetch + uri: String, + + #[arg(long, default_value_t = false)] + /// store fetched object in local db + save: bool, + }, + + /// follow a remote relay + Relay { + /// actor url, same as with pleroma + actor: String, + + #[arg(long, default_value_t = false)] + /// instead of sending a follow request, send an accept + accept: bool + }, + + /// run db maintenance tasks + Fix { + #[arg(long, default_value_t = false)] + /// fix likes counts for posts + likes: bool, + + #[arg(long, default_value_t = false)] + /// fix shares counts for posts + shares: bool, + + #[arg(long, default_value_t = false)] + /// fix replies counts for posts + replies: bool, + }, + + /// update remote users + Update { + #[arg(long, short, default_value_t = 7)] + /// number of days after which users should get updated + days: i64, + } +} + +pub async fn run( + command: CliCommand, + db: sea_orm::DatabaseConnection, + domain: String, +) -> crate::Result<()> { + match command { + CliCommand::Faker { count } => + Ok(faker(&db, domain, count).await?), + CliCommand::Fetch { uri, save } => + Ok(fetch(db, domain, uri, save).await?), + CliCommand::Relay { actor, accept } => + Ok(relay(db, domain, actor, accept).await?), + CliCommand::Fix { likes, shares, replies } => + Ok(fix(db, likes, shares, replies).await?), + CliCommand::Update { days } => + Ok(update_users(db, domain, days).await?), + } +} diff --git a/src/main.rs b/src/main.rs index 937f586..aeaab1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,14 @@ pub mod server; // TODO there are some methods that i dont use yet, make it public so that ra shuts up mod model; mod routes; -mod cli; mod errors; +mod config; + +#[cfg(feature = "cli")] +mod cli; + #[cfg(feature = "migrations")] mod migrations; @@ -21,10 +25,10 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION"); #[derive(Parser)] /// all names were taken -struct CliArgs { +struct Args { #[clap(subcommand)] /// command to run - command: CliCommand, + command: Mode, #[arg(short = 'd', long = "db", default_value = "sqlite://./upub.db")] /// database connection uri @@ -40,7 +44,7 @@ struct CliArgs { } #[derive(Clone, Subcommand)] -enum CliCommand { +enum Mode { /// run fediverse server Serve , @@ -48,59 +52,19 @@ enum CliCommand { /// apply database migrations Migrate, - /// generate fake user, note and activity - Faker{ - /// how many fake statuses to insert for root user - count: u64, + #[cfg(feature = "cli")] + /// run maintenance CLI tasks + Cli { + #[clap(subcommand)] + /// task to run + command: cli::CliCommand, }, - - /// fetch a single AP object - Fetch { - /// object id, or uri, to fetch - uri: String, - - #[arg(long, default_value_t = false)] - /// store fetched object in local db - save: bool, - }, - - /// follow a remote relay - Relay { - /// actor url, same as with pleroma - actor: String, - - #[arg(long, default_value_t = false)] - /// instead of sending a follow request, send an accept - accept: bool - }, - - /// run db maintenance tasks - Fix { - #[arg(long, default_value_t = false)] - /// fix likes counts for posts - likes: bool, - - #[arg(long, default_value_t = false)] - /// fix shares counts for posts - shares: bool, - - #[arg(long, default_value_t = false)] - /// fix replies counts for posts - replies: bool, - }, - - /// update remote users - Update { - #[arg(long, short, default_value_t = 7)] - /// number of days after which users should get updated - days: i64, - } } #[tokio::main] async fn main() { - let args = CliArgs::parse(); + let args = Args::parse(); tracing_subscriber::fmt() .compact() @@ -118,31 +82,16 @@ async fn main() { match args.command { #[cfg(feature = "migrations")] - CliCommand::Migrate => + Mode::Migrate => migrations::Migrator::up(&db, None) .await.expect("error applying migrations"), - CliCommand::Faker { count } => - cli::faker(&db, args.domain, count) - .await.expect("error creating fake entities"), + #[cfg(feature = "cli")] + Mode::Cli { command } => + cli::run(command, db, args.domain) + .await.expect("failed running cli task"), - CliCommand::Fetch { uri, save } => - cli::fetch(db, args.domain, uri, save) - .await.expect("error fetching object"), - - CliCommand::Relay { actor, accept } => - cli::relay(db, args.domain, actor, accept) - .await.expect("error registering/accepting relay"), - - CliCommand::Fix { likes, shares, replies } => - cli::fix(db, likes, shares, replies) - .await.expect("failed running fix task"), - - CliCommand::Update { days } => - cli::update_users(db, args.domain, days) - .await.expect("error updating users"), - - CliCommand::Serve => { + Mode::Serve => { let ctx = server::Context::new(db, args.domain) .await.expect("failed creating server context");