agent-ctl: Simplify error handling

Replace `ok_or().map_err()` combinations with the simpler `ok_or_else()`
construct.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
James O. D. Hunt 2021-11-22 14:21:24 +00:00
parent ac058b3897
commit c7349d0bf1

View File

@ -134,16 +134,14 @@ fn make_examples_text(program_name: &str) -> String {
fn connect(name: &str, global_args: clap::ArgMatches) -> Result<()> { fn connect(name: &str, global_args: clap::ArgMatches) -> Result<()> {
let args = global_args let args = global_args
.subcommand_matches("connect") .subcommand_matches("connect")
.ok_or("BUG: missing sub-command arguments".to_string()) .ok_or_else(|| anyhow!("BUG: missing sub-command arguments"))?;
.map_err(|e| anyhow!(e))?;
let interactive = args.is_present("interactive"); let interactive = args.is_present("interactive");
let ignore_errors = args.is_present("ignore-errors"); let ignore_errors = args.is_present("ignore-errors");
let server_address = args let server_address = args
.value_of("server-address") .value_of("server-address")
.ok_or("need server adddress".to_string()) .ok_or_else(|| anyhow!("need server adddress"))?
.map_err(|e| anyhow!(e))?
.to_string(); .to_string();
let mut commands: Vec<&str> = Vec::new(); let mut commands: Vec<&str> = Vec::new();
@ -151,8 +149,7 @@ fn connect(name: &str, global_args: clap::ArgMatches) -> Result<()> {
if !interactive { if !interactive {
commands = args commands = args
.values_of("cmd") .values_of("cmd")
.ok_or("need commands to send to the server".to_string()) .ok_or_else(|| anyhow!("need commands to send to the server"))?
.map_err(|e| anyhow!(e))?
.collect(); .collect();
} }
@ -304,8 +301,7 @@ fn real_main() -> Result<()> {
let subcmd = args let subcmd = args
.subcommand_name() .subcommand_name()
.ok_or("need sub-command".to_string()) .ok_or_else(|| anyhow!("need sub-command"))?;
.map_err(|e| anyhow!(e))?;
match subcmd { match subcmd {
"generate-cid" => { "generate-cid" => {