diff --git a/Cargo.toml b/Cargo.toml index 260ec49..ea3ce22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" authors = ["Tochukwu Nkemdilim "] [dependencies] +tempfile = "3.0.2" diff --git a/src/connectivity/handlers/xml_profile_handler.rs b/src/connectivity/handlers/xml_profile_handler.rs index 9234ae7..0ec9656 100644 --- a/src/connectivity/handlers/xml_profile_handler.rs +++ b/src/connectivity/handlers/xml_profile_handler.rs @@ -1,6 +1,8 @@ -use std::fs; -use std::io; +extern crate tempfile; + +use self::tempfile::NamedTempFile; use connectivity::stubs::windows_wifi_profile; +use std::{io, io::Write}; pub(crate) struct NetworkXmlProfileHandler { pub content: String, @@ -18,7 +20,10 @@ impl NetworkXmlProfileHandler { } /// Recreate the file and dump the processed contents to it - pub fn to_file(&mut self, file_path: &str) -> Result<(), io::Error> { - Ok(fs::write(&file_path, self.content.as_bytes())?) + pub fn write_to_temp_file(&mut self) -> Result { + let mut temp_file = NamedTempFile::new()?; + write!(temp_file, "{}", self.content)?; + + Ok(temp_file) } } diff --git a/src/connectivity/providers/windows.rs b/src/connectivity/providers/windows.rs index b86dcaf..b91b512 100644 --- a/src/connectivity/providers/windows.rs +++ b/src/connectivity/providers/windows.rs @@ -1,12 +1,10 @@ use connectivity::handlers::NetworkXmlProfileHandler; use connectivity::{Network, NetworkError}; - use std::process::Command; #[derive(Debug)] pub(crate) struct Windows { name: String, - pub output_xml_path: String, } impl Windows { @@ -14,7 +12,6 @@ impl Windows { pub fn new(name: &str, interface: Option<&str>) -> Self { Windows { name: String::from(name), - output_xml_path: OUTPUT_XML_FILE_PATH.into(), } } @@ -25,10 +22,7 @@ impl Windows { .replace("{SSID}", &self.name) .replace("{password}", password); - // Write details to new xml file - let _ = handler - .to_file(&self.output_xml_path) - .map_err(|err| NetworkError::IoError(err))?; + let temp_file = handler.write_to_temp_file()?; // Add the network profile Command::new("netsh") @@ -36,7 +30,7 @@ impl Windows { "wlan", "add", "profile", - &format!("filename={}", self.output_xml_path), + &format!("filename={}", temp_file.path().to_str().unwrap()), ]) .output() .map_err(|_| NetworkError::AddNetworkProfileFailed)?;