From 4d1b20c61c369e60654e04073ffb4f4a292991d8 Mon Sep 17 00:00:00 2001 From: Rusko124 Date: Tue, 2 Jul 2024 13:19:15 +0400 Subject: [PATCH] Edits after review --- src/commands/edge_app.rs | 50 ++++++++++++++++++++++++---------------- src/commands/mod.rs | 2 ++ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/commands/edge_app.rs b/src/commands/edge_app.rs index 70a8607..8fc7405 100644 --- a/src/commands/edge_app.rs +++ b/src/commands/edge_app.rs @@ -391,26 +391,10 @@ impl EdgeAppCommand { address_shared.lock().unwrap().as_ref().unwrap() ); - #[cfg(target_os = "macos")] - { - let _ = std::process::Command::new("open") - .arg(format!("{}/index.html", address_shared.lock().unwrap().as_ref().unwrap())) - .output(); - } - - #[cfg(target_os = "linux")] - { - let _ = std::process::Command::new("xdg-open") - .arg(format!("{}/index.html", address_shared.lock().unwrap().as_ref().unwrap())) - .output(); - } - - #[cfg(target_os = "windows")] - { - let _ = std::process::Command::new("cmd") - .arg("/C") - .arg(format!("start {}/index.html", address_shared.lock().unwrap().as_ref().unwrap())) - .output(); + if let Err(e) = self.open_browser( + &format!("{}/index.html", address_shared.lock().unwrap().as_ref().unwrap()), + ) { + eprintln!("{}", e); } loop { @@ -421,6 +405,32 @@ impl EdgeAppCommand { Ok(()) } + fn open_browser(&self, address: &str) -> Result<(), CommandError> { + let command = match std::env::consts::OS { + "macos" => "open", + "windows" => "start", + "linux" => "xdg-open", + _ => { + return Err(CommandError::OpenBrowserError( + "Unsupported OS to open browser".to_string() + )) + } + }; + + let output = std::process::Command::new(command) + .arg(address) + .output() + .expect("Failed to open browser"); + + if !output.status.success() { + return Err(CommandError::OpenBrowserError( + format!("Failed to open browser: {}", str::from_utf8(&output.stderr).unwrap()) + )); + } + + Ok(()) + } + fn maybe_delete_missing_settings( &self, delete_missing_settings: Option, diff --git a/src/commands/mod.rs b/src/commands/mod.rs index e144c54..918f38d 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -126,6 +126,8 @@ pub enum CommandError { SettingDoesNotExist(String), #[error("Wrong setting name: {0}.")] WrongSettingName(String), + #[error("Failed to open browser")] + OpenBrowserError(String), } pub fn get(