Merge branch 'context' into 'main'

Use a context struct

See merge request kernald/immich-tools!18
This commit is contained in:
Marc Plano-Lesay 2024-11-05 22:25:00 +00:00
commit 6ee741fd5f
10 changed files with 134 additions and 45 deletions

83
Cargo.lock generated
View file

@ -177,6 +177,29 @@ version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "bon"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97493a391b4b18ee918675fb8663e53646fd09321c58b46afa04e8ce2499c869"
dependencies = [
"bon-macros",
"rustversion",
]
[[package]]
name = "bon-macros"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a2af3eac944c12cdf4423eab70d310da0a8e5851a18ffb192c0a5e3f7ae1663"
dependencies = [
"darling",
"ident_case",
"proc-macro2",
"quote",
"syn 2.0.82",
]
[[package]]
name = "bumpalo"
version = "3.16.0"
@ -333,6 +356,41 @@ version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "darling"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn 2.0.82",
]
[[package]]
name = "darling_macro"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
dependencies = [
"darling_core",
"quote",
"syn 2.0.82",
]
[[package]]
name = "deranged"
version = "0.3.11"
@ -773,6 +831,12 @@ dependencies = [
"cc",
]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "idna"
version = "0.5.0"
@ -787,6 +851,7 @@ dependencies = [
name = "immich-tools"
version = "0.2.0"
dependencies = [
"bon",
"chrono",
"clap",
"color-eyre",
@ -799,6 +864,7 @@ dependencies = [
"prettyplease",
"progenitor",
"progenitor-client",
"readonly",
"regress",
"reqwest",
"serde",
@ -1324,6 +1390,17 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "readonly"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a25d631e41bfb5fdcde1d4e2215f62f7f0afa3ff11e26563765bd6ea1d229aeb"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.82",
]
[[package]]
name = "redox_syscall"
version = "0.5.7"
@ -1501,6 +1578,12 @@ dependencies = [
"untrusted",
]
[[package]]
name = "rustversion"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248"
[[package]]
name = "ryu"
version = "1.0.18"

View file

@ -12,6 +12,7 @@ keywords = ["immich"]
ignored = ["progenitor-client", "regress"]
[dependencies]
bon = "2.3.0"
chrono = { version = "0.4.38", features = ["serde"] }
clap = { version = "4.5.20", features = ["derive"] }
color-eyre = "0.6.3"
@ -22,6 +23,7 @@ figment_file_provider_adapter = "0.1.1"
log = "0.4.22"
pretty_env_logger = "0.5.0"
progenitor-client = "0.8.0"
readonly = "0.2.12"
regress = "0.10.1"
reqwest = { version = "0.12.8", features = ["json", "stream"] }
serde = { version = "1.0.213", features = ["derive"] }

View file

@ -1,24 +1,19 @@
use crate::{
context::Context,
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<()> {
pub async fn delete_assets(ctx: Context, offline: bool) -> Result<()> {
let query = AssetQuery {
is_offline: if offline { Some(true) } else { None },
..Default::default()
};
let assets = fetch_all_assets(query, client).await?;
let assets = fetch_all_assets(query, &ctx.client).await?;
for asset in &assets {
info!(
@ -27,8 +22,8 @@ pub async fn delete_assets(
);
}
if !dry_run {
if !(no_confirm
if !ctx.dry_run {
if !(ctx.no_confirm
|| Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Do you want to continue?")
.interact()?)
@ -40,7 +35,7 @@ pub async fn delete_assets(
.iter()
.map(|asset| Uuid::parse_str(&asset.id))
.collect();
client
ctx.client
.delete_assets(&crate::types::AssetBulkDeleteDto {
force: Some(true),
ids: asset_ids?,
@ -51,7 +46,7 @@ pub async fn delete_assets(
info!(
"Deleted {} assets{}",
assets.len(),
if dry_run { " (dry run)" } else { "" }
if ctx.dry_run { " (dry run)" } else { "" }
);
Ok(())

View file

@ -1,6 +1,6 @@
use crate::{
context::Context,
utils::assets::{fetch_all_assets, AssetQuery},
Client,
};
use color_eyre::eyre::Result;
use tabled::{
@ -20,13 +20,13 @@ struct Asset {
model: String,
}
pub async fn list_assets(offline: bool, client: &Client) -> Result<()> {
pub async fn list_assets(ctx: Context, offline: bool) -> Result<()> {
let query = AssetQuery {
is_offline: if offline { Some(true) } else { None },
with_exif: true,
};
let mut assets: Vec<_> = fetch_all_assets(query, client)
let mut assets: Vec<_> = fetch_all_assets(query, &ctx.client)
.await?
.into_iter()
.map(|asset| Asset {

View file

@ -1,9 +1,9 @@
use crate::{utils::people::fetch_all_contacts, Client};
use crate::{context::Context, utils::people::fetch_all_contacts};
use color_eyre::eyre::Result;
use log::*;
pub async fn missing_date_of_birth(client: &Client) -> Result<()> {
let contacts = fetch_all_contacts(client).await?;
pub async fn missing_date_of_birth(ctx: Context) -> Result<()> {
let contacts = fetch_all_contacts(&ctx.client).await?;
let mut filtered_contacts = contacts
.iter()
.filter(|c| c.birth_date.is_none() && !c.name.is_empty())

View file

@ -8,7 +8,7 @@ use tabled::{
Table, Tabled,
};
use crate::Client;
use crate::context::Context;
#[derive(Tabled)]
struct ServerFeature {
@ -18,8 +18,8 @@ struct ServerFeature {
enabled: bool,
}
pub async fn server_features(client: &Client) -> Result<()> {
let response = client.get_server_features().await?;
pub async fn server_features(ctx: Context) -> Result<()> {
let response = ctx.client.get_server_features().await?;
let mut features = vec![
ServerFeature {
name: "Configuration file",

View file

@ -1,9 +1,9 @@
use color_eyre::eyre::Result;
use crate::Client;
use crate::context::Context;
pub async fn server_version(client: &Client) -> Result<()> {
let version = client.get_server_version().await?;
pub async fn server_version(ctx: Context) -> Result<()> {
let version = ctx.client.get_server_version().await?;
println!("{}.{}.{}", version.major, version.minor, version.patch);
Ok(())
}

View file

@ -10,17 +10,14 @@ use vcard4::{
};
use crate::{
context::Context,
types::{PersonResponseDto, PersonUpdateDto},
utils::people::fetch_all_contacts,
Client,
};
pub async fn sync_date_of_birth(
client: &Client,
vcard_file: &PathBuf,
dry_run: bool,
) -> Result<()> {
let server_contacts = fetch_all_contacts(client).await?;
pub async fn sync_date_of_birth(ctx: Context, vcard_file: &PathBuf) -> Result<()> {
let server_contacts = fetch_all_contacts(&ctx.client).await?;
let filtered_contacts: Vec<_> = server_contacts
.iter()
@ -55,7 +52,7 @@ pub async fn sync_date_of_birth(
formatted_name
);
} else if bday.year() > 0 {
update_person_bday(c, bday, client, dry_run).await?;
update_person_bday(c, bday, &ctx.client, ctx.dry_run).await?;
updated_dobs += 1;
} else {
debug!(

11
src/context.rs Normal file
View file

@ -0,0 +1,11 @@
use bon::Builder;
use crate::Client;
#[readonly::make]
#[derive(Builder)]
pub struct Context {
pub client: Client,
pub dry_run: bool,
pub no_confirm: bool,
}

View file

@ -12,6 +12,7 @@ use commands::server_features::server_features;
use commands::server_version::server_version;
use commands::sync_date_of_birth::sync_date_of_birth;
use config::Config;
use context::Context;
use directories::ProjectDirs;
use figment::providers::{Env, Format, Serialized, Toml};
use figment::Figment;
@ -22,6 +23,7 @@ use reqwest::header;
mod args;
mod commands;
mod config;
mod context;
mod utils;
#[tokio::main]
@ -51,29 +53,28 @@ async fn main() -> Result<()> {
let client = get_client(&conf.server_url, &conf.api_key)?;
validate_client_connection(&client).await?;
let ctx = Context::builder()
.client(client)
.dry_run(args.dry_run)
.no_confirm(args.no_confirm)
.build();
validate_client_connection(&ctx.client).await?;
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,
AssetsCommands::Delete { offline } => delete_assets(ctx, *offline).await,
AssetsCommands::List { offline } => list_assets(ctx, *offline).await,
},
Commands::People { people_command } => match people_command {
PeopleCommands::MissingDateOfBirths {} => missing_date_of_birth(&client).await,
PeopleCommands::MissingDateOfBirths {} => missing_date_of_birth(ctx).await,
PeopleCommands::SyncDateOfBirths { vcard: _ } => {
sync_date_of_birth(
&client,
&conf.people.sync_date_of_births.vcard,
args.dry_run,
)
.await
sync_date_of_birth(ctx, &conf.people.sync_date_of_births.vcard).await
}
},
Commands::Server { server_command } => match server_command {
ServerCommands::Features {} => server_features(&client).await,
ServerCommands::Version {} => server_version(&client).await,
ServerCommands::Features {} => server_features(ctx).await,
ServerCommands::Version {} => server_version(ctx).await,
},
}
}