diff --git a/cmd/soroban-cli/src/commands/network/mod.rs b/cmd/soroban-cli/src/commands/network/mod.rs index c64d1b4c45..3555df8200 100644 --- a/cmd/soroban-cli/src/commands/network/mod.rs +++ b/cmd/soroban-cli/src/commands/network/mod.rs @@ -15,6 +15,7 @@ use super::config::locator; pub mod add; pub mod ls; pub mod rm; +pub mod shared; pub mod start; pub mod stop; diff --git a/cmd/soroban-cli/src/commands/network/shared.rs b/cmd/soroban-cli/src/commands/network/shared.rs new file mode 100644 index 0000000000..7fc34a04d4 --- /dev/null +++ b/cmd/soroban-cli/src/commands/network/shared.rs @@ -0,0 +1,41 @@ +use core::fmt; + +use bollard::{ClientVersion, Docker}; +use clap::ValueEnum; + +// DEFAULT_TIMEOUT and API_DEFAULT_VERSION are from the bollard crate +const DEFAULT_TIMEOUT: u64 = 120; +const API_DEFAULT_VERSION: &ClientVersion = &ClientVersion { + major_version: 1, + minor_version: 40, +}; + +#[derive(ValueEnum, Debug, Clone, PartialEq)] +pub enum Network { + Local, + Testnet, + Futurenet, + Pubnet, +} + +impl fmt::Display for Network { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let variant_str = match self { + Network::Local => "local", + Network::Testnet => "testnet", + Network::Futurenet => "futurenet", + Network::Pubnet => "pubnet", + }; + + write!(f, "{}", variant_str) + } +} + +pub fn connect_to_docker(docker_socket_path: Option) -> Docker { + if docker_socket_path.is_some() { + let socket = docker_socket_path.as_ref().unwrap(); + Docker::connect_with_socket(socket, DEFAULT_TIMEOUT, API_DEFAULT_VERSION).unwrap() + } else { + Docker::connect_with_socket_defaults().unwrap() + } +} diff --git a/cmd/soroban-cli/src/commands/network/start.rs b/cmd/soroban-cli/src/commands/network/start.rs index aacf9b7e10..b920a32030 100644 --- a/cmd/soroban-cli/src/commands/network/start.rs +++ b/cmd/soroban-cli/src/commands/network/start.rs @@ -9,40 +9,15 @@ use core::fmt; use futures_util::TryStreamExt; use std::collections::HashMap; +use crate::commands::network::shared::connect_to_docker; +use crate::commands::network::shared::Network; + const DEFAULT_PORT_MAPPING: &str = "8000:8000"; const DOCKER_IMAGE: &str = "docker.io/stellar/quickstart"; -// DEFAULT_TIMEOUT and API_DEFAULT_VERSION are from the bollard crate -const DEFAULT_TIMEOUT: u64 = 120; -const API_DEFAULT_VERSION: &ClientVersion = &ClientVersion { - major_version: 1, - minor_version: 40, -}; - #[derive(thiserror::Error, Debug)] pub enum Error {} -#[derive(ValueEnum, Debug, Clone, PartialEq)] -pub enum Network { - Local, - Testnet, - Futurenet, - Pubnet, -} - -impl fmt::Display for Network { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let variant_str = match self { - Network::Local => "local", - Network::Testnet => "testnet", - Network::Futurenet => "futurenet", - Network::Pubnet => "pubnet", - }; - - write!(f, "{}", variant_str) - } -} - #[derive(Debug, clap::Parser, Clone)] pub struct Cmd { /// network to start @@ -82,7 +57,7 @@ impl Cmd { } async fn run_docker_command(cmd: &Cmd) { - let docker = connect_to_docker(cmd); + let docker = connect_to_docker(cmd.docker_socket_path.clone()); let image = get_image_name(cmd); let create_image_options = Some(CreateImageOptions { @@ -124,15 +99,6 @@ async fn run_docker_command(cmd: &Cmd) { .await; } -fn connect_to_docker(cmd: &Cmd) -> Docker { - if cmd.docker_socket_path.is_some() { - let socket = cmd.docker_socket_path.as_ref().unwrap(); - Docker::connect_with_socket(socket, DEFAULT_TIMEOUT, API_DEFAULT_VERSION).unwrap() - } else { - Docker::connect_with_socket_defaults().unwrap() - } -} - fn get_container_args(cmd: &Cmd) -> Vec { let enable_soroban_rpc = if cmd.disable_soroban_rpc { String::new() diff --git a/cmd/soroban-cli/src/commands/network/stop.rs b/cmd/soroban-cli/src/commands/network/stop.rs index ebe34bd82d..637bb80b3c 100644 --- a/cmd/soroban-cli/src/commands/network/stop.rs +++ b/cmd/soroban-cli/src/commands/network/stop.rs @@ -1,7 +1,5 @@ -use core::fmt; - -use bollard::{ClientVersion, Docker}; -use clap::ValueEnum; +use crate::commands::network::shared::connect_to_docker; +use crate::commands::network::shared::Network; #[derive(thiserror::Error, Debug)] pub enum Error { @@ -9,35 +7,6 @@ pub enum Error { StopContainerError(#[from] bollard::errors::Error), } -// DEFAULT_TIMEOUT and API_DEFAULT_VERSION are from the bollard crate -const DEFAULT_TIMEOUT: u64 = 120; -const API_DEFAULT_VERSION: &ClientVersion = &ClientVersion { - major_version: 1, - minor_version: 40, -}; - -// TODO: move to a shared module -#[derive(ValueEnum, Debug, Clone, PartialEq)] -pub enum Network { - Local, - Testnet, - Futurenet, - Pubnet, -} - -impl fmt::Display for Network { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let variant_str = match self { - Network::Local => "local", - Network::Testnet => "testnet", - Network::Futurenet => "futurenet", - Network::Pubnet => "pubnet", - }; - - write!(f, "{}", variant_str) - } -} - #[derive(Debug, clap::Parser, Clone)] pub struct Cmd { /// network container to stop @@ -51,20 +20,10 @@ pub struct Cmd { impl Cmd { pub async fn run(&self) -> Result<(), Error> { let container_name = format!("stellar-{}", self.network); - let docker = connect_to_docker(self); + let docker = connect_to_docker(self.docker_socket_path.clone()); println!("Stopping container: {container_name}"); docker.stop_container(&container_name, None).await.unwrap(); Ok(()) } } - -//TODO: move to a shared module -fn connect_to_docker(cmd: &Cmd) -> Docker { - if cmd.docker_socket_path.is_some() { - let socket = cmd.docker_socket_path.as_ref().unwrap(); - Docker::connect_with_socket(socket, DEFAULT_TIMEOUT, API_DEFAULT_VERSION).unwrap() - } else { - Docker::connect_with_socket_defaults().unwrap() - } -}