diff --git a/implementations/rust/ockam/ockam_command/src/identity/create.rs b/implementations/rust/ockam/ockam_command/src/identity/create.rs index ffaa4ff9ebd..cb56ddc5e46 100644 --- a/implementations/rust/ockam/ockam_command/src/identity/create.rs +++ b/implementations/rust/ockam/ockam_command/src/identity/create.rs @@ -18,7 +18,7 @@ pub struct CreateCommand { impl CreateCommand { pub fn run(self, options: CommandGlobalOpts) -> anyhow::Result<()> { let cfg = options.config; - let port = cfg.get_node_port(&self.node_opts.api_node); + let port = cfg.get_node_port(&self.node_opts.api_node).unwrap(); connect_to(port, self, create_identity); diff --git a/implementations/rust/ockam/ockam_command/src/identity/show.rs b/implementations/rust/ockam/ockam_command/src/identity/show.rs index 1434e4a9a92..dee23081ef1 100644 --- a/implementations/rust/ockam/ockam_command/src/identity/show.rs +++ b/implementations/rust/ockam/ockam_command/src/identity/show.rs @@ -18,7 +18,7 @@ impl ShowCommand { pub fn run(self, options: CommandGlobalOpts) -> anyhow::Result<()> { let cfg = options.config; let node = get_final_element(&self.node_opts.api_node); - let port = cfg.get_node_port(node); + let port = cfg.get_node_port(node).unwrap(); connect_to(port, self, show_identity); diff --git a/implementations/rust/ockam/ockam_command/src/node/util.rs b/implementations/rust/ockam/ockam_command/src/node/util.rs index b4ca78117ff..51efa39e41a 100644 --- a/implementations/rust/ockam/ockam_command/src/node/util.rs +++ b/implementations/rust/ockam/ockam_command/src/node/util.rs @@ -228,7 +228,10 @@ fn delete_node_pid(opts: &CommandGlobalOpts, node_name: &str, sigkill: bool) -> // Give some room for the process to stop std::thread::sleep(std::time::Duration::from_millis(100)); // If it fails to bind, the port is still in use, so we try again to stop the process - let addr = format!("127.0.0.1:{}", opts.config.get_node_port(node_name)); + let addr = format!( + "127.0.0.1:{}", + opts.config.get_node_port(node_name).unwrap() + ); if std::net::TcpListener::bind(&addr).is_err() { startup::stop(pid, sigkill)?; } diff --git a/implementations/rust/ockam/ockam_command/src/secure_channel/listener/create.rs b/implementations/rust/ockam/ockam_command/src/secure_channel/listener/create.rs index a64ec7459e8..96a025994eb 100644 --- a/implementations/rust/ockam/ockam_command/src/secure_channel/listener/create.rs +++ b/implementations/rust/ockam/ockam_command/src/secure_channel/listener/create.rs @@ -36,7 +36,7 @@ impl CreateCommand { pub fn run(self, options: CommandGlobalOpts) { let cfg = options.config; let node = get_final_element(&self.node_opts.at); - let port = cfg.get_node_port(node); + let port = cfg.get_node_port(node).unwrap(); connect_to(port, self, |ctx, cmd, rte| async { create_listener(&ctx, cmd.address, cmd.authorized_identifier, rte).await?; diff --git a/implementations/rust/ockam/ockam_command/src/service/start.rs b/implementations/rust/ockam/ockam_command/src/service/start.rs index 8f36a355152..5ce052b9b40 100644 --- a/implementations/rust/ockam/ockam_command/src/service/start.rs +++ b/implementations/rust/ockam/ockam_command/src/service/start.rs @@ -89,7 +89,7 @@ fn authenticator_default_addr() -> String { impl StartCommand { pub fn run(self, options: CommandGlobalOpts) -> Result<()> { let cfg = options.config; - let port = cfg.get_node_port(&self.node_opts.api_node); + let port = cfg.get_node_port(&self.node_opts.api_node).unwrap(); match self.create_subcommand { StartSubCommand::Vault { .. } => connect_to(port, self, |ctx, cmd, rte| async { diff --git a/implementations/rust/ockam/ockam_command/src/tcp/connection/create.rs b/implementations/rust/ockam/ockam_command/src/tcp/connection/create.rs index b6afc789833..db3ca6ae840 100644 --- a/implementations/rust/ockam/ockam_command/src/tcp/connection/create.rs +++ b/implementations/rust/ockam/ockam_command/src/tcp/connection/create.rs @@ -40,7 +40,7 @@ impl CreateCommand { pub fn run(self, options: CommandGlobalOpts) { let cfg = &options.config; let node = get_final_element(&self.node_opts.from); - let port = cfg.get_node_port(node); + let port = cfg.get_node_port(node).unwrap(); connect_to(port, (self.clone(), options.clone()), create_connection); } diff --git a/implementations/rust/ockam/ockam_command/src/tcp/connection/delete.rs b/implementations/rust/ockam/ockam_command/src/tcp/connection/delete.rs index 046d61f0c78..b348aebf823 100644 --- a/implementations/rust/ockam/ockam_command/src/tcp/connection/delete.rs +++ b/implementations/rust/ockam/ockam_command/src/tcp/connection/delete.rs @@ -27,7 +27,7 @@ impl DeleteCommand { pub fn run(self, options: CommandGlobalOpts) { let cfg = &options.config; let node = get_final_element(&self.node_opts.api_node); - let port = cfg.get_node_port(node); + let port = cfg.get_node_port(node).unwrap(); connect_to(port, self, delete_connection); } } diff --git a/implementations/rust/ockam/ockam_command/src/tcp/connection/list.rs b/implementations/rust/ockam/ockam_command/src/tcp/connection/list.rs index 4bdc506f346..d5b702596e6 100644 --- a/implementations/rust/ockam/ockam_command/src/tcp/connection/list.rs +++ b/implementations/rust/ockam/ockam_command/src/tcp/connection/list.rs @@ -19,7 +19,7 @@ impl ListCommand { pub fn run(self, options: CommandGlobalOpts) { let cfg = &options.config; let node = get_final_element(&self.node_opts.api_node); - let port = cfg.get_node_port(node); + let port = cfg.get_node_port(node).unwrap(); connect_to(port, (), list_connections); } diff --git a/implementations/rust/ockam/ockam_command/src/tcp/inlet/create.rs b/implementations/rust/ockam/ockam_command/src/tcp/inlet/create.rs index 8bd8730c0f9..d2e4563ac61 100644 --- a/implementations/rust/ockam/ockam_command/src/tcp/inlet/create.rs +++ b/implementations/rust/ockam/ockam_command/src/tcp/inlet/create.rs @@ -68,7 +68,7 @@ impl CreateCommand { }; let node = get_final_element(&command.at); - let port = cfg.get_node_port(node); + let port = cfg.get_node_port(node).unwrap(); // Check if the port is used by some other services or process if !bind_to_port_check(&command.from) { diff --git a/implementations/rust/ockam/ockam_command/src/tcp/listener/create.rs b/implementations/rust/ockam/ockam_command/src/tcp/listener/create.rs index d15ab95016f..e913f195596 100644 --- a/implementations/rust/ockam/ockam_command/src/tcp/listener/create.rs +++ b/implementations/rust/ockam/ockam_command/src/tcp/listener/create.rs @@ -32,7 +32,7 @@ impl CreateCommand { pub fn run(self, options: CommandGlobalOpts) { let cfg = &options.config; let node = get_final_element(&self.node_opts.at); - let port = cfg.get_node_port(node); + let port = cfg.get_node_port(node).unwrap(); let input_addr = match std::net::SocketAddr::from_str(&self.address) { Ok(value) => value, diff --git a/implementations/rust/ockam/ockam_command/src/tcp/listener/delete.rs b/implementations/rust/ockam/ockam_command/src/tcp/listener/delete.rs index 3bbc4c7965c..28e4b621ced 100644 --- a/implementations/rust/ockam/ockam_command/src/tcp/listener/delete.rs +++ b/implementations/rust/ockam/ockam_command/src/tcp/listener/delete.rs @@ -27,7 +27,7 @@ impl DeleteCommand { pub fn run(self, options: CommandGlobalOpts) { let cfg = &options.config; let node = get_final_element(&self.node_opts.api_node); - let port = cfg.get_node_port(node); + let port = cfg.get_node_port(node).unwrap(); connect_to(port, self, delete_listener); } } diff --git a/implementations/rust/ockam/ockam_command/src/tcp/listener/list.rs b/implementations/rust/ockam/ockam_command/src/tcp/listener/list.rs index 556701d8e71..4818e82c56c 100644 --- a/implementations/rust/ockam/ockam_command/src/tcp/listener/list.rs +++ b/implementations/rust/ockam/ockam_command/src/tcp/listener/list.rs @@ -19,7 +19,7 @@ impl ListCommand { pub fn run(self, options: CommandGlobalOpts) { let cfg = &options.config; let node = get_final_element(&self.node_opts.api_node); - let port = cfg.get_node_port(node); + let port = cfg.get_node_port(node).unwrap(); connect_to(port, (), list_listeners); } diff --git a/implementations/rust/ockam/ockam_command/src/tcp/outlet/create.rs b/implementations/rust/ockam/ockam_command/src/tcp/outlet/create.rs index 36a931c97ad..3371aba551e 100644 --- a/implementations/rust/ockam/ockam_command/src/tcp/outlet/create.rs +++ b/implementations/rust/ockam/ockam_command/src/tcp/outlet/create.rs @@ -61,7 +61,7 @@ impl CreateCommand { let cfg = &options.config; let at = &self.at.clone(); let node = get_final_element(at); - let port = cfg.get_node_port(node); + let port = cfg.get_node_port(node).unwrap(); let command = CreateCommand { from: String::from(get_final_element(&self.from)), diff --git a/implementations/rust/ockam/ockam_command/src/util/config.rs b/implementations/rust/ockam/ockam_command/src/util/config.rs index c7498e512ad..4d2b9f1fd2e 100644 --- a/implementations/rust/ockam/ockam_command/src/util/config.rs +++ b/implementations/rust/ockam/ockam_command/src/util/config.rs @@ -11,8 +11,6 @@ pub use ockam_api::config::cli::NodeConfig; use ockam_api::config::lookup::ProjectLookup; use ockam_api::config::{cli, lookup::ConfigLookup, lookup::InternetAddress, Config}; -use crate::util::exitcode; - /// A simple wrapper around the main configuration structure to add /// local config utility/ query functions #[derive(Clone)] @@ -115,16 +113,15 @@ impl OckamConfig { } /// Get the API port used by a node - pub fn get_node_port(&self, name: &str) -> u16 { + pub fn get_node_port(&self, name: &str) -> Result { let inner = self.inner.readlock_inner(); - inner + let port = inner .nodes .get(name) - .unwrap_or_else(|| { - eprintln!("No such node available. Run `ockam node list` to list available nodes"); - std::process::exit(exitcode::IOERR); - }) - .port + .context("No such node available. Run `ockam node list` to list available nodes")? + .port; + + Ok(port) } /// In the future this will actually refer to the watchdog pid or diff --git a/implementations/rust/ockam/ockam_command/src/vault/create.rs b/implementations/rust/ockam/ockam_command/src/vault/create.rs index cb2f56b8ac0..c0a5c71ccda 100644 --- a/implementations/rust/ockam/ockam_command/src/vault/create.rs +++ b/implementations/rust/ockam/ockam_command/src/vault/create.rs @@ -20,7 +20,7 @@ pub struct CreateCommand { impl CreateCommand { pub fn run(self, options: CommandGlobalOpts) -> anyhow::Result<()> { let cfg = options.config; - let port = cfg.get_node_port(&self.node_opts.api_node); + let port = cfg.get_node_port(&self.node_opts.api_node).unwrap(); connect_to(port, self, create_vault);