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

View File

@ -14,13 +14,15 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // 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() { match arg.to_lowercase().as_ref() {
"dev" => Ok(include_str!("./config.dev.toml")), "dev" => Ok(include_str!("./config.dev.toml")),
"mining" => Ok(include_str!("./config.mining.toml")), "mining" => Ok(include_str!("./config.mining.toml")),
"non-standard-ports" => Ok(include_str!("./config.non-standard-ports.toml")), "non-standard-ports" => Ok(include_str!("./config.non-standard-ports.toml")),
"insecure" => Ok(include_str!("./config.insecure.toml")), "insecure" => Ok(include_str!("./config.insecure.toml")),
"dev-insecure" => Ok(include_str!("./config.dev-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), Docopt(DocoptError),
Decode(toml::de::Error), Decode(toml::de::Error),
Config(String, io::Error), Config(String, io::Error),
Preset(String),
} }
impl ArgsError { impl ArgsError {
@ -78,10 +77,6 @@ macro_rules! usage {
println_stderr!("{}", e); println_stderr!("{}", e);
process::exit(2) 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())); 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 = 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); let config_file = replace_home(&::dir::default_data_path(), &config_file);
match (fs::File::open(&config_file), raw_args.flag_config.is_some()) { match (fs::File::open(&config_file), raw_args.flag_config.clone()) {
// Load config file // Load config file
(Ok(mut file), _) => { (Ok(mut file), _) => {
println_stderr!("Loading config file from {}", &config_file); println_stderr!("Loading config file from {}", &config_file);
let mut config = String::new(); let mut config = String::new();
file.read_to_string(&mut config).map_err(|e| ArgsError::Config(config_file, e))?; file.read_to_string(&mut config).map_err(|e| ArgsError::Config(config_file, e))?;
Self::parse_config(&config)? Ok(raw_args.into_args(Self::parse_config(&config)?))
}, },
// Don't display error in case default config cannot be loaded. // Don't display error in case default config cannot be loaded.
(Err(_), false) => Config::default(), (Err(_), None) => Ok(raw_args.into_args(Config::default())),
// Config set from CLI (fail with error) // Config set from CLI (fail with error)
(Err(e), true) => { (Err(_), Some(ref config_arg)) => {
return Err(ArgsError::Config(config_file, e)); 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))
}
}, },
} }
};
Ok(raw_args.into_args(config))
} }
#[cfg(test)] #[cfg(test)]

View File

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

View File

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