From ebe22cfc103a148b7784bb41274b532072fd3c30 Mon Sep 17 00:00:00 2001 From: Marc Plano-Lesay Date: Tue, 5 Nov 2024 14:27:37 +1100 Subject: [PATCH 1/2] Add a command to delete assets --- Cargo.lock | 39 +++++++++++++++++++++++ Cargo.toml | 1 + src/args.rs | 11 +++++++ src/commands/delete_assets.rs | 58 +++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 1 + src/main.rs | 4 +++ 6 files changed, 114 insertions(+) create mode 100644 src/commands/delete_assets.rs diff --git a/Cargo.lock b/Cargo.lock index d4d61b0..0ee5f30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -304,6 +304,19 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "unicode-width", + "windows-sys 0.52.0", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -329,6 +342,19 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "dialoguer" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "658bce805d770f407bc62102fca7c2c64ceef2fbcb2b8bd19d2765ce093980de" +dependencies = [ + "console", + "shell-words", + "tempfile", + "thiserror", + "zeroize", +] + [[package]] name = "directories" version = "5.0.1" @@ -356,6 +382,12 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "encoding_rs" version = "0.8.34" @@ -758,6 +790,7 @@ dependencies = [ "chrono", "clap", "color-eyre", + "dialoguer", "directories", "figment", "figment_file_provider_adapter", @@ -1645,6 +1678,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shell-words" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" + [[package]] name = "shlex" version = "1.3.0" diff --git a/Cargo.toml b/Cargo.toml index 3cbc80f..6e773dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ ignored = ["progenitor-client", "regress"] chrono = { version = "0.4.38", features = ["serde"] } clap = { version = "4.5.20", features = ["derive"] } color-eyre = "0.6.3" +dialoguer = "0.11.0" directories = "5.0.1" figment = { version = "0.10.19", features = ["env", "toml"] } figment_file_provider_adapter = "0.1.1" diff --git a/src/args.rs b/src/args.rs index f8be709..db19b06 100644 --- a/src/args.rs +++ b/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 { diff --git a/src/commands/delete_assets.rs b/src/commands/delete_assets.rs new file mode 100644 index 0000000..47c9b0e --- /dev/null +++ b/src/commands/delete_assets.rs @@ -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, 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(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index d8684ee..4b01565 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,3 +1,4 @@ +pub mod delete_assets; pub mod list_assets; pub mod missing_date_of_birth; pub mod server_features; diff --git a/src/main.rs b/src/main.rs index c65231b..96e71f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { From cbac0654861d3e87bc5b7218153b91e84bcf62c7 Mon Sep 17 00:00:00 2001 From: Marc Plano-Lesay Date: Tue, 5 Nov 2024 14:28:43 +1100 Subject: [PATCH 2/2] Add new features to the README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0d5d73c..17336e0 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ - List named faces that do not have an associated date of birth - Synchronise date of births from a vcard file + - List and delete assets, including filtering offline assets only + - Show details about the server Some other features are planned, mainly around using external libraries. Feature ideas and contributions welcome.