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]>"]
[dependencies]
tempfile = "3.0.2"
@@ -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<NamedTempFile, io::Error> {
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::{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)?;