Add support for a configuration file

This commit is contained in:
Marc Plano-Lesay 2024-10-30 17:46:14 +11:00
parent a34dbb7fd3
commit b9664b43c8
10 changed files with 505 additions and 63 deletions

View file

@ -1,41 +1,66 @@
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
use crate::args::Commands;
use anyhow::Result;
use args::{PeopleCommands, ServerCommands};
use clap::Parser;
use color_eyre::eyre::Result;
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;
use config::Config;
use directories::ProjectDirs;
use figment::providers::{Env, Format, Serialized, Toml};
use figment::Figment;
use figment_file_provider_adapter::FileAdapter;
use log::*;
use reqwest::header;
mod args;
mod commands;
mod config;
mod utils;
#[tokio::main]
async fn main() {
async fn main() -> Result<()> {
color_eyre::install()?;
pretty_env_logger::init();
let mut conf_extractor = Figment::new();
if let Some(project_dirs) = ProjectDirs::from("fr", "enoent", "Immich Tools") {
let config_file_path = project_dirs.config_dir().join("config.toml");
if config_file_path.exists() {
debug!("Reading configuration from {:?}", config_file_path);
conf_extractor =
conf_extractor.merge(FileAdapter::wrap(Toml::file_exact(config_file_path)));
}
} else {
warn!("Unable to determine configuration file path");
}
let args = args::Opts::parse();
let client = get_client(&args.server_url, &args.api_key).unwrap();
let conf: Config = conf_extractor
.merge(FileAdapter::wrap(Env::prefixed("IMMICH_TOOLS_")))
.merge(Serialized::defaults(&args))
.extract()?;
let res = match &args.command {
let client = get_client(&conf.server_url, &conf.api_key)?;
match &args.command {
Commands::People { people_command } => match people_command {
PeopleCommands::MissingDateOfBirths {} => missing_date_of_birth(&client).await,
PeopleCommands::SyncDateOfBirths { vcard_file } => {
sync_date_of_birth(&client, vcard_file, args.dry_run).await
PeopleCommands::SyncDateOfBirths { vcard: _ } => {
sync_date_of_birth(
&client,
&conf.people.sync_date_of_births.vcard,
args.dry_run,
)
.await
}
},
Commands::Server { server_command } => match server_command {
ServerCommands::Version {} => server_version(&client).await,
},
};
match res {
Ok(_) => {}
Err(e) => println!("Error: {:?}", e),
}
}