creating all in one place

This commit is contained in:
Nikolay Volf 2016-05-13 18:32:32 +03:00
parent 9150538ac4
commit 7c19930efa

View File

@ -37,6 +37,11 @@ pub struct Configuration {
pub args: Args pub args: Args
} }
pub struct Directories {
pub keys: String,
pub db: String,
}
impl Configuration { impl Configuration {
pub fn parse() -> Self { pub fn parse() -> Self {
Configuration { Configuration {
@ -60,10 +65,6 @@ impl Configuration {
self.args.flag_maxpeers.unwrap_or(self.args.flag_peers) as u32 self.args.flag_maxpeers.unwrap_or(self.args.flag_peers) as u32
} }
pub fn path(&self) -> String {
Configuration::replace_home(&self.args.flag_datadir.as_ref().unwrap_or(&self.args.flag_db_path))
}
pub fn author(&self) -> Address { pub fn author(&self) -> Address {
let d = self.args.flag_etherbase.as_ref().unwrap_or(&self.args.flag_author); let d = self.args.flag_etherbase.as_ref().unwrap_or(&self.args.flag_author);
Address::from_str(clean_0x(d)).unwrap_or_else(|_| { Address::from_str(clean_0x(d)).unwrap_or_else(|_| {
@ -112,10 +113,6 @@ impl Configuration {
} }
} }
pub fn keys_path(&self) -> String {
Configuration::replace_home(&self.args.flag_keys_path)
}
pub fn spec(&self) -> Spec { pub fn spec(&self) -> Spec {
match self.chain().as_str() { match self.chain().as_str() {
"frontier" | "homestead" | "mainnet" => ethereum::new_frontier(), "frontier" | "homestead" | "mainnet" => ethereum::new_frontier(),
@ -270,15 +267,6 @@ impl Configuration {
path::ethereum::with_default("geth.ipc").to_str().unwrap().to_owned() path::ethereum::with_default("geth.ipc").to_str().unwrap().to_owned()
} }
fn replace_home(arg: &str) -> String {
arg.replace("$HOME", env::home_dir().unwrap().to_str().unwrap())
}
fn ipc_path(&self) -> String {
if self.args.flag_geth { Self::geth_ipc_path() }
else { Configuration::replace_home(&self.args.flag_ipcpath.clone().unwrap_or(self.args.flag_ipc_path.clone())) }
}
pub fn ipc_settings(&self) -> IpcConfiguration { pub fn ipc_settings(&self) -> IpcConfiguration {
IpcConfiguration { IpcConfiguration {
enabled: !(self.args.flag_ipcdisable || self.args.flag_ipc_off), enabled: !(self.args.flag_ipcdisable || self.args.flag_ipc_off),
@ -299,6 +287,36 @@ impl Configuration {
rpc_port: self.args.flag_rpcport.unwrap_or(self.args.flag_jsonrpc_port), rpc_port: self.args.flag_rpcport.unwrap_or(self.args.flag_jsonrpc_port),
} }
} }
pub fn directories(&self) -> Directories {
let db_path = Configuration::replace_home(
&self.args.flag_datadir.as_ref().unwrap_or(&self.args.flag_db_path));
std::fs::create_dir_all(db_path).unwrap_or_else(|e| die_with_io_error(e));
let keys_path = Configuration::replace_home(&self.args.flag_keys_path);
Directories {
keys: keys_path,
db: db_path,
}
}
pub fn keys_path(&self) -> String {
self.directories().keys
}
pub fn path(&self) -> String {
self.directories().db
}
fn replace_home(arg: &str) -> String {
arg.replace("$HOME", env::home_dir().unwrap().to_str().unwrap())
}
fn ipc_path(&self) -> String {
if self.args.flag_geth { Self::geth_ipc_path() }
else { Configuration::replace_home(&self.args.flag_ipcpath.clone().unwrap_or(self.args.flag_ipc_path.clone())) }
}
} }
#[cfg(test)] #[cfg(test)]