2018-03-05 22:33:44 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2017-07-13 20:30:33 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Configuration file management
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::fs::File;
|
|
|
|
|
|
|
|
use toml;
|
2018-04-24 11:18:24 +03:00
|
|
|
use servers::{ServerConfig, StratumServerConfig};
|
2017-10-12 19:56:44 +03:00
|
|
|
use util::LoggingConfig;
|
2017-11-01 02:32:33 +03:00
|
|
|
use types::{ConfigError, ConfigMembers, GlobalConfig};
|
2017-12-07 22:04:17 +03:00
|
|
|
use wallet::WalletConfig;
|
2017-07-13 20:30:33 +03:00
|
|
|
|
|
|
|
/// The default file name to use when trying to derive
|
|
|
|
/// the config file location
|
|
|
|
|
|
|
|
const CONFIG_FILE_NAME: &'static str = "grin.toml";
|
|
|
|
const GRIN_HOME: &'static str = ".grin";
|
|
|
|
|
|
|
|
/// Returns the defaults, as strewn throughout the code
|
|
|
|
|
|
|
|
impl Default for ConfigMembers {
|
|
|
|
fn default() -> ConfigMembers {
|
2017-09-26 20:58:56 +03:00
|
|
|
ConfigMembers {
|
|
|
|
server: ServerConfig::default(),
|
2018-04-24 11:18:24 +03:00
|
|
|
mining_server: Some(StratumServerConfig::default()),
|
2017-10-12 19:56:44 +03:00
|
|
|
logging: Some(LoggingConfig::default()),
|
2017-12-07 22:04:17 +03:00
|
|
|
wallet: WalletConfig::default(),
|
2017-09-26 20:58:56 +03:00
|
|
|
}
|
|
|
|
}
|
2017-07-13 20:30:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for GlobalConfig {
|
2017-09-26 20:58:56 +03:00
|
|
|
fn default() -> GlobalConfig {
|
|
|
|
GlobalConfig {
|
|
|
|
config_file_path: None,
|
|
|
|
using_config_file: false,
|
|
|
|
members: Some(ConfigMembers::default()),
|
|
|
|
}
|
|
|
|
}
|
2017-07-13 20:30:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GlobalConfig {
|
2017-09-26 20:58:56 +03:00
|
|
|
/// Need to decide on rules where to read the config file from,
|
|
|
|
/// but will take a stab at logic for now
|
2017-07-13 20:30:33 +03:00
|
|
|
|
2017-09-26 20:58:56 +03:00
|
|
|
fn derive_config_location(&mut self) -> Result<(), ConfigError> {
|
|
|
|
// First, check working directory
|
|
|
|
let mut config_path = env::current_dir().unwrap();
|
|
|
|
config_path.push(CONFIG_FILE_NAME);
|
|
|
|
if config_path.exists() {
|
|
|
|
self.config_file_path = Some(config_path);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
// Next, look in directory of executable
|
|
|
|
let mut config_path = env::current_exe().unwrap();
|
2017-07-13 20:30:33 +03:00
|
|
|
config_path.pop();
|
|
|
|
config_path.push(CONFIG_FILE_NAME);
|
2017-09-26 20:58:56 +03:00
|
|
|
if config_path.exists() {
|
|
|
|
self.config_file_path = Some(config_path);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
// Then look in {user_home}/.grin
|
|
|
|
let config_path = env::home_dir();
|
|
|
|
if let Some(mut p) = config_path {
|
|
|
|
p.push(GRIN_HOME);
|
|
|
|
p.push(CONFIG_FILE_NAME);
|
|
|
|
if p.exists() {
|
|
|
|
self.config_file_path = Some(p);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give up
|
|
|
|
Err(ConfigError::FileNotFoundError(String::from("")))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Takes the path to a config file, or if NONE, tries
|
|
|
|
/// to determine a config file based on rules in
|
|
|
|
/// derive_config_location
|
|
|
|
|
|
|
|
pub fn new(file_path: Option<&str>) -> Result<GlobalConfig, ConfigError> {
|
|
|
|
let mut return_value = GlobalConfig::default();
|
|
|
|
if let Some(fp) = file_path {
|
|
|
|
return_value.config_file_path = Some(PathBuf::from(&fp));
|
|
|
|
} else {
|
2017-11-01 02:32:33 +03:00
|
|
|
let _result = return_value.derive_config_location();
|
2017-09-26 20:58:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// No attempt at a config file, just return defaults
|
|
|
|
if let None = return_value.config_file_path {
|
|
|
|
return Ok(return_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config file path is given but not valid
|
|
|
|
if !return_value.config_file_path.as_mut().unwrap().exists() {
|
|
|
|
return Err(ConfigError::FileNotFoundError(String::from(
|
|
|
|
return_value
|
|
|
|
.config_file_path
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.clone(),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to parse the config file if it exists
|
2018-03-04 03:19:54 +03:00
|
|
|
// explode if it does exist but something's wrong
|
|
|
|
// with it
|
2017-09-26 20:58:56 +03:00
|
|
|
return_value.read_config()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read config
|
|
|
|
pub fn read_config(mut self) -> Result<GlobalConfig, ConfigError> {
|
|
|
|
let mut file = File::open(self.config_file_path.as_mut().unwrap())?;
|
|
|
|
let mut contents = String::new();
|
|
|
|
file.read_to_string(&mut contents)?;
|
|
|
|
let decoded: Result<ConfigMembers, toml::de::Error> = toml::from_str(&contents);
|
|
|
|
match decoded {
|
|
|
|
Ok(mut gc) => {
|
|
|
|
// Put the struct back together, because the config
|
2017-12-07 22:04:17 +03:00
|
|
|
// file was flattened a bit
|
2018-04-24 11:18:24 +03:00
|
|
|
gc.server.stratum_mining_config = gc.mining_server.clone();
|
2017-09-26 20:58:56 +03:00
|
|
|
self.using_config_file = true;
|
|
|
|
self.members = Some(gc);
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
return Err(ConfigError::ParseError(
|
|
|
|
String::from(
|
|
|
|
self.config_file_path
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.clone(),
|
|
|
|
),
|
|
|
|
String::from(format!("{}", e)),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serialize config
|
|
|
|
pub fn ser_config(&mut self) -> Result<String, ConfigError> {
|
|
|
|
let encoded: Result<String, toml::ser::Error> =
|
|
|
|
toml::to_string(self.members.as_mut().unwrap());
|
|
|
|
match encoded {
|
|
|
|
Ok(enc) => return Ok(enc),
|
|
|
|
Err(e) => {
|
2018-03-04 03:19:54 +03:00
|
|
|
return Err(ConfigError::SerializationError(String::from(format!(
|
|
|
|
"{}",
|
|
|
|
e
|
|
|
|
))));
|
2017-09-26 20:58:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*pub fn wallet_enabled(&mut self) -> bool {
|
2017-07-13 20:30:33 +03:00
|
|
|
return self.members.as_mut().unwrap().wallet.as_mut().unwrap().enable_wallet;
|
|
|
|
}*/
|
|
|
|
|
2017-09-26 20:58:56 +03:00
|
|
|
/// Enable mining
|
2018-04-24 11:18:24 +03:00
|
|
|
pub fn stratum_enabled(&mut self) -> bool {
|
2017-09-26 20:58:56 +03:00
|
|
|
return self.members
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
2018-04-24 11:18:24 +03:00
|
|
|
.mining_server
|
2017-09-26 20:58:56 +03:00
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
2018-04-24 11:18:24 +03:00
|
|
|
.enable_stratum_server
|
|
|
|
.unwrap();
|
2017-09-26 20:58:56 +03:00
|
|
|
}
|
2017-07-13 20:30:33 +03:00
|
|
|
}
|