Skip to content

Commit

Permalink
Update Name::new to just take in one arg
Browse files Browse the repository at this point in the history
Co-authored-by: Willem Wyndham <[email protected]>
  • Loading branch information
elizabethengelman and willemneal committed Jul 18, 2024
1 parent 6fbb3f3 commit ec3d39f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 75 deletions.
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/network/container/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Cmd {

impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
let container_name = Name::new(Some(self.name.clone()), None).get_internal_container_name();
let container_name = Name::new(self.name.clone()).get_internal_container_name();
let docker = self.container_args.connect_to_docker().await?;
let logs_stream = &mut docker.logs(
&container_name,
Expand Down
78 changes: 6 additions & 72 deletions cmd/soroban-cli/src/commands/network/container/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,18 @@ impl fmt::Display for Network {
}
}

pub struct Name(Option<String>, Option<Network>);
pub struct Name(String);
impl Name {
pub fn new(name: Option<String>, network: Option<Network>) -> Self {
Self(name, network)
pub fn new(name: String) -> Self {
Self(name)
}

pub fn get_internal_container_name(&self) -> String {
match (&self.0, &self.1) {
(Some(name), _) => format!("stellar-{}", name),
(None, Some(network)) => format!("stellar-{}", network),
(None, None) => panic!("Container name and/or network are required."),
}
format!("stellar-{}", self.0)
}

pub fn get_external_container_name(&self) -> String {
match (&self.0, &self.1) {
(Some(name), _) => name.to_string(),
(None, Some(network)) => network.to_string(),
(None, None) => panic!("Container name and/or network are required."),
}
self.0.to_string()
}
}

Expand Down Expand Up @@ -186,62 +178,4 @@ async fn check_docker_connection(docker: &Docker) -> Result<(), bollard::errors:
Err(err)
}
}
}

#[cfg(test)]
mod tests {
use super::*;

const NETWORK: Network = Network::Local;
const CONTAINER_NAME: &str = "test-name";

#[test]
fn test_get_internal_container_name_with_both_args() {
let name = Name::new(Some(CONTAINER_NAME.to_string()), Some(NETWORK));
assert_eq!(name.get_internal_container_name(), "stellar-test-name");
}

#[test]
fn test_get_internal_container_name_with_network() {
let name = Name::new(None, Some(NETWORK));
assert_eq!(name.get_internal_container_name(), "stellar-local");
}

#[test]
fn test_get_internal_container_name_with_name() {
// in practice, this would fail clap validation and we would never get here, but testing anyway
let name = Name::new(Some(CONTAINER_NAME.to_string()), None);
assert_eq!(name.get_internal_container_name(), "stellar-test-name");
}

#[test]
#[should_panic]
fn test_get_internal_container_name_with_neither_args() {
Name::new(None, None).get_internal_container_name();
}

#[test]
fn test_get_external_container_name_with_both_args() {
let name = Name::new(Some(CONTAINER_NAME.to_string()), Some(NETWORK));
assert_eq!(name.get_external_container_name(), "test-name");
}

#[test]
fn test_get_external_container_name_with_network() {
let name = Name::new(None, Some(NETWORK));
assert_eq!(name.get_external_container_name(), "local");
}

#[test]
fn test_get_external_container_name_with_name() {
// in practice, this would fail clap validation and we would never get here, but testing anyway
let name = Name::new(Some(CONTAINER_NAME.to_string()), None);
assert_eq!(name.get_external_container_name(), "test-name");
}

#[test]
#[should_panic]
fn test_get_external_container_name_with_neither_args() {
Name::new(None, None).get_external_container_name();
}
}
}
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/network/container/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl Cmd {
}

fn container_name(&self) -> Name {
Name::new(self.name.clone(), Some(self.network))
Name::new(self.name.clone().unwrap_or(self.network.to_string()))
}

fn print_log_message(&self) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/network/container/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Cmd {

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

0 comments on commit ec3d39f

Please sign in to comment.