Fix(Windows/Profile): Write Windows Profile To Temporary File

This commit is contained in:
Tochukwu Nkemdilim
2018-07-03 22:48:52 +01:00
parent c6e4d7359f
commit e2c9712168
3 changed files with 12 additions and 12 deletions
+1
View File
@@ -4,3 +4,4 @@ version = "0.1.0"
authors = ["Tochukwu Nkemdilim <[email protected]>"] authors = ["Tochukwu Nkemdilim <[email protected]>"]
[dependencies] [dependencies]
tempfile = "3.0.2"
@@ -1,6 +1,8 @@
use std::fs; extern crate tempfile;
use std::io;
use self::tempfile::NamedTempFile;
use connectivity::stubs::windows_wifi_profile; use connectivity::stubs::windows_wifi_profile;
use std::{io, io::Write};
pub(crate) struct NetworkXmlProfileHandler { pub(crate) struct NetworkXmlProfileHandler {
pub content: String, pub content: String,
@@ -18,7 +20,10 @@ impl NetworkXmlProfileHandler {
} }
/// Recreate the file and dump the processed contents to it /// Recreate the file and dump the processed contents to it
pub fn to_file(&mut self, file_path: &str) -> Result<(), io::Error> { pub fn write_to_temp_file(&mut self) -> Result<NamedTempFile, io::Error> {
Ok(fs::write(&file_path, self.content.as_bytes())?) let mut temp_file = NamedTempFile::new()?;
write!(temp_file, "{}", self.content)?;
Ok(temp_file)
} }
} }
+2 -8
View File
@@ -1,12 +1,10 @@
use connectivity::handlers::NetworkXmlProfileHandler; use connectivity::handlers::NetworkXmlProfileHandler;
use connectivity::{Network, NetworkError}; use connectivity::{Network, NetworkError};
use std::process::Command; use std::process::Command;
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Windows { pub(crate) struct Windows {
name: String, name: String,
pub output_xml_path: String,
} }
impl Windows { impl Windows {
@@ -14,7 +12,6 @@ impl Windows {
pub fn new(name: &str, interface: Option<&str>) -> Self { pub fn new(name: &str, interface: Option<&str>) -> Self {
Windows { Windows {
name: String::from(name), name: String::from(name),
output_xml_path: OUTPUT_XML_FILE_PATH.into(),
} }
} }
@@ -25,10 +22,7 @@ impl Windows {
.replace("{SSID}", &self.name) .replace("{SSID}", &self.name)
.replace("{password}", password); .replace("{password}", password);
// Write details to new xml file let temp_file = handler.write_to_temp_file()?;
let _ = handler
.to_file(&self.output_xml_path)
.map_err(|err| NetworkError::IoError(err))?;
// Add the network profile // Add the network profile
Command::new("netsh") Command::new("netsh")
@@ -36,7 +30,7 @@ impl Windows {
"wlan", "wlan",
"add", "add",
"profile", "profile",
&format!("filename={}", self.output_xml_path), &format!("filename={}", temp_file.path().to_str().unwrap()),
]) ])
.output() .output()
.map_err(|_| NetworkError::AddNetworkProfileFailed)?; .map_err(|_| NetworkError::AddNetworkProfileFailed)?;