From ce516ca498115b51e55231a154b799ab585cc740 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 12 Dec 2021 10:41:03 +0100 Subject: [PATCH] Add daemon discovery check Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 3 ++- mediarepo-api/src/daemon_management/mod.rs | 7 +++++++ mediarepo-api/src/tauri_plugin/commands/daemon.rs | 11 +++++++++-- mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- mediarepo-api/src/tauri_plugin/settings.rs | 5 +++-- mediarepo-api/src/tauri_plugin/state.rs | 10 +++++++--- 6 files changed, 30 insertions(+), 9 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 604b24b..44221e3 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -17,6 +17,7 @@ mime_guess = {version = "2.0.3", optional=true} serde_piecewise_default = "0.2.0" futures = {version = "0.3.17", optional=true} url = {version = "2.2.2", optional=true } +pathsearch = {version="0.2.0", optional=true} [dependencies.bromine] version = "0.16.1" @@ -48,4 +49,4 @@ optional = true [features] tauri-plugin = ["client-api","tauri", "parking_lot", "serde_json", "tokio", "toml", "directories", "mime_guess", "futures", "url"] -client-api = ["bromine", "async-trait", "tokio"] \ No newline at end of file +client-api = ["bromine", "async-trait", "tokio", "pathsearch"] \ No newline at end of file diff --git a/mediarepo-api/src/daemon_management/mod.rs b/mediarepo-api/src/daemon_management/mod.rs index 0414a80..41b2d53 100644 --- a/mediarepo-api/src/daemon_management/mod.rs +++ b/mediarepo-api/src/daemon_management/mod.rs @@ -1,2 +1,9 @@ +use pathsearch::find_executable_in_path; +use std::path::PathBuf; + pub mod cli; pub mod error; + +pub fn find_daemon_executable() -> Option { + find_executable_in_path("mediarepo-daemon") +} diff --git a/mediarepo-api/src/tauri_plugin/commands/daemon.rs b/mediarepo-api/src/tauri_plugin/commands/daemon.rs index 3a539bf..af4b9a3 100644 --- a/mediarepo-api/src/tauri_plugin/commands/daemon.rs +++ b/mediarepo-api/src/tauri_plugin/commands/daemon.rs @@ -6,9 +6,16 @@ use std::io::ErrorKind; use std::net::{SocketAddr, ToSocketAddrs}; use tokio::net::TcpListener; +#[tauri::command] +pub async fn has_executable(app_state: AppAccess<'_>) -> PluginResult { + let settings = app_state.settings.read().await; + + Ok(settings.daemon_path.is_some()) +} + #[tauri::command] pub async fn init_repository(app_state: AppAccess<'_>, repo_path: String) -> PluginResult<()> { - let daemon = app_state.get_daemon_cli(repo_path).await; + let daemon = app_state.get_daemon_cli(repo_path).await?; daemon.init_repo().await?; Ok(()) @@ -16,7 +23,7 @@ pub async fn init_repository(app_state: AppAccess<'_>, repo_path: String) -> Plu #[tauri::command] pub async fn start_daemon(app_state: AppAccess<'_>, repo_path: String) -> PluginResult<()> { - let mut daemon = app_state.get_daemon_cli(repo_path).await; + let mut daemon = app_state.get_daemon_cli(repo_path).await?; daemon.start_daemon()?; app_state.add_started_daemon(daemon).await; diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 73110f7..b19f587 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -58,7 +58,8 @@ impl MediarepoPlugin { save_file_locally, delete_thumbnails, read_file, - delete_repository + delete_repository, + has_executable ]), } } diff --git a/mediarepo-api/src/tauri_plugin/settings.rs b/mediarepo-api/src/tauri_plugin/settings.rs index 7c1f53b..b020c95 100644 --- a/mediarepo-api/src/tauri_plugin/settings.rs +++ b/mediarepo-api/src/tauri_plugin/settings.rs @@ -1,3 +1,4 @@ +use crate::daemon_management::find_daemon_executable; use crate::tauri_plugin::error::PluginResult; use directories::ProjectDirs; use serde::{Deserialize, Serialize}; @@ -19,14 +20,14 @@ pub struct Repository { #[derive(DeserializePiecewiseDefault, Debug, Serialize)] pub struct Settings { - pub daemon_path: String, + pub daemon_path: Option, pub repositories: HashMap, } impl Default for Settings { fn default() -> Self { Self { - daemon_path: String::from("mediarepo-daemon"), + daemon_path: find_daemon_executable().map(|e| e.to_string_lossy().to_string()), repositories: HashMap::new(), } } diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index e59bb01..f40076f 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -179,11 +179,15 @@ impl AppState { } /// Returns the daemon cli client - pub async fn get_daemon_cli(&self, repo_path: String) -> DaemonCli { + pub async fn get_daemon_cli(&self, repo_path: String) -> PluginResult { let settings = self.settings.read().await; - let path = settings.daemon_path.clone(); + let path = settings + .daemon_path + .clone() + .ok_or_else(|| PluginError::from("Missing daemon executable"))?; + let cli = DaemonCli::new(path, repo_path); - DaemonCli::new(path, repo_path) + Ok(cli) } /// Adds a started daemon to the running daemons