Roll preset command into --config option (#4673)

* --config takes either toml file or bundled preset eg. '--config=dev'
* Maintains consistency with --chain arguments
This commit is contained in:
Joseph Mark 2017-07-22 10:37:38 +07:00
parent ae3dfe9327
commit 6ae93cf14e
No known key found for this signature in database
GPG Key ID: 9BA8D1EE2E53C283
5 changed files with 30 additions and 44 deletions

View File

@ -43,14 +43,12 @@ usage! {
cmd_hash: bool,
cmd_kill: bool,
cmd_db: bool,
cmd_preset: bool,
// Arguments
arg_pid_file: String,
arg_file: Option<String>,
arg_path: Vec<String>,
arg_id: Option<usize>,
arg_preset: String,
// Flags
// -- Legacy Options
@ -707,14 +705,12 @@ mod tests {
cmd_hash: false,
cmd_db: false,
cmd_kill: false,
cmd_preset: false,
// Arguments
arg_pid_file: "".into(),
arg_file: None,
arg_id: None,
arg_path: vec![],
arg_preset: "".into(),
// -- Operating Options
flag_mode: "last".into(),

View File

@ -14,13 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
pub fn preset_config_string(arg: &str) -> Result<&'static str, &str> {
use std::io::{Error, ErrorKind};
pub fn preset_config_string(arg: &str) -> Result<&'static str, Error> {
match arg.to_lowercase().as_ref() {
"dev" => Ok(include_str!("./config.dev.toml")),
"mining" => Ok(include_str!("./config.mining.toml")),
"non-standard-ports" => Ok(include_str!("./config.non-standard-ports.toml")),
"insecure" => Ok(include_str!("./config.insecure.toml")),
"dev-insecure" => Ok(include_str!("./config.dev-insecure.toml")),
_ => Err(arg.clone())
_ => Err(Error::new(ErrorKind::InvalidInput, arg.clone()))
}
}

View File

@ -61,7 +61,6 @@ macro_rules! usage {
Docopt(DocoptError),
Decode(toml::de::Error),
Config(String, io::Error),
Preset(String),
}
impl ArgsError {
@ -78,10 +77,6 @@ macro_rules! usage {
println_stderr!("{}", e);
process::exit(2)
},
ArgsError::Preset(a) => {
println_stderr!("Invalid preset argument: {}", a);
process::exit(2)
}
}
}
}
@ -154,32 +149,26 @@ macro_rules! usage {
return Ok(raw_args.into_args(Config::default()));
}
let config = if raw_args.cmd_preset {
match presets::preset_config_string(&raw_args.arg_preset) {
Ok(s) => Self::parse_config(&s)?,
Err(e) => return Err(ArgsError::Preset(e.to_string()))
}
} else {
let config_file = raw_args.flag_config.clone().unwrap_or_else(|| raw_args.clone().into_args(Config::default()).flag_config);
let config_file = replace_home(&::dir::default_data_path(), &config_file);
match (fs::File::open(&config_file), raw_args.flag_config.is_some()) {
// Load config file
(Ok(mut file), _) => {
println_stderr!("Loading config file from {}", &config_file);
let mut config = String::new();
file.read_to_string(&mut config).map_err(|e| ArgsError::Config(config_file, e))?;
Self::parse_config(&config)?
},
// Don't display error in case default config cannot be loaded.
(Err(_), false) => Config::default(),
// Config set from CLI (fail with error)
(Err(e), true) => {
return Err(ArgsError::Config(config_file, e));
},
}
};
Ok(raw_args.into_args(config))
let config_file = raw_args.flag_config.clone().unwrap_or_else(|| raw_args.clone().into_args(Config::default()).flag_config);
let config_file = replace_home(&::dir::default_data_path(), &config_file);
match (fs::File::open(&config_file), raw_args.flag_config.clone()) {
// Load config file
(Ok(mut file), _) => {
println_stderr!("Loading config file from {}", &config_file);
let mut config = String::new();
file.read_to_string(&mut config).map_err(|e| ArgsError::Config(config_file, e))?;
Ok(raw_args.into_args(Self::parse_config(&config)?))
},
// Don't display error in case default config cannot be loaded.
(Err(_), None) => Ok(raw_args.into_args(Config::default())),
// Config set from CLI (fail with error)
(Err(_), Some(ref config_arg)) => {
match presets::preset_config_string(config_arg) {
Ok(s) => Ok(raw_args.into_args(Self::parse_config(&s)?)),
Err(e) => Err(ArgsError::Config(config_file, e))
}
},
}
}
#[cfg(test)]

View File

@ -20,7 +20,6 @@ Usage:
parity restore [ <file> ] [options]
parity tools hash <file>
parity db kill [options]
parity preset <preset> [options]
Operating Options:
--mode MODE Set the operating mode. MODE can be one of:

View File

@ -1595,7 +1595,7 @@ mod tests {
#[test]
fn test_dev_preset() {
let args = vec!["parity", "preset", "dev"];
let args = vec!["parity", "--config", "dev"];
let conf = Configuration::parse(&args, None).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
@ -1609,7 +1609,7 @@ mod tests {
#[test]
fn test_mining_preset() {
let args = vec!["parity", "preset", "mining"];
let args = vec!["parity", "--config", "mining"];
let conf = Configuration::parse(&args, None).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
@ -1631,7 +1631,7 @@ mod tests {
#[test]
fn test_non_standard_ports_preset() {
let args = vec!["parity", "preset", "non-standard-ports"];
let args = vec!["parity", "--config", "non-standard-ports"];
let conf = Configuration::parse(&args, None).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
@ -1644,7 +1644,7 @@ mod tests {
#[test]
fn test_insecure_preset() {
let args = vec!["parity", "preset", "insecure"];
let args = vec!["parity", "--config", "insecure"];
let conf = Configuration::parse(&args, None).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
@ -1664,7 +1664,7 @@ mod tests {
#[test]
fn test_dev_insecure_preset() {
let args = vec!["parity", "preset", "dev-insecure"];
let args = vec!["parity", "--config", "dev-insecure"];
let conf = Configuration::parse(&args, None).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
@ -1687,7 +1687,7 @@ mod tests {
#[test]
fn test_override_preset() {
let args = vec!["parity", "preset", "mining", "--min-peers=99"];
let args = vec!["parity", "--config", "mining", "--min-peers=99"];
let conf = Configuration::parse(&args, None).unwrap();
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {