Skip to content

Commit

Permalink
Factor out shared code from start and stop
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Feb 8, 2024
1 parent dbda60a commit d31b8be
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 82 deletions.
1 change: 1 addition & 0 deletions cmd/soroban-cli/src/commands/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
41 changes: 41 additions & 0 deletions cmd/soroban-cli/src/commands/network/shared.rs
Original file line number Diff line number Diff line change
@@ -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<String>) -> 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()
}
}
42 changes: 4 additions & 38 deletions cmd/soroban-cli/src/commands/network/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String> {
let enable_soroban_rpc = if cmd.disable_soroban_rpc {
String::new()
Expand Down
47 changes: 3 additions & 44 deletions cmd/soroban-cli/src/commands/network/stop.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,12 @@
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 {
#[error("Failed to stop container: {0}")]
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
Expand All @@ -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()
}
}

0 comments on commit d31b8be

Please sign in to comment.