Add a command to delete assets
This commit is contained in:
parent
af5990796e
commit
ebe22cfc10
6 changed files with 114 additions and 0 deletions
11
src/args.rs
11
src/args.rs
|
@ -20,6 +20,10 @@ pub(crate) struct Opts {
|
|||
#[arg(short, long)]
|
||||
pub dry_run: bool,
|
||||
|
||||
/// If enabled, don't ask for confirmation of destructive actions
|
||||
#[arg(short, long)]
|
||||
pub no_confirm: bool,
|
||||
|
||||
#[command(subcommand)]
|
||||
#[serde(flatten)]
|
||||
pub command: Commands,
|
||||
|
@ -53,6 +57,13 @@ pub(crate) enum Commands {
|
|||
|
||||
#[derive(Serialize, Subcommand)]
|
||||
pub(crate) enum AssetsCommands {
|
||||
/// Delete all assets
|
||||
#[serde(rename = "delete")]
|
||||
Delete {
|
||||
/// Delete only offline assets
|
||||
#[arg(short, long, action)]
|
||||
offline: bool,
|
||||
},
|
||||
/// List all assets
|
||||
#[serde(rename = "list")]
|
||||
List {
|
||||
|
|
58
src/commands/delete_assets.rs
Normal file
58
src/commands/delete_assets.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
use crate::{
|
||||
utils::assets::{fetch_all_assets, AssetQuery},
|
||||
Client,
|
||||
};
|
||||
use color_eyre::eyre::Result;
|
||||
use dialoguer::{theme::ColorfulTheme, Confirm};
|
||||
use log::info;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn delete_assets(
|
||||
client: &Client,
|
||||
offline: bool,
|
||||
dry_run: bool,
|
||||
no_confirm: bool,
|
||||
) -> Result<()> {
|
||||
let query = AssetQuery {
|
||||
is_offline: if offline { Some(true) } else { None },
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let assets = fetch_all_assets(query, client).await?;
|
||||
|
||||
for asset in &assets {
|
||||
info!(
|
||||
"Deleting asset {} located at {}",
|
||||
asset.id, asset.original_path
|
||||
);
|
||||
}
|
||||
|
||||
if !dry_run {
|
||||
if !(no_confirm
|
||||
|| Confirm::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt("Do you want to continue?")
|
||||
.interact()?)
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let asset_ids: Result<Vec<_>, uuid::Error> = assets
|
||||
.iter()
|
||||
.map(|asset| Uuid::parse_str(&asset.id))
|
||||
.collect();
|
||||
client
|
||||
.delete_assets(&crate::types::AssetBulkDeleteDto {
|
||||
force: Some(true),
|
||||
ids: asset_ids?,
|
||||
})
|
||||
.await?;
|
||||
}
|
||||
|
||||
info!(
|
||||
"Deleted {} assets{}",
|
||||
assets.len(),
|
||||
if dry_run { " (dry run)" } else { "" }
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
pub mod delete_assets;
|
||||
pub mod list_assets;
|
||||
pub mod missing_date_of_birth;
|
||||
pub mod server_features;
|
||||
|
|
|
@ -5,6 +5,7 @@ use args::{AssetsCommands, PeopleCommands, ServerCommands};
|
|||
use clap::Parser;
|
||||
use color_eyre::eyre::{Result, WrapErr};
|
||||
use color_eyre::Section;
|
||||
use commands::delete_assets::delete_assets;
|
||||
use commands::list_assets::list_assets;
|
||||
use commands::missing_date_of_birth::missing_date_of_birth;
|
||||
use commands::server_features::server_features;
|
||||
|
@ -54,6 +55,9 @@ async fn main() -> Result<()> {
|
|||
|
||||
match &args.command {
|
||||
Commands::Assets { assets_command } => match assets_command {
|
||||
AssetsCommands::Delete { offline } => {
|
||||
delete_assets(&client, *offline, args.dry_run, args.no_confirm).await
|
||||
}
|
||||
AssetsCommands::List { offline } => list_assets(*offline, &client).await,
|
||||
},
|
||||
Commands::People { people_command } => match people_command {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue