use super::common::*;
use super::database::*;
use super::model::*;
use super::progress::*;
use super::prompt::*;
use super::util::*;
use anyhow::Result;
use clap::{Arg, ArgAction, ArgMatches, Command};
use indicatif::ProgressBar;
use sqlx::sqlite::SqliteConnection;
use std::path::Path;
use std::time::Duration;
pub fn subcommand() -> Command {
Command::new("purge-systems").about("Purge systems").arg(
Arg::new("EMPTY")
.short('e')
.long("empty")
.help("Only list empty systems for selection")
.required(false)
.action(ArgAction::SetTrue),
)
}
pub async fn main(
connection: &mut SqliteConnection,
matches: &ArgMatches,
progress_bar: &ProgressBar,
) -> Result<()> {
let systems =
prompt_for_systems(connection, None, false, matches.get_flag("EMPTY"), false).await?;
progress_bar.enable_steady_tick(Duration::from_millis(100));
for system in systems {
print_header(progress_bar, &format!("Purging \"{}\"", system.name));
purge_system(connection, progress_bar, &system).await?;
print_separator(progress_bar);
}
Ok(())
}
pub async fn purge_system(
connection: &mut SqliteConnection,
progress_bar: &ProgressBar,
system: &System,
) -> Result<()> {
print_header(progress_bar, &format!("Processing \"{}\"", system.name));
let romfiles = find_romfiles_by_system_id(connection, system.id).await;
let trash_directory = get_trash_directory(connection, Some(system)).await?;
for romfile in romfiles {
let new_path = trash_directory.join(Path::new(&romfile.path).file_name().unwrap());
romfile
.as_common(connection)
.await?
.rename(progress_bar, &new_path, false)
.await?
.update(connection, progress_bar, romfile.id)
.await?;
}
print_action(progress_bar, "Deleting system");
progress_bar.enable_steady_tick(Duration::from_millis(100));
delete_system_by_id(connection, system.id).await;
progress_bar.disable_steady_tick();
Ok(())
}
#[cfg(test)]
mod test_purge_systems;