Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit 1315141

Browse files
committed
made suggested changes, and removed some deprecated flags and options
1 parent 35dca38 commit 1315141

File tree

6 files changed

+90
-143
lines changed

6 files changed

+90
-143
lines changed

parity/cli/args.rs

Lines changed: 39 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ pub struct Args {
138138
pub arg_jsonrpc_interface: String,
139139
pub arg_jsonrpc_apis: String,
140140
pub arg_jsonrpc_hosts: String,
141-
pub arg_jsonrpc_threads: Option<usize>,
142141
pub arg_jsonrpc_server_threads: Option<usize>,
143142
pub arg_jsonrpc_cors: String,
144143
pub arg_jsonrpc_max_payload: Option<usize>,
@@ -282,7 +281,7 @@ impl Args {
282281
let (default_config, fallback_config) =
283282
Args::generate_default_configuration(default_config_path, fallback_config_path)?;
284283

285-
args.from_cli(raw_input, default_config, fallback_config)?;
284+
args.absorb_cli(raw_input, default_config, fallback_config)?;
286285

287286
Ok(args)
288287
}
@@ -294,14 +293,17 @@ impl Args {
294293
globals.convenience.config_generate = None;
295294

296295
if let Some(path) = &config_generate {
297-
let current_flags = match toml::to_string(globals) {
298-
Ok(x) => x,
299-
Err(_) => return Err(ArgsError::ConfigWriteError("Failed to generate valid config toml from current flags. Please report a bug if this error persists.".to_owned())),
300-
};
301-
302-
if let Err(_) = fs::write(&path, current_flags) {
303-
return Err(ArgsError::ConfigParseError(format!("Failed to write config to given file path: {}. Please try again with a valid path and config name.", &path)));
304-
};
296+
let current_flags = toml::to_string(globals).map_err(|e| {
297+
ArgsError::ConfigWriteError(format!(
298+
"Failed to generate valid config toml from current flags: {}.Please report a bug if this error persists.", e
299+
))
300+
})?;
301+
302+
fs::write(&path, current_flags).map_err(|_| {
303+
ArgsError::ConfigParseError(format!(
304+
"Failed to write config to given file path {}. Please try again with a valid path and config name.", &path
305+
))
306+
})?;
305307
}
306308
Ok(())
307309
}
@@ -314,40 +316,35 @@ impl Args {
314316
let default_config_file:String = get_config(default_config_path)?;
315317
let fallback_config_file:String = get_config(fallback_config_path)?;
316318

317-
let default_config: Globals = match toml::from_str(&default_config_file) {
318-
Ok(x) => x,
319-
Err(e) => {
320-
return Err(ArgsError::ConfigParseError(format!(
321-
"Failure to parse config file: {}, error: {}",
322-
default_config_path, e
323-
)))
324-
}
325-
};
326-
let fallback_config: Globals = match toml::from_str(&fallback_config_file) {
327-
Ok(x) => x,
328-
Err(_) => {
329-
return Err(ArgsError::ConfigParseError(format!(
330-
"Failure to parse config file: {}",
331-
fallback_config_path
332-
)))
333-
}
334-
};
319+
let default_config: Globals = toml::from_str(&default_config_file).map_err(|e| {
320+
ArgsError::ConfigParseError(format!(
321+
"Failure to parse config file: {}, error: {}",
322+
default_config_path, e
323+
))
324+
})?;
325+
326+
let fallback_config: Globals = toml::from_str(&fallback_config_file).map_err(|e| {
327+
ArgsError::ConfigParseError(format!(
328+
"Failure to parse config file {}: {}",
329+
fallback_config_path, e
330+
))
331+
})?;
335332

336333
Ok((default_config, fallback_config))
337334
}
338335

339-
pub fn from_cli(
336+
pub fn absorb_cli(
340337
&mut self,
341338
cli_args: ArgsInput,
342339
default_globals: Globals,
343340
fallback_globals: Globals,
344341
) -> Result<(), ArgsError> {
345-
self.from_subcommands(&cli_args)?;
346-
self.from_globals(cli_args, default_globals, fallback_globals)?;
342+
self.absorb_subcommands(&cli_args)?;
343+
self.absorb_globals(cli_args, default_globals, fallback_globals)?;
347344
Ok(())
348345
}
349346

350-
fn from_subcommands(&mut self, cli_args: &ArgsInput) -> Result<(), ArgsError> {
347+
fn absorb_subcommands(&mut self, cli_args: &ArgsInput) -> Result<(), ArgsError> {
351348
match &cli_args.subcommands {
352349
None => {}
353350
Some(subcommand) => match &subcommand {
@@ -425,13 +422,6 @@ impl Args {
425422
}
426423
}
427424
}
428-
SubCommands::Tools(t) => {
429-
self.cmd_tools = true;
430-
self.cmd_tools_hash = true;
431-
432-
let Tools::Hash { file } = t;
433-
self.arg_tools_hash_file = (*file).clone();
434-
}
435425
SubCommands::Restore(r) => {
436426
self.cmd_restore = true;
437427
self.arg_restore_file = r.file.clone();
@@ -456,44 +446,31 @@ impl Args {
456446
SubCommands::ExportHardcodedSync => {
457447
self.cmd_export_hardcoded_sync = true;
458448
}
459-
SubCommands::Dapp(dapp) => {
460-
self.cmd_dapp = true;
461-
self.arg_dapp_path = dapp.path.clone();
462-
}
449+
463450
},
464451
}
465452
Ok(())
466453
}
467454

468455
fn select_value<T>(raw: Option<T>, default: Option<T>, fallback: Option<T>) -> T {
469-
match (raw, default, fallback) {
470-
(Some(x), _, _) => x,
471-
(None, Some(x), _) => x,
472-
(None, None, Some(x)) => x,
473-
_ => todo!(), // FIXME: this case should never arise, but we need to fail gracefully if it does
474-
}
456+
raw.or(default).or(fallback).expect("Value is always present, at least in fallback")
475457
}
476458

477459
fn select_option<T>(raw: Option<T>, default: Option<T>, fallback: Option<T>) -> Option<T> {
478-
match (&raw, &default, &fallback) {
479-
(None, None, Some(_)) => fallback,
480-
(None, Some(_), _) => default,
481-
// Handles the cases where there was a raw value, so we can ignore everything else
482-
_ => raw,
483-
}
460+
raw.or(default).or(fallback)
484461
}
485462

486-
fn from_globals(
463+
fn absorb_globals(
487464
&mut self,
488465
cli_args: ArgsInput,
489466
defaults: Globals,
490467
fallback: Globals,
491468
) -> Result<(), ArgsError> {
492-
self.flags_from_globals(&cli_args, &defaults, &fallback);
493-
self.options_from_globals(cli_args, defaults, fallback);
469+
self.absorb_global_flags(&cli_args, &defaults, &fallback);
470+
self.absorb_global_options(cli_args, defaults, fallback);
494471

495472
if let (Some(min_peers), Some(max_peers)) = (self.arg_min_peers, self.arg_max_peers) {
496-
if (max_peers >= min_peers).not() {
473+
if (max_peers < min_peers){
497474
return Err(ArgsError::PeerConfigurationError(
498475
"max-peers need to be greater than or equal to min-peers".to_owned(),
499476
));
@@ -502,12 +479,12 @@ impl Args {
502479
Ok(())
503480
}
504481

505-
fn options_from_globals(&mut self, cli_args: ArgsInput, defaults: Globals, fallback: Globals) {
482+
fn absorb_global_options(&mut self, cli_args: ArgsInput, defaults: Globals, fallback: Globals) {
506483
// Unnatural cases
507484

508485
self.arg_ipc_path = Args::select_value(
509486
cli_args.globals.ipc.ipc_path,
510-
IPCOptions::ipc_path_default(),
487+
Some(IPCOptions::ipc_path_default()),
511488
None, // We don't care about fallback in this case, since the previous operation is infallible
512489
);
513490
self.arg_password = cli_args.globals.account.password;
@@ -715,11 +692,6 @@ impl Args {
715692
defaults.http_json_rpc.jsonrpc_hosts,
716693
fallback.http_json_rpc.jsonrpc_hosts,
717694
);
718-
self.arg_jsonrpc_threads = Args::select_option(
719-
cli_args.globals.http_json_rpc.jsonrpc_threads,
720-
defaults.http_json_rpc.jsonrpc_threads,
721-
fallback.http_json_rpc.jsonrpc_threads,
722-
);
723695
self.arg_jsonrpc_server_threads = Args::select_option(
724696
cli_args.globals.http_json_rpc.jsonrpc_server_threads,
725697
defaults.http_json_rpc.jsonrpc_server_threads,
@@ -1114,7 +1086,7 @@ impl Args {
11141086
);
11151087
}
11161088

1117-
fn flags_from_globals(&mut self, cli_args: &ArgsInput, defaults: &Globals, fallback: &Globals) {
1089+
fn absorb_global_flags(&mut self, cli_args: &ArgsInput, defaults: &Globals, fallback: &Globals) {
11181090
self.arg_enable_signing_queue = cli_args.globals.account.enable_signing_queue
11191091
|| defaults.account.enable_signing_queue
11201092
|| fallback.account.enable_signing_queue;

parity/cli/config.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use rust_embed::RustEmbed;
66
struct Config;
77

88
pub fn get_config(config_name: &str) -> Result<String, ArgsError> {
9-
match Config::get(config_name) {
10-
Some(x) => Ok((std::str::from_utf8(x.as_ref()).unwrap()).to_owned()),
11-
None => Err(ArgsError::ConfigReadError(format!(
12-
"Failure to read config file: {}",
13-
config_name
14-
))),
15-
}
9+
Config::get(config_name)
10+
.ok_or_else(|| "does not exist".to_owned())
11+
// .and_then(|x| srt(x).map_err(|e| e.to_owned()))
12+
.and_then(|x| String::from_utf8(x.to_vec()).map_err(|e| e.to_string()))
13+
.map_err(|e| ArgsError::ConfigReadError(format!(
14+
"Failure to read config file {}: {}",
15+
config_name, e
16+
)))
1617
}

parity/cli/globals.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ pub struct PrivateTransactions {
261261

262262
#[structopt(
263263
long = "private-signer",
264-
long = "ACCOUNT",
264+
name = "ACCOUNT",
265265
help = "Specify the account for signing public transaction created upon verified private transaction."
266266
)]
267267
pub private_signer: Option<String>,
@@ -457,11 +457,11 @@ pub struct IPCOptions {
457457
}
458458

459459
impl IPCOptions {
460-
pub fn ipc_path_default() -> Option<String> {
460+
pub fn ipc_path_default() -> String {
461461
if cfg!(windows) {
462-
Some(r"\\.\pipe\jsonrpc.ipc".to_string())
462+
r"\\.\pipe\jsonrpc.ipc".to_owned()
463463
} else {
464-
Some("$BASE/jsonrpc.ipc".to_string())
464+
"$BASE/jsonrpc.ipc".to_owned()
465465
}
466466
}
467467
}
@@ -517,13 +517,6 @@ pub struct HTTP_JSON_RPC_Options {
517517
)]
518518
pub jsonrpc_hosts: Option<String>,
519519

520-
#[structopt(
521-
long = "jsonrpc-threads",
522-
name = "JSONRPC_THREADS_NUM",
523-
help = "DEPRECATED, DOES NOTHING"
524-
)]
525-
pub jsonrpc_threads: Option<usize>,
526-
527520
#[structopt(
528521
long = "jsonrpc-server-threads",
529522
name = "JSONRPC_SERVER_THREADS",
@@ -633,7 +626,7 @@ pub struct WebsocketsOptions {
633626

634627
#[structopt(
635628
help = "Maximum number of allowed concurrent WebSockets JSON-RPC connections.",
636-
long = "ws=-connections",
629+
long = "ws-connections",
637630
name = "WS_MAX_CONN"
638631
)]
639632
pub ws_max_connections: Option<usize>,

parity/cli/subcommands.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ pub enum SubCommands {
2020
Signer(Signer),
2121
Snapshots(Snapshots),
2222
Restore(Restore),
23-
Tools(Tools),
2423
Db(Db),
2524
#[structopt(
2625
about = "Print the hashed light clients headers of the given --chain (default: mainnet) in a JSON format. To be used as hardcoded headers in a genesis file."
2726
)]
2827
ExportHardcodedSync,
29-
Dapp(Dapp),
3028
}
3129

3230
#[derive(StructOpt, Deserialize, Debug, Clone)]
@@ -208,16 +206,6 @@ pub struct Restore {
208206
pub file: Option<String>,
209207
}
210208

211-
#[derive(StructOpt, Deserialize, Debug, Clone)]
212-
#[structopt(about = "Tools")]
213-
pub enum Tools {
214-
#[structopt(about = "Hash a file using the Keccak-256 algorithm")]
215-
Hash {
216-
#[structopt(name = "FILE")]
217-
file: Option<String>,
218-
},
219-
}
220-
221209
#[derive(StructOpt, Deserialize, Debug, Clone)]
222210
#[structopt(about = "Manage the Database representing the state of the blockchain on this system")]
223211
pub enum Db {
@@ -233,10 +221,3 @@ pub enum Db {
233221
num: u32,
234222
},
235223
}
236-
237-
#[derive(StructOpt, Deserialize, Debug, Clone)]
238-
#[structopt(about = "Manage Dapps")]
239-
pub struct Dapp {
240-
#[structopt(help = "Path to the dapps", name = "PATH")]
241-
pub path: Option<String>,
242-
}

parity/cli/tests/test_cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn test_overwrite_custom_config_with_raw_flags() {
3434
let (user_defaults, fallback) =
3535
Args::generate_default_configuration("test_config.toml", "config_default.toml").unwrap();
3636

37-
resolved.from_cli(raw, user_defaults, fallback);
37+
resolved.absorb_cli(raw, user_defaults, fallback);
3838

3939
assert_eq!(resolved.arg_stratum_secret, Some("Changed".to_owned()));
4040
}
@@ -50,7 +50,7 @@ fn test_not_accepting_min_peers_bigger_than_max_peers() {
5050
raw.globals.networking.min_peers = Some(50);
5151
raw.globals.networking.max_peers = Some(40);
5252

53-
let output = resolved.from_cli(raw, user_defaults, fallback);
53+
let output = resolved.absorb_cli(raw, user_defaults, fallback);
5454

5555
assert_eq!(
5656
output,

0 commit comments

Comments
 (0)