Some tweaks to main.rs for parity as a library (#8370)

* Some tweaks to main.rs for parity as a library

* Remove pub from PostExecutionAction
This commit is contained in:
Pierre Krieger 2018-04-13 14:21:15 +02:00 committed by GitHub
parent 869fa6fda8
commit 03b96a7c0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 26 deletions

View File

@ -95,16 +95,14 @@ pub struct Execute {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Configuration { pub struct Configuration {
pub args: Args, pub args: Args,
pub spec_name_override: Option<String>,
} }
impl Configuration { impl Configuration {
pub fn parse<S: AsRef<str>>(command: &[S], spec_name_override: Option<String>) -> Result<Self, ArgsError> { pub fn parse<S: AsRef<str>>(command: &[S]) -> Result<Self, ArgsError> {
let args = Args::parse(command)?; let args = Args::parse(command)?;
let config = Configuration { let config = Configuration {
args: args, args: args,
spec_name_override: spec_name_override,
}; };
Ok(config) Ok(config)
@ -462,9 +460,7 @@ impl Configuration {
} }
fn chain(&self) -> Result<SpecType, String> { fn chain(&self) -> Result<SpecType, String> {
let name = if let Some(ref s) = self.spec_name_override { let name = if self.args.flag_testnet {
s.clone()
} else if self.args.flag_testnet {
"testnet".to_owned() "testnet".to_owned()
} else { } else {
self.args.arg_chain.clone() self.args.arg_chain.clone()
@ -1264,7 +1260,6 @@ mod tests {
fn parse(args: &[&str]) -> Configuration { fn parse(args: &[&str]) -> Configuration {
Configuration { Configuration {
args: Args::parse_without_config(args).unwrap(), args: Args::parse_without_config(args).unwrap(),
spec_name_override: None,
} }
} }
@ -1844,7 +1839,7 @@ mod tests {
let filename = tempdir.path().join("peers"); let filename = tempdir.path().join("peers");
File::create(&filename).unwrap().write_all(b" \n\t\n").unwrap(); File::create(&filename).unwrap().write_all(b" \n\t\n").unwrap();
let args = vec!["parity", "--reserved-peers", filename.to_str().unwrap()]; let args = vec!["parity", "--reserved-peers", filename.to_str().unwrap()];
let conf = Configuration::parse(&args, None).unwrap(); let conf = Configuration::parse(&args).unwrap();
assert!(conf.init_reserved_nodes().is_ok()); assert!(conf.init_reserved_nodes().is_ok());
} }
@ -1854,7 +1849,7 @@ mod tests {
let filename = tempdir.path().join("peers_comments"); let filename = tempdir.path().join("peers_comments");
File::create(&filename).unwrap().write_all(b"# Sample comment\nenode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@172.0.0.1:30303\n").unwrap(); File::create(&filename).unwrap().write_all(b"# Sample comment\nenode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@172.0.0.1:30303\n").unwrap();
let args = vec!["parity", "--reserved-peers", filename.to_str().unwrap()]; let args = vec!["parity", "--reserved-peers", filename.to_str().unwrap()];
let conf = Configuration::parse(&args, None).unwrap(); let conf = Configuration::parse(&args).unwrap();
let reserved_nodes = conf.init_reserved_nodes(); let reserved_nodes = conf.init_reserved_nodes();
assert!(reserved_nodes.is_ok()); assert!(reserved_nodes.is_ok());
assert_eq!(reserved_nodes.unwrap().len(), 1); assert_eq!(reserved_nodes.unwrap().len(), 1);
@ -1863,7 +1858,7 @@ mod tests {
#[test] #[test]
fn test_dev_preset() { fn test_dev_preset() {
let args = vec!["parity", "--config", "dev"]; let args = vec!["parity", "--config", "dev"];
let conf = Configuration::parse(&args, None).unwrap(); let conf = Configuration::parse(&args).unwrap();
match conf.into_command().unwrap().cmd { match conf.into_command().unwrap().cmd {
Cmd::Run(c) => { Cmd::Run(c) => {
assert_eq!(c.net_settings.chain, "dev"); assert_eq!(c.net_settings.chain, "dev");
@ -1877,7 +1872,7 @@ mod tests {
#[test] #[test]
fn test_mining_preset() { fn test_mining_preset() {
let args = vec!["parity", "--config", "mining"]; let args = vec!["parity", "--config", "mining"];
let conf = Configuration::parse(&args, None).unwrap(); let conf = Configuration::parse(&args).unwrap();
match conf.into_command().unwrap().cmd { match conf.into_command().unwrap().cmd {
Cmd::Run(c) => { Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 50); assert_eq!(c.net_conf.min_peers, 50);
@ -1899,7 +1894,7 @@ mod tests {
#[test] #[test]
fn test_non_standard_ports_preset() { fn test_non_standard_ports_preset() {
let args = vec!["parity", "--config", "non-standard-ports"]; let args = vec!["parity", "--config", "non-standard-ports"];
let conf = Configuration::parse(&args, None).unwrap(); let conf = Configuration::parse(&args).unwrap();
match conf.into_command().unwrap().cmd { match conf.into_command().unwrap().cmd {
Cmd::Run(c) => { Cmd::Run(c) => {
assert_eq!(c.net_settings.network_port, 30305); assert_eq!(c.net_settings.network_port, 30305);
@ -1912,7 +1907,7 @@ mod tests {
#[test] #[test]
fn test_insecure_preset() { fn test_insecure_preset() {
let args = vec!["parity", "--config", "insecure"]; let args = vec!["parity", "--config", "insecure"];
let conf = Configuration::parse(&args, None).unwrap(); let conf = Configuration::parse(&args).unwrap();
match conf.into_command().unwrap().cmd { match conf.into_command().unwrap().cmd {
Cmd::Run(c) => { Cmd::Run(c) => {
assert_eq!(c.update_policy.require_consensus, false); assert_eq!(c.update_policy.require_consensus, false);
@ -1932,7 +1927,7 @@ mod tests {
#[test] #[test]
fn test_dev_insecure_preset() { fn test_dev_insecure_preset() {
let args = vec!["parity", "--config", "dev-insecure"]; let args = vec!["parity", "--config", "dev-insecure"];
let conf = Configuration::parse(&args, None).unwrap(); let conf = Configuration::parse(&args).unwrap();
match conf.into_command().unwrap().cmd { match conf.into_command().unwrap().cmd {
Cmd::Run(c) => { Cmd::Run(c) => {
assert_eq!(c.net_settings.chain, "dev"); assert_eq!(c.net_settings.chain, "dev");
@ -1955,7 +1950,7 @@ mod tests {
#[test] #[test]
fn test_override_preset() { fn test_override_preset() {
let args = vec!["parity", "--config", "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).unwrap();
match conf.into_command().unwrap().cmd { match conf.into_command().unwrap().cmd {
Cmd::Run(c) => { Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 99); assert_eq!(c.net_conf.min_peers, 99);
@ -2078,7 +2073,7 @@ mod tests {
#[test] #[test]
fn should_respect_only_max_peers_and_default() { fn should_respect_only_max_peers_and_default() {
let args = vec!["parity", "--max-peers=50"]; let args = vec!["parity", "--max-peers=50"];
let conf = Configuration::parse(&args, None).unwrap(); let conf = Configuration::parse(&args).unwrap();
match conf.into_command().unwrap().cmd { match conf.into_command().unwrap().cmd {
Cmd::Run(c) => { Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 25); assert_eq!(c.net_conf.min_peers, 25);
@ -2091,7 +2086,7 @@ mod tests {
#[test] #[test]
fn should_respect_only_max_peers_less_than_default() { fn should_respect_only_max_peers_less_than_default() {
let args = vec!["parity", "--max-peers=5"]; let args = vec!["parity", "--max-peers=5"];
let conf = Configuration::parse(&args, None).unwrap(); let conf = Configuration::parse(&args).unwrap();
match conf.into_command().unwrap().cmd { match conf.into_command().unwrap().cmd {
Cmd::Run(c) => { Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 5); assert_eq!(c.net_conf.min_peers, 5);
@ -2104,7 +2099,7 @@ mod tests {
#[test] #[test]
fn should_respect_only_min_peers_and_default() { fn should_respect_only_min_peers_and_default() {
let args = vec!["parity", "--min-peers=5"]; let args = vec!["parity", "--min-peers=5"];
let conf = Configuration::parse(&args, None).unwrap(); let conf = Configuration::parse(&args).unwrap();
match conf.into_command().unwrap().cmd { match conf.into_command().unwrap().cmd {
Cmd::Run(c) => { Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 5); assert_eq!(c.net_conf.min_peers, 5);
@ -2117,7 +2112,7 @@ mod tests {
#[test] #[test]
fn should_respect_only_min_peers_and_greater_than_default() { fn should_respect_only_min_peers_and_greater_than_default() {
let args = vec!["parity", "--min-peers=500"]; let args = vec!["parity", "--min-peers=500"];
let conf = Configuration::parse(&args, None).unwrap(); let conf = Configuration::parse(&args).unwrap();
match conf.into_command().unwrap().cmd { match conf.into_command().unwrap().cmd {
Cmd::Run(c) => { Cmd::Run(c) => {
assert_eq!(c.net_conf.min_peers, 500); assert_eq!(c.net_conf.min_peers, 500);

View File

@ -180,9 +180,10 @@ fn execute(command: Execute, can_restart: bool) -> Result<PostExecutionAction, S
} }
} }
fn start(can_restart: bool) -> Result<PostExecutionAction, String> { fn start(mut args: Vec<String>) -> Result<PostExecutionAction, String> {
let args: Vec<String> = env::args().collect(); args.insert(0, "parity".to_owned());
let conf = Configuration::parse(&args, take_spec_name_override()).unwrap_or_else(|e| e.exit()); let conf = Configuration::parse(&args).unwrap_or_else(|e| e.exit());
let can_restart = conf.args.flag_can_restart;
let deprecated = find_deprecated(&conf.args); let deprecated = find_deprecated(&conf.args);
for d in deprecated { for d in deprecated {
@ -276,7 +277,7 @@ const PLEASE_RESTART_EXIT_CODE: i32 = 69;
// Run our version of parity. // Run our version of parity.
// Returns the exit error code. // Returns the exit error code.
fn main_direct(can_restart: bool) -> i32 { fn main_direct(force_can_restart: bool) -> i32 {
global_init(); global_init();
let mut alt_mains = HashMap::new(); let mut alt_mains = HashMap::new();
sync_main(&mut alt_mains); sync_main(&mut alt_mains);
@ -285,7 +286,25 @@ fn main_direct(can_restart: bool) -> i32 {
f(); f();
0 0
} else { } else {
match start(can_restart) { let mut args = std::env::args().skip(1).collect::<Vec<_>>();
if force_can_restart && !args.iter().any(|arg| arg == "--can-restart") {
args.push("--can-restart".to_owned());
}
if let Some(spec_override) = take_spec_name_override() {
args.retain(|f| f != "--testnet");
args.retain(|f| !f.starts_with("--chain="));
while let Some(pos) = args.iter().position(|a| a == "--chain") {
if args.len() > pos + 1 {
args.remove(pos + 1);
}
args.remove(pos);
}
args.push("--chain".to_owned());
args.push(spec_override);
}
match start(args) {
Ok(result) => match result { Ok(result) => match result {
PostExecutionAction::Print(s) => { println!("{}", s); 0 }, PostExecutionAction::Print(s) => { println!("{}", s); 0 },
PostExecutionAction::Restart(spec_name_override) => { PostExecutionAction::Restart(spec_name_override) => {
@ -365,7 +384,6 @@ fn main() {
} else { } else {
trace_main!("Running direct"); trace_main!("Running direct");
// Otherwise, we're presumably running the version we want. Just run and fall-through. // Otherwise, we're presumably running the version we want. Just run and fall-through.
let can_restart = std::env::args().any(|arg| arg == "--can-restart"); process::exit(main_direct(false));
process::exit(main_direct(can_restart));
} }
} }