Check installation path, add error messages to frontend

This commit is contained in:
James 2018-01-30 17:19:54 +11:00
parent c8af1009a2
commit 34e7698d76
3 changed files with 62 additions and 4 deletions

View File

@ -4,6 +4,9 @@
use regex::Regex;
use std::fs::create_dir_all;
use std::fs::read_dir;
use std::env::home_dir;
use std::env::var;
use std::env::consts::OS;
@ -62,10 +65,34 @@ impl InstallerFramework {
pub fn install(
&self,
items: Vec<String>,
path: &str,
messages: &Sender<InstallMessage>,
) -> Result<(), String> {
// TODO: Error handling
println!("Framework: Installing {:?}", items);
println!("Framework: Installing {:?} to {}", items, path);
// Create our install directory
let path = PathBuf::from(path);
if !path.exists() {
match create_dir_all(&path) {
Ok(_) => {},
Err(v) => return Err(format!("Failed to create install directory: {:?}", v)),
}
}
if !path.is_dir() {
return Err(format!("Install destination is not a directory."));
}
// Make sure it is empty
let paths = match read_dir(path) {
Ok(v) => v,
Err(v) => return Err(format!("Failed to read install destination: {:?}", v)),
};
if paths.count() != 0 {
return Err(format!("Install destination is not empty."));
}
// Resolve items in config
let mut to_install = Vec::new();

View File

@ -158,20 +158,29 @@ impl Service for WebService {
.collect::<HashMap<String, String>>();
let mut to_install = Vec::new();
let mut path : Option<String> = None;
// Transform results into just an array of stuff to install
for (key, value) in results.iter() {
if key == "path" {
path = Some(value.to_owned());
continue;
}
if value == "true" {
to_install.push(key.to_owned());
}
}
// The frontend always provides this
let path = path.unwrap();
let (sender, receiver) = channel();
let (tx, rx) = hyper::Body::pair();
// Startup a thread to do this operation for us
thread::spawn(move || {
match cloned_element.install(to_install, &sender) {
match cloned_element.install(to_install, &path, &sender) {
Err(v) => sender.send(InstallMessage::Error(v)).unwrap(),
_ => {}
}

View File

@ -33,7 +33,15 @@
</h2>
</div>
<div class="column" v-if="select_packages">
<div class="column" v-if="has_error">
<h4 class="subtitle">An error occurred:</h4>
<pre><code>{{ error }}</code></pre>
<a class="button is-primary is-pulled-right" v-on:click="back_to_packages">Back</a>
</div>
<div class="column" v-else-if="select_packages">
<h4 class="subtitle">Select your preferred settings:</h4>
<!-- Build options -->
@ -108,7 +116,9 @@
is_installing : false,
is_finished : false,
progress : 0,
progress_message : ""
progress_message : "",
has_error : false,
error : ""
},
methods: {
"select_file": function() {
@ -134,17 +144,29 @@
}
console.log(results);
results["path"] = this.install_location;
stream_ajax("/api/start-install", function(line) {
console.log(line);
if (line.hasOwnProperty("Status")) {
app.progress_message = line.Status[0];
app.progress = line.Status[1] * 100;
}
if (line.hasOwnProperty("Error")) {
app.is_installing = false;
app.has_error = true;
app.error = line.Error;
}
}, function(e) {
app.is_installing = false;
app.is_finished = true;
}, undefined, results);
},
"back_to_packages": function() {
app.select_packages = true;
app.has_error = false;
},
"exit": function() {
ajax("/api/exit", function() {});
}