Skip to content

Commit

Permalink
refactor(rust): altered get_node_port so it returns a result
Browse files Browse the repository at this point in the history
  • Loading branch information
gybrish authored and mrinalwadhwa committed Oct 5, 2022
1 parent 76a2324 commit 7962f7d
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 4 additions & 1 deletion implementations/rust/ockam/ockam_command/src/node/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
15 changes: 6 additions & 9 deletions implementations/rust/ockam/ockam_command/src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<u16> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 7962f7d

Please sign in to comment.