Improve error handling of default directory

This commit is contained in:
Age Manning 2019-07-02 17:32:14 +10:00
parent dd410535cb
commit 1aeec12b78
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
4 changed files with 48 additions and 16 deletions

View File

@ -61,13 +61,24 @@ fn main() {
)
.get_matches();
let mut default_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
default_dir.push(DEFAULT_DATA_DIR);
let data_dir = &matches
let data_dir = match matches
.value_of("datadir")
.and_then(|v| Some(PathBuf::from(v)))
.unwrap_or_else(|| PathBuf::from(default_dir));
{
Some(v) => v,
None => {
// use the default
let mut default_dir = match dirs::home_dir() {
Some(v) => v,
None => {
crit!(log, "Failed to find a home directory");
return;
}
};
default_dir.push(DEFAULT_DATA_DIR);
PathBuf::from(default_dir)
}
};
// create the directory if needed
match fs::create_dir_all(&data_dir) {

View File

@ -11,7 +11,6 @@ use tokio::io::{AsyncRead, AsyncWrite};
const MAX_READ_SIZE: usize = 4_194_304; // 4M
/// Implementation of the `ConnectionUpgrade` for the rpc protocol.
#[derive(Debug, Clone)]
pub struct RPCProtocol;

View File

@ -161,13 +161,24 @@ fn main() {
let log = slog::Logger::root(drain.fuse(), o!());
let mut default_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
default_dir.push(DEFAULT_DATA_DIR);
let data_dir = &matches
let data_dir = match matches
.value_of("datadir")
.and_then(|v| Some(PathBuf::from(v)))
.unwrap_or_else(|| PathBuf::from(default_dir));
{
Some(v) => v,
None => {
// use the default
let mut default_dir = match dirs::home_dir() {
Some(v) => v,
None => {
crit!(log, "Failed to find a home directory");
return;
}
};
default_dir.push(DEFAULT_DATA_DIR);
PathBuf::from(default_dir)
}
};
// create the directory if needed
match fs::create_dir_all(&data_dir) {

View File

@ -67,13 +67,24 @@ fn main() {
)
.get_matches();
let mut default_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
default_dir.push(DEFAULT_DATA_DIR);
let data_dir = &matches
let data_dir = match matches
.value_of("datadir")
.and_then(|v| Some(PathBuf::from(v)))
.unwrap_or_else(|| PathBuf::from(default_dir));
{
Some(v) => v,
None => {
// use the default
let mut default_dir = match dirs::home_dir() {
Some(v) => v,
None => {
crit!(log, "Failed to find a home directory");
return;
}
};
default_dir.push(DEFAULT_DATA_DIR);
PathBuf::from(default_dir)
}
};
// create the directory if needed
match fs::create_dir_all(&data_dir) {