Merge branch 'context' into 'main'
Use a context struct See merge request kernald/immich-tools!18
This commit is contained in:
commit
6ee741fd5f
10 changed files with 134 additions and 45 deletions
83
Cargo.lock
generated
83
Cargo.lock
generated
|
@ -177,6 +177,29 @@ version = "2.6.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
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]]
|
[[package]]
|
||||||
name = "bumpalo"
|
name = "bumpalo"
|
||||||
version = "3.16.0"
|
version = "3.16.0"
|
||||||
|
@ -333,6 +356,41 @@ version = "0.8.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
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]]
|
[[package]]
|
||||||
name = "deranged"
|
name = "deranged"
|
||||||
version = "0.3.11"
|
version = "0.3.11"
|
||||||
|
@ -773,6 +831,12 @@ dependencies = [
|
||||||
"cc",
|
"cc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ident_case"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "idna"
|
name = "idna"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
|
@ -787,6 +851,7 @@ dependencies = [
|
||||||
name = "immich-tools"
|
name = "immich-tools"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"bon",
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
|
@ -799,6 +864,7 @@ dependencies = [
|
||||||
"prettyplease",
|
"prettyplease",
|
||||||
"progenitor",
|
"progenitor",
|
||||||
"progenitor-client",
|
"progenitor-client",
|
||||||
|
"readonly",
|
||||||
"regress",
|
"regress",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -1324,6 +1390,17 @@ dependencies = [
|
||||||
"proc-macro2",
|
"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]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.7"
|
version = "0.5.7"
|
||||||
|
@ -1501,6 +1578,12 @@ dependencies = [
|
||||||
"untrusted",
|
"untrusted",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustversion"
|
||||||
|
version = "1.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ryu"
|
name = "ryu"
|
||||||
version = "1.0.18"
|
version = "1.0.18"
|
||||||
|
|
|
@ -12,6 +12,7 @@ keywords = ["immich"]
|
||||||
ignored = ["progenitor-client", "regress"]
|
ignored = ["progenitor-client", "regress"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bon = "2.3.0"
|
||||||
chrono = { version = "0.4.38", features = ["serde"] }
|
chrono = { version = "0.4.38", features = ["serde"] }
|
||||||
clap = { version = "4.5.20", features = ["derive"] }
|
clap = { version = "4.5.20", features = ["derive"] }
|
||||||
color-eyre = "0.6.3"
|
color-eyre = "0.6.3"
|
||||||
|
@ -22,6 +23,7 @@ figment_file_provider_adapter = "0.1.1"
|
||||||
log = "0.4.22"
|
log = "0.4.22"
|
||||||
pretty_env_logger = "0.5.0"
|
pretty_env_logger = "0.5.0"
|
||||||
progenitor-client = "0.8.0"
|
progenitor-client = "0.8.0"
|
||||||
|
readonly = "0.2.12"
|
||||||
regress = "0.10.1"
|
regress = "0.10.1"
|
||||||
reqwest = { version = "0.12.8", features = ["json", "stream"] }
|
reqwest = { version = "0.12.8", features = ["json", "stream"] }
|
||||||
serde = { version = "1.0.213", features = ["derive"] }
|
serde = { version = "1.0.213", features = ["derive"] }
|
||||||
|
|
|
@ -1,24 +1,19 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
|
context::Context,
|
||||||
utils::assets::{fetch_all_assets, AssetQuery},
|
utils::assets::{fetch_all_assets, AssetQuery},
|
||||||
Client,
|
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
use dialoguer::{theme::ColorfulTheme, Confirm};
|
use dialoguer::{theme::ColorfulTheme, Confirm};
|
||||||
use log::info;
|
use log::info;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub async fn delete_assets(
|
pub async fn delete_assets(ctx: Context, offline: bool) -> Result<()> {
|
||||||
client: &Client,
|
|
||||||
offline: bool,
|
|
||||||
dry_run: bool,
|
|
||||||
no_confirm: bool,
|
|
||||||
) -> Result<()> {
|
|
||||||
let query = AssetQuery {
|
let query = AssetQuery {
|
||||||
is_offline: if offline { Some(true) } else { None },
|
is_offline: if offline { Some(true) } else { None },
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let assets = fetch_all_assets(query, client).await?;
|
let assets = fetch_all_assets(query, &ctx.client).await?;
|
||||||
|
|
||||||
for asset in &assets {
|
for asset in &assets {
|
||||||
info!(
|
info!(
|
||||||
|
@ -27,8 +22,8 @@ pub async fn delete_assets(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if !dry_run {
|
if !ctx.dry_run {
|
||||||
if !(no_confirm
|
if !(ctx.no_confirm
|
||||||
|| Confirm::with_theme(&ColorfulTheme::default())
|
|| Confirm::with_theme(&ColorfulTheme::default())
|
||||||
.with_prompt("Do you want to continue?")
|
.with_prompt("Do you want to continue?")
|
||||||
.interact()?)
|
.interact()?)
|
||||||
|
@ -40,7 +35,7 @@ pub async fn delete_assets(
|
||||||
.iter()
|
.iter()
|
||||||
.map(|asset| Uuid::parse_str(&asset.id))
|
.map(|asset| Uuid::parse_str(&asset.id))
|
||||||
.collect();
|
.collect();
|
||||||
client
|
ctx.client
|
||||||
.delete_assets(&crate::types::AssetBulkDeleteDto {
|
.delete_assets(&crate::types::AssetBulkDeleteDto {
|
||||||
force: Some(true),
|
force: Some(true),
|
||||||
ids: asset_ids?,
|
ids: asset_ids?,
|
||||||
|
@ -51,7 +46,7 @@ pub async fn delete_assets(
|
||||||
info!(
|
info!(
|
||||||
"Deleted {} assets{}",
|
"Deleted {} assets{}",
|
||||||
assets.len(),
|
assets.len(),
|
||||||
if dry_run { " (dry run)" } else { "" }
|
if ctx.dry_run { " (dry run)" } else { "" }
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
|
context::Context,
|
||||||
utils::assets::{fetch_all_assets, AssetQuery},
|
utils::assets::{fetch_all_assets, AssetQuery},
|
||||||
Client,
|
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
use tabled::{
|
use tabled::{
|
||||||
|
@ -20,13 +20,13 @@ struct Asset {
|
||||||
model: String,
|
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 {
|
let query = AssetQuery {
|
||||||
is_offline: if offline { Some(true) } else { None },
|
is_offline: if offline { Some(true) } else { None },
|
||||||
with_exif: true,
|
with_exif: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut assets: Vec<_> = fetch_all_assets(query, client)
|
let mut assets: Vec<_> = fetch_all_assets(query, &ctx.client)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|asset| Asset {
|
.map(|asset| Asset {
|
||||||
|
|
|
@ -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 color_eyre::eyre::Result;
|
||||||
use log::*;
|
use log::*;
|
||||||
|
|
||||||
pub async fn missing_date_of_birth(client: &Client) -> Result<()> {
|
pub async fn missing_date_of_birth(ctx: Context) -> Result<()> {
|
||||||
let contacts = fetch_all_contacts(client).await?;
|
let contacts = fetch_all_contacts(&ctx.client).await?;
|
||||||
let mut filtered_contacts = contacts
|
let mut filtered_contacts = contacts
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|c| c.birth_date.is_none() && !c.name.is_empty())
|
.filter(|c| c.birth_date.is_none() && !c.name.is_empty())
|
||||||
|
|
|
@ -8,7 +8,7 @@ use tabled::{
|
||||||
Table, Tabled,
|
Table, Tabled,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::Client;
|
use crate::context::Context;
|
||||||
|
|
||||||
#[derive(Tabled)]
|
#[derive(Tabled)]
|
||||||
struct ServerFeature {
|
struct ServerFeature {
|
||||||
|
@ -18,8 +18,8 @@ struct ServerFeature {
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn server_features(client: &Client) -> Result<()> {
|
pub async fn server_features(ctx: Context) -> Result<()> {
|
||||||
let response = client.get_server_features().await?;
|
let response = ctx.client.get_server_features().await?;
|
||||||
let mut features = vec![
|
let mut features = vec![
|
||||||
ServerFeature {
|
ServerFeature {
|
||||||
name: "Configuration file",
|
name: "Configuration file",
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
|
|
||||||
use crate::Client;
|
use crate::context::Context;
|
||||||
|
|
||||||
pub async fn server_version(client: &Client) -> Result<()> {
|
pub async fn server_version(ctx: Context) -> Result<()> {
|
||||||
let version = client.get_server_version().await?;
|
let version = ctx.client.get_server_version().await?;
|
||||||
println!("{}.{}.{}", version.major, version.minor, version.patch);
|
println!("{}.{}.{}", version.major, version.minor, version.patch);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,17 +10,14 @@ use vcard4::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
context::Context,
|
||||||
types::{PersonResponseDto, PersonUpdateDto},
|
types::{PersonResponseDto, PersonUpdateDto},
|
||||||
utils::people::fetch_all_contacts,
|
utils::people::fetch_all_contacts,
|
||||||
Client,
|
Client,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub async fn sync_date_of_birth(
|
pub async fn sync_date_of_birth(ctx: Context, vcard_file: &PathBuf) -> Result<()> {
|
||||||
client: &Client,
|
let server_contacts = fetch_all_contacts(&ctx.client).await?;
|
||||||
vcard_file: &PathBuf,
|
|
||||||
dry_run: bool,
|
|
||||||
) -> Result<()> {
|
|
||||||
let server_contacts = fetch_all_contacts(client).await?;
|
|
||||||
|
|
||||||
let filtered_contacts: Vec<_> = server_contacts
|
let filtered_contacts: Vec<_> = server_contacts
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -55,7 +52,7 @@ pub async fn sync_date_of_birth(
|
||||||
formatted_name
|
formatted_name
|
||||||
);
|
);
|
||||||
} else if bday.year() > 0 {
|
} 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;
|
updated_dobs += 1;
|
||||||
} else {
|
} else {
|
||||||
debug!(
|
debug!(
|
||||||
|
|
11
src/context.rs
Normal file
11
src/context.rs
Normal 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,
|
||||||
|
}
|
29
src/main.rs
29
src/main.rs
|
@ -12,6 +12,7 @@ use commands::server_features::server_features;
|
||||||
use commands::server_version::server_version;
|
use commands::server_version::server_version;
|
||||||
use commands::sync_date_of_birth::sync_date_of_birth;
|
use commands::sync_date_of_birth::sync_date_of_birth;
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
use context::Context;
|
||||||
use directories::ProjectDirs;
|
use directories::ProjectDirs;
|
||||||
use figment::providers::{Env, Format, Serialized, Toml};
|
use figment::providers::{Env, Format, Serialized, Toml};
|
||||||
use figment::Figment;
|
use figment::Figment;
|
||||||
|
@ -22,6 +23,7 @@ use reqwest::header;
|
||||||
mod args;
|
mod args;
|
||||||
mod commands;
|
mod commands;
|
||||||
mod config;
|
mod config;
|
||||||
|
mod context;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -51,29 +53,28 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
let client = get_client(&conf.server_url, &conf.api_key)?;
|
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 {
|
match &args.command {
|
||||||
Commands::Assets { assets_command } => match assets_command {
|
Commands::Assets { assets_command } => match assets_command {
|
||||||
AssetsCommands::Delete { offline } => {
|
AssetsCommands::Delete { offline } => delete_assets(ctx, *offline).await,
|
||||||
delete_assets(&client, *offline, args.dry_run, args.no_confirm).await
|
AssetsCommands::List { offline } => list_assets(ctx, *offline).await,
|
||||||
}
|
|
||||||
AssetsCommands::List { offline } => list_assets(*offline, &client).await,
|
|
||||||
},
|
},
|
||||||
Commands::People { people_command } => match people_command {
|
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: _ } => {
|
PeopleCommands::SyncDateOfBirths { vcard: _ } => {
|
||||||
sync_date_of_birth(
|
sync_date_of_birth(ctx, &conf.people.sync_date_of_births.vcard).await
|
||||||
&client,
|
|
||||||
&conf.people.sync_date_of_births.vcard,
|
|
||||||
args.dry_run,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Commands::Server { server_command } => match server_command {
|
Commands::Server { server_command } => match server_command {
|
||||||
ServerCommands::Features {} => server_features(&client).await,
|
ServerCommands::Features {} => server_features(ctx).await,
|
||||||
ServerCommands::Version {} => server_version(&client).await,
|
ServerCommands::Version {} => server_version(ctx).await,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue