Merge branch 'better-error' into 'main'

Validate client configuration before running commands

See merge request kernald/immich-tools!8
This commit is contained in:
Marc Plano-Lesay 2024-10-30 07:04:39 +00:00
commit bcae3b3cb9

View file

@ -3,7 +3,8 @@ include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
use crate::args::Commands;
use args::{PeopleCommands, ServerCommands};
use clap::Parser;
use color_eyre::eyre::Result;
use color_eyre::eyre::{Result, WrapErr};
use color_eyre::Section;
use commands::missing_date_of_birth::missing_date_of_birth;
use commands::server_version::server_version;
use commands::sync_date_of_birth::sync_date_of_birth;
@ -46,6 +47,8 @@ async fn main() -> Result<()> {
let client = get_client(&conf.server_url, &conf.api_key)?;
validate_client_connection(&client).await?;
match &args.command {
Commands::People { people_command } => match people_command {
PeopleCommands::MissingDateOfBirths {} => missing_date_of_birth(&client).await,
@ -79,3 +82,21 @@ fn get_client(url: &str, api_key: &str) -> Result<Client> {
.unwrap(),
))
}
async fn validate_client_connection(client: &Client) -> Result<()> {
client
.validate_access_token()
.await
.wrap_err_with(|| "Unable to connect to Immich")
.with_suggestion(|| "The API key might be invalid")
.with_suggestion(|| "There server URL might be invalid, it should likely end in /api")
.with_note(|| format!("The provided server URL was {}", client.baseurl()))
.with_note(|| {
format!(
"The API version for this client is {}, make sure the server version is compatible",
client.api_version()
)
})?;
Ok(())
}