Skip to content

Commit

Permalink
Simplify stop and logs commmands
Browse files Browse the repository at this point in the history
Instead of allowing these commands to take in either --network or
--container_name this commit change these commands to just require the
name of the container <NAME>

This commit also renames the container_name args to name
  • Loading branch information
elizabethengelman committed Jul 16, 2024
1 parent d6d495f commit d21365a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 48 deletions.
11 changes: 4 additions & 7 deletions cmd/soroban-cli/src/commands/network/container/logs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use futures_util::TryStreamExt;

use crate::commands::network::container::shared::{
connect_to_docker, Error as ConnectionError, Network,
};
use crate::commands::network::container::shared::{connect_to_docker, Error as ConnectionError};

use super::shared::Args;

Expand All @@ -20,14 +18,13 @@ pub struct Cmd {
#[command(flatten)]
pub container_args: Args,

/// Network container to tail (used in container name generation)
#[arg(required_unless_present = "container_name")]
pub network: Option<Network>,
/// Container to get logs from
pub name: String,
}

impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
let container_name = self.container_args.get_container_name(self.network);
let container_name = self.name.clone();
let docker = connect_to_docker(&self.container_args.docker_host).await?;
let logs_stream = &mut docker.logs(
&container_name,
Expand Down
31 changes: 0 additions & 31 deletions cmd/soroban-cli/src/commands/network/container/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ pub enum Error {

#[derive(Debug, clap::Parser, Clone)]
pub struct Args {
/// Optional argument to specify the container name
#[arg(short = 'c', long, required_unless_present = "network")]
pub container_name: Option<String>,

/// Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock
#[arg(short = 'd', long, help = DOCKER_HOST_HELP, env = "DOCKER_HOST")]
pub docker_host: Option<String>,
Expand All @@ -49,33 +45,6 @@ impl Args {
.map(|docker_host| format!("--docker-host {docker_host}"))
.unwrap_or_default()
}

pub(crate) fn get_container_name(&self, network: Option<Network>) -> String {
self.container_name.as_ref().map_or_else(
|| {
format!(
"stellar-{}",
network.expect("Container name and/or network are required.")
)
},
|container_name| container_name.to_string(),
)
}

// This method is used in start.rs to create a message for the user to let them know how to stop the container they
// just started, and how to view its logs. For both `stop` and `logs` the user is able to pass in either the network
// (and we generate the container name) or the container name directly. Which is why we need to check if the
// container_name is present or not here.
pub(crate) fn get_container_name_arg(&self, network: Option<Network>) -> String {
self.container_name.as_ref().map_or_else(
|| {
network
.expect("Container name and/or network are required.")
.to_string()
},
|container_name| format!("--container-name {container_name}"),
)
}
}

#[derive(ValueEnum, Debug, Copy, Clone, PartialEq)]
Expand Down
21 changes: 16 additions & 5 deletions cmd/soroban-cli/src/commands/network/container/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub struct Cmd {
/// Network to start
pub network: Network,

/// Optional argument to specify the container name
#[arg(long)]
pub name: Option<String>,

/// Optional argument to specify the limits for the local network only
#[arg(short = 'l', long)]
pub limits: Option<String>,
Expand Down Expand Up @@ -89,7 +93,7 @@ async fn run_docker_command(cmd: &Cmd) -> Result<(), Error> {
..Default::default()
};

let container_name = cmd.container_args.get_container_name(Some(cmd.network));
let container_name = get_container_name(cmd);
let create_container_response = docker
.create_container(
Some(CreateContainerOptions {
Expand All @@ -112,19 +116,26 @@ async fn run_docker_command(cmd: &Cmd) -> Result<(), Error> {
Ok(())
}

fn get_container_name(cmd: &Cmd) -> String {
cmd.name.as_ref().map_or_else(
|| format!("stellar-{}", cmd.network),
std::string::ToString::to_string,
)
}

fn print_log_message(cmd: &Cmd) {
let log_message = format!(
"ℹ️ To see the logs for this container run: stellar network container logs {arg} {additional_flags}",
arg = cmd.container_args.get_container_name_arg(Some(cmd.network)),
"ℹ️ To see the logs for this container run: stellar network container logs {name} {additional_flags}",
name = get_container_name(cmd),
additional_flags = cmd.container_args.get_additional_flags(),
);
println!("{log_message}");
}

fn print_stop_message(cmd: &Cmd) {
let stop_message = format!(
"ℹ️ To stop this container run: stellar network container stop {arg} {additional_flags}",
arg = cmd.container_args.get_container_name_arg(Some(cmd.network)),
"ℹ️ To stop this container run: stellar network container stop {name} {additional_flags}",
name = get_container_name(cmd),
additional_flags = cmd.container_args.get_additional_flags(),
);
println!("{stop_message}");
Expand Down
9 changes: 4 additions & 5 deletions cmd/soroban-cli/src/commands/network/container/stop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::commands::network::container::shared::{
connect_to_docker, Error as BollardConnectionError, Network,
connect_to_docker, Error as BollardConnectionError,
};

use super::shared::Args;
Expand All @@ -25,14 +25,13 @@ pub struct Cmd {
#[command(flatten)]
pub container_args: Args,

/// Network to stop (used in container name generation)
#[arg(required_unless_present = "container_name")]
pub network: Option<Network>,
/// Container to stop
pub name: String,
}

impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
let container_name = self.container_args.get_container_name(self.network);
let container_name = self.name.clone();
let docker = connect_to_docker(&self.container_args.docker_host).await?;
println!("ℹ️ Stopping container: {container_name}");
docker
Expand Down

0 comments on commit d21365a

Please sign in to comment.