chore: moved cli tasks under cli dir

also they're optional, turn off the feature flag if u dont want them
This commit is contained in:
əlemi 2024-05-06 01:09:14 +02:00
parent 854fd95a4d
commit c56203bf57
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 93 additions and 73 deletions

View file

@ -46,6 +46,7 @@ time = { version = "0.3", features = ["serde"], optional = true }
async-recursion = "1.1" async-recursion = "1.1"
[features] [features]
default = ["migrations"] default = ["migrations", "cli"]
cli = []
migrations = ["dep:sea-orm-migration"] migrations = ["dep:sea-orm-migration"]
mastodon = ["dep:mastodon-async-entities", "dep:time"] mastodon = ["dep:mastodon-async-entities", "dep:time"]

View file

@ -15,3 +15,73 @@ pub use register::*;
mod update; mod update;
pub use 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?),
}
}

View file

@ -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 pub mod server; // TODO there are some methods that i dont use yet, make it public so that ra shuts up
mod model; mod model;
mod routes; mod routes;
mod cli;
mod errors; mod errors;
mod config;
#[cfg(feature = "cli")]
mod cli;
#[cfg(feature = "migrations")] #[cfg(feature = "migrations")]
mod migrations; mod migrations;
@ -21,10 +25,10 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Parser)] #[derive(Parser)]
/// all names were taken /// all names were taken
struct CliArgs { struct Args {
#[clap(subcommand)] #[clap(subcommand)]
/// command to run /// command to run
command: CliCommand, command: Mode,
#[arg(short = 'd', long = "db", default_value = "sqlite://./upub.db")] #[arg(short = 'd', long = "db", default_value = "sqlite://./upub.db")]
/// database connection uri /// database connection uri
@ -40,7 +44,7 @@ struct CliArgs {
} }
#[derive(Clone, Subcommand)] #[derive(Clone, Subcommand)]
enum CliCommand { enum Mode {
/// run fediverse server /// run fediverse server
Serve , Serve ,
@ -48,59 +52,19 @@ enum CliCommand {
/// apply database migrations /// apply database migrations
Migrate, Migrate,
/// generate fake user, note and activity #[cfg(feature = "cli")]
Faker{ /// run maintenance CLI tasks
/// how many fake statuses to insert for root user Cli {
count: u64, #[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] #[tokio::main]
async fn main() { async fn main() {
let args = CliArgs::parse(); let args = Args::parse();
tracing_subscriber::fmt() tracing_subscriber::fmt()
.compact() .compact()
@ -118,31 +82,16 @@ async fn main() {
match args.command { match args.command {
#[cfg(feature = "migrations")] #[cfg(feature = "migrations")]
CliCommand::Migrate => Mode::Migrate =>
migrations::Migrator::up(&db, None) migrations::Migrator::up(&db, None)
.await.expect("error applying migrations"), .await.expect("error applying migrations"),
CliCommand::Faker { count } => #[cfg(feature = "cli")]
cli::faker(&db, args.domain, count) Mode::Cli { command } =>
.await.expect("error creating fake entities"), cli::run(command, db, args.domain)
.await.expect("failed running cli task"),
CliCommand::Fetch { uri, save } => Mode::Serve => {
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 => {
let ctx = server::Context::new(db, args.domain) let ctx = server::Context::new(db, args.domain)
.await.expect("failed creating server context"); .await.expect("failed creating server context");