diff --git a/rust/src/chrome.rs b/rust/src/chrome.rs index 8ef5e52409b97..e4183d8fc8cf3 100644 --- a/rust/src/chrome.rs +++ b/rust/src/chrome.rs @@ -312,19 +312,20 @@ impl ChromeManager { } } - fn get_browser_path_in_cache(&self) -> PathBuf { - self.get_cache_path() + fn get_browser_path_in_cache(&self) -> Result> { + Ok(self + .get_cache_path()? .join(self.get_browser_name()) .join(self.get_platform_label()) - .join(self.get_browser_version()) + .join(self.get_browser_version())) } - fn get_browser_binary_path_in_cache(&self) -> PathBuf { - let browser_in_cache = self.get_browser_path_in_cache(); + fn get_browser_binary_path_in_cache(&self) -> Result> { + let browser_in_cache = self.get_browser_path_in_cache()?; if MACOS.is(self.get_os()) { - browser_in_cache.join(CFT_MACOS_APP_NAME) + Ok(browser_in_cache.join(CFT_MACOS_APP_NAME)) } else { - browser_in_cache.join(self.get_browser_name_with_extension()) + Ok(browser_in_cache.join(self.get_browser_name_with_extension())) } } } @@ -400,7 +401,7 @@ impl SeleniumManager for ChromeManager { fn request_driver_version(&mut self) -> Result> { let major_browser_version_binding = self.get_major_browser_version(); let major_browser_version = major_browser_version_binding.as_str(); - let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()); + let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?); match get_driver_version_from_metadata( &metadata.drivers, @@ -445,7 +446,7 @@ impl SeleniumManager for ChromeManager { &driver_version, driver_ttl, )); - write_metadata(&metadata, self.get_logger(), self.get_cache_path()); + write_metadata(&metadata, self.get_logger(), self.get_cache_path()?); } Ok(driver_version) } @@ -456,7 +457,7 @@ impl SeleniumManager for ChromeManager { let browser_name = self.browser_name; let browser_version; let major_browser_version = self.get_major_browser_version(); - let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()); + let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?); // First, browser version is checked in the local metadata match get_browser_version_from_metadata( @@ -484,7 +485,7 @@ impl SeleniumManager for ChromeManager { &browser_version, browser_ttl, )); - write_metadata(&metadata, self.get_logger(), self.get_cache_path()); + write_metadata(&metadata, self.get_logger(), self.get_cache_path()?); } } } @@ -534,17 +535,17 @@ impl SeleniumManager for ChromeManager { )) } - fn get_driver_path_in_cache(&self) -> PathBuf { + fn get_driver_path_in_cache(&self) -> Result> { let driver_version = self.get_driver_version(); let os = self.get_os(); let arch_folder = self.get_platform_label(); - compose_driver_path_in_cache( - self.get_cache_path(), + Ok(compose_driver_path_in_cache( + self.get_cache_path()?, self.driver_name, os, arch_folder, driver_version, - ) + )) } fn get_config(&self) -> &ManagerConfig { @@ -570,7 +571,7 @@ impl SeleniumManager for ChromeManager { fn download_browser(&mut self) -> Result, Box> { let browser_version; let browser_name = self.browser_name; - let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()); + let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?); let major_browser_version = self.get_major_browser_version(); let major_browser_version_int = major_browser_version.parse::().unwrap_or_default(); @@ -622,7 +623,7 @@ impl SeleniumManager for ChromeManager { &browser_version, browser_ttl, )); - write_metadata(&metadata, self.get_logger(), self.get_cache_path()); + write_metadata(&metadata, self.get_logger(), self.get_cache_path()?); } } } @@ -632,7 +633,7 @@ impl SeleniumManager for ChromeManager { )); // Checking if browser version is in the cache - let browser_binary_path = self.get_browser_binary_path_in_cache(); + let browser_binary_path = self.get_browser_binary_path_in_cache()?; if browser_binary_path.exists() { self.get_logger().debug(format!( "{} {} already in the cache", @@ -661,7 +662,7 @@ impl SeleniumManager for ChromeManager { uncompress( &driver_zip_file, - &self.get_browser_path_in_cache(), + &self.get_browser_path_in_cache()?, self.get_logger(), None, )?; diff --git a/rust/src/edge.rs b/rust/src/edge.rs index 31677edbbee3d..651c14278bf0f 100644 --- a/rust/src/edge.rs +++ b/rust/src/edge.rs @@ -134,7 +134,7 @@ impl SeleniumManager for EdgeManager { fn request_driver_version(&mut self) -> Result> { let mut major_browser_version = self.get_major_browser_version(); - let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()); + let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?); match get_driver_version_from_metadata( &metadata.drivers, @@ -191,7 +191,7 @@ impl SeleniumManager for EdgeManager { &driver_version, driver_ttl, )); - write_metadata(&metadata, self.get_logger(), self.get_cache_path()); + write_metadata(&metadata, self.get_logger(), self.get_cache_path()?); } Ok(driver_version) @@ -230,7 +230,7 @@ impl SeleniumManager for EdgeManager { )) } - fn get_driver_path_in_cache(&self) -> PathBuf { + fn get_driver_path_in_cache(&self) -> Result> { let driver_version = self.get_driver_version(); let os = self.get_os(); let arch = self.get_arch(); @@ -251,13 +251,13 @@ impl SeleniumManager for EdgeManager { } else { "linux64" }; - compose_driver_path_in_cache( - self.get_cache_path(), + Ok(compose_driver_path_in_cache( + self.get_cache_path()?, self.driver_name, os, arch_folder, driver_version, - ) + )) } fn get_config(&self) -> &ManagerConfig { diff --git a/rust/src/files.rs b/rust/src/files.rs index c6ac40feff04c..51b46172c63eb 100644 --- a/rust/src/files.rs +++ b/rust/src/files.rs @@ -54,16 +54,18 @@ impl BrowserPath { } } -pub fn create_parent_path_if_not_exists(path: &Path) { +pub fn create_parent_path_if_not_exists(path: &Path) -> Result<(), Box> { if let Some(p) = path.parent() { - create_path_if_not_exists(p); + create_path_if_not_exists(p)?; } + Ok(()) } -pub fn create_path_if_not_exists(path: &Path) { +pub fn create_path_if_not_exists(path: &Path) -> Result<(), Box> { if !path.exists() { - fs::create_dir_all(path).unwrap(); + fs::create_dir_all(path)?; } + Ok(()) } pub fn uncompress( @@ -132,7 +134,7 @@ pub fn unzip( None => continue, }; if single_file.is_none() { - create_path_if_not_exists(target); + create_path_if_not_exists(target)?; out_path = target.join(path); } @@ -148,7 +150,7 @@ pub fn unzip( out_path.display(), file.size() )); - create_parent_path_if_not_exists(out_path.as_path()); + create_parent_path_if_not_exists(out_path.as_path())?; let mut outfile = File::create(&out_path)?; io::copy(&mut file, &mut outfile)?; diff --git a/rust/src/firefox.rs b/rust/src/firefox.rs index 8726e0a6d0a61..1659628f4efbb 100644 --- a/rust/src/firefox.rs +++ b/rust/src/firefox.rs @@ -132,7 +132,7 @@ impl SeleniumManager for FirefoxManager { fn request_driver_version(&mut self) -> Result> { let major_browser_version_binding = self.get_major_browser_version(); let major_browser_version = major_browser_version_binding.as_str(); - let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()); + let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?); match get_driver_version_from_metadata( &metadata.drivers, @@ -161,7 +161,7 @@ impl SeleniumManager for FirefoxManager { &driver_version, driver_ttl, )); - write_metadata(&metadata, self.get_logger(), self.get_cache_path()); + write_metadata(&metadata, self.get_logger(), self.get_cache_path()?); } Ok(driver_version) @@ -211,7 +211,7 @@ impl SeleniumManager for FirefoxManager { )) } - fn get_driver_path_in_cache(&self) -> PathBuf { + fn get_driver_path_in_cache(&self) -> Result> { let driver_version = self.get_driver_version(); let os = self.get_os(); let arch = self.get_arch(); @@ -241,13 +241,13 @@ impl SeleniumManager for FirefoxManager { } else { "linux64" }; - compose_driver_path_in_cache( - self.get_cache_path(), + Ok(compose_driver_path_in_cache( + self.get_cache_path()?, self.driver_name, os, arch_folder, driver_version, - ) + )) } fn get_config(&self) -> &ManagerConfig { diff --git a/rust/src/grid.rs b/rust/src/grid.rs index 360ed103ac5c7..6f477f9cc817d 100644 --- a/rust/src/grid.rs +++ b/rust/src/grid.rs @@ -97,7 +97,7 @@ impl SeleniumManager for GridManager { fn request_driver_version(&mut self) -> Result> { let major_browser_version_binding = self.get_major_browser_version(); let major_browser_version = major_browser_version_binding.as_str(); - let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()); + let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?); match get_driver_version_from_metadata( &metadata.drivers, @@ -156,7 +156,7 @@ impl SeleniumManager for GridManager { &driver_version, driver_ttl, )); - write_metadata(&metadata, self.get_logger(), self.get_cache_path()); + write_metadata(&metadata, self.get_logger(), self.get_cache_path()?); } Ok(driver_version) @@ -187,14 +187,15 @@ impl SeleniumManager for GridManager { )) } - fn get_driver_path_in_cache(&self) -> PathBuf { + fn get_driver_path_in_cache(&self) -> Result> { let browser_name = self.get_browser_name(); let driver_name = self.get_driver_name(); let driver_version = self.get_driver_version(); - self.get_cache_path() + Ok(self + .get_cache_path()? .join(browser_name) .join(driver_version) - .join(format!("{driver_name}-{driver_version}.{GRID_EXTENSION}")) + .join(format!("{driver_name}-{driver_version}.{GRID_EXTENSION}"))) } fn get_config(&self) -> &ManagerConfig { diff --git a/rust/src/iexplorer.rs b/rust/src/iexplorer.rs index 34941c3ad1ff3..5a73176fa81c9 100644 --- a/rust/src/iexplorer.rs +++ b/rust/src/iexplorer.rs @@ -109,7 +109,7 @@ impl SeleniumManager for IExplorerManager { fn request_driver_version(&mut self) -> Result> { let major_browser_version_binding = self.get_major_browser_version(); let major_browser_version = major_browser_version_binding.as_str(); - let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()); + let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?); match get_driver_version_from_metadata( &metadata.drivers, @@ -164,7 +164,7 @@ impl SeleniumManager for IExplorerManager { &driver_version, driver_ttl, )); - write_metadata(&metadata, self.get_logger(), self.get_cache_path()); + write_metadata(&metadata, self.get_logger(), self.get_cache_path()?); } Ok(driver_version) @@ -194,20 +194,20 @@ impl SeleniumManager for IExplorerManager { )) } - fn get_driver_path_in_cache(&self) -> PathBuf { + fn get_driver_path_in_cache(&self) -> Result> { let driver_version = self.get_driver_version(); let _minor_driver_version = self .get_minor_version(driver_version) .unwrap_or_default() .parse::() .unwrap_or_default(); - compose_driver_path_in_cache( - self.get_cache_path(), + Ok(compose_driver_path_in_cache( + self.get_cache_path()?, self.driver_name, "Windows", "win32", driver_version, - ) + )) } fn get_config(&self) -> &ManagerConfig { diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 045fa6586bcf6..becc2657ec9ef 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -118,7 +118,7 @@ pub trait SeleniumManager { fn get_driver_url(&mut self) -> Result>; - fn get_driver_path_in_cache(&self) -> PathBuf; + fn get_driver_path_in_cache(&self) -> Result>; fn get_config(&self) -> &ManagerConfig; @@ -144,18 +144,18 @@ pub trait SeleniumManager { download_to_tmp_folder(self.get_http_client(), driver_url, self.get_logger())?; if self.is_grid() { - let driver_path_in_cache = Self::get_driver_path_in_cache(self); - create_parent_path_if_not_exists(&driver_path_in_cache); + let driver_path_in_cache = self.get_driver_path_in_cache()?; + create_parent_path_if_not_exists(&driver_path_in_cache)?; Ok(fs::rename(driver_zip_file, driver_path_in_cache)?) } else { - let driver_path_in_cache = Self::get_driver_path_in_cache(self); + let driver_path_in_cache = self.get_driver_path_in_cache()?; let driver_name_with_extension = self.get_driver_name_with_extension(); - uncompress( + Ok(uncompress( &driver_zip_file, &driver_path_in_cache, self.get_logger(), Some(driver_name_with_extension), - ) + )?) } } @@ -525,7 +525,7 @@ pub trait SeleniumManager { } // If driver was not in the PATH, try to find it in the cache - let driver_path = self.get_driver_path_in_cache(); + let driver_path = self.get_driver_path_in_cache()?; if driver_path.exists() { if !self.is_safari() { self.get_logger().debug(format!( @@ -854,11 +854,11 @@ pub trait SeleniumManager { } } - fn get_cache_path(&self) -> PathBuf { + fn get_cache_path(&self) -> Result> { let path = Path::new(&self.get_config().cache_path); - create_path_if_not_exists(path); + create_path_if_not_exists(path)?; let canon_path = self.canonicalize_path(path.to_path_buf()); - Path::new(&canon_path).to_path_buf() + Ok(Path::new(&canon_path).to_path_buf()) } fn set_cache_path(&mut self, cache_path: String) { @@ -917,7 +917,8 @@ pub fn get_manager_by_driver( } } -pub fn clear_cache(log: &Logger, cache_path: PathBuf) { +pub fn clear_cache(log: &Logger, path: &str) { + let cache_path = Path::new(path).to_path_buf(); if cache_path.exists() { log.debug(format!("Clearing cache at: {}", cache_path.display())); fs::remove_dir_all(&cache_path).unwrap_or_else(|err| { diff --git a/rust/src/main.rs b/rust/src/main.rs index eee06039209fb..45309fe0cb53d 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -23,6 +23,7 @@ use exitcode::DATAERR; use exitcode::OK; use exitcode::UNAVAILABLE; use selenium_manager::config::{BooleanKey, StringKey, CACHE_PATH_KEY}; +use selenium_manager::files::{default_cache_folder, path_buf_to_string}; use selenium_manager::grid::GridManager; use selenium_manager::logger::{Logger, BROWSER_PATH, DRIVER_PATH}; use selenium_manager::REQUEST_TIMEOUT_SEC; @@ -128,8 +129,13 @@ struct Cli { fn main() { let cli = Cli::parse(); - let cache_path = - StringKey(vec![CACHE_PATH_KEY], &cli.cache_path.unwrap_or_default()).get_value(); + + let default_cache_path = path_buf_to_string(default_cache_folder()); + let cache_path = StringKey( + vec![CACHE_PATH_KEY], + &cli.cache_path.unwrap_or(default_cache_path), + ) + .get_value(); let debug = cli.debug || BooleanKey("debug", false).get_value(); let trace = cli.trace || BooleanKey("trace", false).get_value(); let log = Logger::create(&cli.output, debug, trace); @@ -170,16 +176,10 @@ fn main() { selenium_manager.set_cache_path(cache_path.clone()); if cli.clear_cache || BooleanKey("clear-cache", false).get_value() { - clear_cache( - selenium_manager.get_logger(), - selenium_manager.get_cache_path(), - ); + clear_cache(selenium_manager.get_logger(), &cache_path); } if cli.clear_metadata || BooleanKey("clear-metadata", false).get_value() { - clear_metadata( - selenium_manager.get_logger(), - selenium_manager.get_cache_path(), - ); + clear_metadata(selenium_manager.get_logger(), &cache_path); } selenium_manager diff --git a/rust/src/metadata.rs b/rust/src/metadata.rs index 5dbb1aa25d5d2..4541b648bb7c6 100644 --- a/rust/src/metadata.rs +++ b/rust/src/metadata.rs @@ -18,7 +18,7 @@ use std::fs; use std::fs::File; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::time::{SystemTime, UNIX_EPOCH}; use serde::{Deserialize, Serialize}; @@ -163,7 +163,8 @@ pub fn write_metadata(metadata: &Metadata, log: &Logger, cache_path: PathBuf) { .unwrap(); } -pub fn clear_metadata(log: &Logger, cache_path: PathBuf) { +pub fn clear_metadata(log: &Logger, path: &str) { + let cache_path = Path::new(path).to_path_buf(); let metadata_path = get_metadata_path(cache_path); log.debug(format!( "Deleting metadata file {}", diff --git a/rust/src/safari.rs b/rust/src/safari.rs index f5170e235ee5a..5b44f6d0afe1d 100644 --- a/rust/src/safari.rs +++ b/rust/src/safari.rs @@ -94,8 +94,8 @@ impl SeleniumManager for SafariManager { Err(format!("{} not available for download", self.get_driver_name()).into()) } - fn get_driver_path_in_cache(&self) -> PathBuf { - PathBuf::from("/usr/bin/safaridriver") + fn get_driver_path_in_cache(&self) -> Result> { + Ok(PathBuf::from("/usr/bin/safaridriver")) } fn get_config(&self) -> &ManagerConfig { diff --git a/rust/src/safaritp.rs b/rust/src/safaritp.rs index 2163f7e3049ad..6c3606bc1e755 100644 --- a/rust/src/safaritp.rs +++ b/rust/src/safaritp.rs @@ -100,8 +100,10 @@ impl SeleniumManager for SafariTPManager { Err(format!("{} not available for download", self.get_driver_name()).into()) } - fn get_driver_path_in_cache(&self) -> PathBuf { - PathBuf::from("/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver") + fn get_driver_path_in_cache(&self) -> Result> { + Ok(PathBuf::from( + "/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver", + )) } fn get_config(&self) -> &ManagerConfig {