Create directory to store resources if it does not exists

This commit is contained in:
Daniele Tricoli 2019-05-18 01:09:36 +02:00
parent 0d27b67ec2
commit 9f7eb62994
2 changed files with 25 additions and 1 deletions

View file

@ -1,13 +1,23 @@
use std::fs;
use std::io;
use std::path::PathBuf;
use home::home_dir;
const QUOLL_HOME: &str = ".quoll";
/// Return the path where quoll looks at resources.
/// Return the path where quoll looks at to find resources.
pub fn get_home() -> Option<PathBuf> {
let mut home = home_dir()?;
home.push(QUOLL_HOME);
Some(home)
}
/// Create directory used to store resources.
pub fn create_home() -> io::Result<()> {
if let Some(home) = get_home() {
fs::create_dir(home)?;
}
Ok(())
}

View file

@ -78,6 +78,20 @@ fn main() {
("serve", _) => {
let command = Arc::new(RwLock::new(Command::from("black")));
// Create directory to store resources if it does not exists.
if let Some(home) = config::get_home() {
if !home.is_dir() {
info!(
"Directory for resources not found, creating at: {}",
home.display()
);
if let Err(e) = config::create_home() {
error!("{}", e);
process::exit(1);
}
}
}
info!("Starting UDP server on port {}...", port);
let mut server = Server::new(address, Arc::clone(&command));
thread::spawn(move || {