From 55fc99bcce11cf6c0e0ec204b19eb1f3faff9d49 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 4 Nov 2021 19:18:17 +0100 Subject: [PATCH] Add commands to delete and check repositories Signed-off-by: trivernis --- .../src/tauri_plugin/commands/repo.rs | 50 +++++++++++++++---- mediarepo-api/src/tauri_plugin/error.rs | 6 +++ mediarepo-api/src/tauri_plugin/mod.rs | 4 +- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 0471d10..015a351 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -65,6 +65,32 @@ pub async fn add_repository( Ok(repositories) } +#[tauri::command] +pub async fn remove_repository(app_state: AppAccess<'_>, name: String) -> PluginResult<()> { + let mut settings = app_state.settings.write().await; + + if let Some(_repo) = settings.repositories.remove(&name) { + save_settings(&settings)?; + Ok(()) + } else { + Err(PluginError::from(format!( + "The repository '{}' does not exist.", + name + ))) + } +} + +#[tauri::command] +pub async fn check_local_repository_exists(path: String) -> PluginResult { + let config_path = PathBuf::from(path).join(REPO_CONFIG_FILE); + + if !config_path.exists() { + Ok(false) + } else { + Ok(true) + } +} + #[tauri::command] pub async fn disconnect_repository( app_state: AppAccess<'_>, @@ -104,15 +130,7 @@ pub async fn select_repository( .ok_or(PluginError::from( format!("Repository '{}' not found", name).as_str(), ))?; - if let Some(path) = app_state - .active_repo - .read() - .await - .clone() - .and_then(|r| r.path) - { - app_state.stop_running_daemon(&path).await?; - } + close_selected_repository(&app_state).await?; let address = if let Some(address) = &repo.address { address.clone() } else { @@ -142,6 +160,20 @@ pub async fn select_repository( Ok(()) } +async fn close_selected_repository(app_state: &AppAccess<'_>) -> PluginResult<()> { + if let Some(path) = app_state + .active_repo + .read() + .await + .clone() + .and_then(|r| r.path) + { + app_state.stop_running_daemon(&path).await?; + } + + Ok(()) +} + async fn read_repo_config(path: PathBuf) -> PluginResult { let toml_str = fs::read_to_string(path).await?; let config = toml::from_str(&toml_str)?; diff --git a/mediarepo-api/src/tauri_plugin/error.rs b/mediarepo-api/src/tauri_plugin/error.rs index 1480239..705dca3 100644 --- a/mediarepo-api/src/tauri_plugin/error.rs +++ b/mediarepo-api/src/tauri_plugin/error.rs @@ -27,6 +27,12 @@ impl From<&str> for PluginError { } } +impl From for PluginError { + fn from(message: String) -> Self { + Self { message } + } +} + impl From for PluginError { fn from(e: ApiError) -> Self { let message = match e { diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 846e695..f3848e8 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -45,7 +45,9 @@ impl MediarepoPlugin { check_daemon_running, stop_daemon, disconnect_repository, - close_local_repository + close_local_repository, + check_local_repository_exists, + remove_repository ]), } }