120 lines
3.0 KiB
Rust
120 lines
3.0 KiB
Rust
//! All logic for running the CLI.
|
|
|
|
use {
|
|
async_std::fs::create_dir_all, clap::Parser, color_eyre::Result,
|
|
sea_orm_migration::MigratorTrait, tracing::info,
|
|
};
|
|
|
|
use crate::{
|
|
charts::UserCountChart,
|
|
cli::{
|
|
Cli, MainSubcommands, MigrateSubcommands, SnapshotSubcommands,
|
|
WebSubcommands,
|
|
},
|
|
group_data::GroupDataModel,
|
|
migrations::Migrator,
|
|
scss::generate_css,
|
|
snapshots::SnapshotModel,
|
|
templates::HomeTemplate,
|
|
utilities::{create_db, today},
|
|
};
|
|
|
|
/// Run the CLI.
|
|
pub async fn run() -> Result<()> {
|
|
let cli = Cli::parse();
|
|
let db = create_db(cli.sql_logging).await?;
|
|
|
|
if !cli.no_migrate {
|
|
Migrator::up(&db, None).await?;
|
|
}
|
|
|
|
match cli.command {
|
|
MainSubcommands::Migrate {
|
|
command: migrate_command,
|
|
} => match migrate_command {
|
|
MigrateSubcommands::Down { amount } => {
|
|
Migrator::down(&db, Some(amount)).await?;
|
|
}
|
|
|
|
MigrateSubcommands::Status => {
|
|
Migrator::status(&db).await?;
|
|
}
|
|
|
|
MigrateSubcommands::Up { amount } => {
|
|
Migrator::up(&db, Some(amount)).await?;
|
|
}
|
|
},
|
|
|
|
MainSubcommands::Snapshot {
|
|
command: snapshot_command,
|
|
} => match snapshot_command {
|
|
SnapshotSubcommands::Create { force } => {
|
|
SnapshotModel::create(&db, force).await?;
|
|
}
|
|
|
|
SnapshotSubcommands::List {} => {
|
|
for snapshot in SnapshotModel::get_all(&db).await? {
|
|
info!("Snapshot {snapshot:?}")
|
|
}
|
|
}
|
|
|
|
SnapshotSubcommands::Show { date } => {
|
|
let date = date.unwrap_or_else(today);
|
|
let snapshot = if let Some(snapshot) =
|
|
SnapshotModel::get_by_date(&db, date).await?
|
|
{
|
|
info!("Snapshot {snapshot:?}");
|
|
snapshot
|
|
} else {
|
|
info!("No snapshot exists for {date}");
|
|
return Ok(());
|
|
};
|
|
|
|
for group in GroupDataModel::get_all_by_snapshot(&db, &snapshot).await?
|
|
{
|
|
info!(
|
|
id = group.id,
|
|
name = group.name,
|
|
subscribers = group.subscribers,
|
|
);
|
|
}
|
|
}
|
|
},
|
|
|
|
MainSubcommands::Web {
|
|
command: web_command,
|
|
} => match web_command {
|
|
WebSubcommands::Build { output } => {
|
|
let (groups, user_count_group) =
|
|
if let Some(snapshot) = SnapshotModel::get_most_recent(&db).await? {
|
|
(
|
|
GroupDataModel::get_all_by_snapshot(&db, &snapshot).await?,
|
|
GroupDataModel::get_highest_subscribers(&db, &snapshot).await?,
|
|
)
|
|
} else {
|
|
(vec![], None)
|
|
};
|
|
|
|
create_dir_all(&output).await?;
|
|
HomeTemplate::new(
|
|
groups,
|
|
user_count_group.as_ref().map(|group| group.subscribers),
|
|
)
|
|
.render_to_file(&output)
|
|
.await?;
|
|
generate_css(&output).await?;
|
|
|
|
if let Some(group) = user_count_group {
|
|
let groups =
|
|
GroupDataModel::get_n_most_recent(&db, 30, &group.name).await?;
|
|
UserCountChart { groups }
|
|
.render(&output, &group.name)
|
|
.await?;
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
Ok(())
|
|
}
|