refactor(rust): change `configuration get` to delegate error mapping
Refactor `configuration set` command to run `run_impl`
and handle all the errors propagated through it in a
single place
Close
use crate::{util::exitcode, CommandGlobalOpts};
+use anyhow::anyhow;
use clap::Args;
#[derive(Clone, Debug, Args)]
@@ -9,18 +10,26 @@ pub struct GetCommand {
impl GetCommand {
pub fn run(self, options: CommandGlobalOpts) {
+ if let Err(e) = run_impl(options, self) {
+ eprintln!("{}", e);
+ std::process::exit(e.code());
+ }
+ }
+}
+
+fn run_impl(options: CommandGlobalOpts, cmd: GetCommand) -> crate::Result<()> {
let lookup = options.config.lookup();
- match lookup.get_node(&self.alias) {
+ match lookup.get_node(&cmd.alias) {
Some(addr) => {
- println!("Node: {}\nAddress: {}", self.alias, addr);
+ println!("Node: {}\nAddress: {}", cmd.alias, addr);
+ Ok(())
}
- None => {
- eprintln!(
+ None => Err(crate::error::Error::new(
+ exitcode::DATAERR,
+ anyhow!(
"Alias {} not known. Add it first with `ockam alias set`!",
- self.alias
- );
- std::process::exit(exitcode::DATAERR);
- }
- }
+ cmd.alias
+ ),
+ )),
}
}
