Use a context struct
This commit is contained in:
parent
60208331ba
commit
a0d9bfa97b
10 changed files with 134 additions and 45 deletions
|
@ -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(())
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -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
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::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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue