use std::{collections::HashMap, error::Error};
use clap::{CommandFactory, Parser, Subcommand};
#[derive(Parser)]
#[command(name = env!("CARGO_PKG_NAME"), version, about = "Aurelin application commands")]
struct Cli {
#[command(subcommand)]
command: Option<Command>,
}
#[derive(Subcommand)]
enum Command {
List,
Serve,
Migrate {
#[arg(long)]
force: bool,
},
#[command(name = "migrate:status")]
MigrateStatus,
#[command(name = "migrate:rollback")]
MigrateRollback {
#[arg(long)]
force: bool,
},
#[command(name = "migrate:fresh")]
MigrateFresh {
#[arg(long)]
force: bool,
},
#[command(name = "db:seed")]
DbSeed {
#[arg(long)]
force: bool,
},
#[command(name = "queue:work")]
QueueWork,
#[command(name = "schedule:run")]
ScheduleRun,
#[command(name = "queue:failed")]
QueueFailed {
#[arg(long)]
queue: Option<String>,
},
#[command(name = "queue:retry")]
QueueRetry {
#[arg(num_args = 1..)]
ids: Vec<String>,
},
#[command(name = "queue:forget")]
QueueForget {
#[arg(num_args = 1..)]
ids: Vec<String>,
},
#[command(name = "queue:flush")]
QueueFlush {
#[arg(long)]
hours: Option<u64>,
#[arg(long)]
queue: Option<String>,
#[arg(long)]
force: bool,
},
}
pub async fn run(environment: HashMap<String, String>) -> Result<(), Box<dyn Error>> {
match Cli::parse().command {
None | Some(Command::List) => {
Cli::command().print_help()?;
println!();
Ok(())
}
Some(Command::Serve) => crate::serve(environment).await,
Some(Command::Migrate { force }) => {
ensure_safe_environment(&environment, force)?;
crate::database::migrate(database_url(&environment)).await
}
Some(Command::MigrateStatus) => {
crate::database::migration_status(database_url(&environment)).await
}
Some(Command::MigrateRollback { force }) => {
ensure_safe_environment(&environment, force)?;
crate::database::rollback(database_url(&environment)).await
}
Some(Command::MigrateFresh { force }) => {
ensure_safe_environment(&environment, force)?;
crate::database::fresh(database_url(&environment)).await
}
Some(Command::DbSeed { force }) => {
if environment.get("APP_ENV").map(String::as_str) == Some("production") && !force {
return Err("refusing to seed in production without --force".into());
}
crate::seed::run(database_url(&environment)).await
}
Some(Command::QueueWork) => {
let mailer = aurelin_framework::mail::Mailer::from_environment(&environment)?;
crate::jobs::work(database_url(&environment), mailer).await
}
Some(Command::ScheduleRun) => {
aurelin_framework::scheduling::commands::run(&crate::schedule::schedule()?).await?;
Ok(())
}
Some(Command::QueueFailed { queue }) => {
aurelin_framework::queue::commands::failed(database_url(&environment), queue.as_deref())
.await
.map_err(Into::into)
}
Some(Command::QueueRetry { ids }) => {
aurelin_framework::queue::commands::retry(database_url(&environment), &ids)
.await
.map_err(Into::into)
}
Some(Command::QueueForget { ids }) => {
aurelin_framework::queue::commands::forget(database_url(&environment), &ids)
.await
.map_err(Into::into)
}
Some(Command::QueueFlush {
hours,
queue,
force,
}) => {
ensure_safe_queue_flush(&environment, force)?;
aurelin_framework::queue::commands::flush(
database_url(&environment),
hours,
queue.as_deref(),
)
.await
.map_err(Into::into)
}
}
}
fn ensure_safe_environment(
environment: &HashMap<String, String>,
force: bool,
) -> Result<(), Box<dyn Error>> {
if environment.get("APP_ENV").map(String::as_str) == Some("production") && !force {
return Err("refusing to run migrations in production without --force".into());
}
Ok(())
}
fn ensure_safe_queue_flush(
environment: &HashMap<String, String>,
force: bool,
) -> Result<(), Box<dyn Error>> {
if environment.get("APP_ENV").map(String::as_str) == Some("production") && !force {
return Err("refusing to flush failed jobs in production without --force".into());
}
Ok(())
}
fn database_url(environment: &HashMap<String, String>) -> &str {
environment
.get("DATABASE_URL")
.map(String::as_str)
.unwrap_or("sqlite://database/database.sqlite?mode=rwc")
}