From 68dbef7b7f59ef864950ccc148799b30591f532e Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Wed, 28 Feb 2024 15:37:09 -0500 Subject: [PATCH] Refactor retrying with docker desktop socket for unix only --- .../src/commands/network/shared.rs | 53 ++++++++++++------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/cmd/soroban-cli/src/commands/network/shared.rs b/cmd/soroban-cli/src/commands/network/shared.rs index fdbccda72..3fe11d97c 100644 --- a/cmd/soroban-cli/src/commands/network/shared.rs +++ b/cmd/soroban-cli/src/commands/network/shared.rs @@ -59,10 +59,12 @@ pub async fn connect_to_docker(docker_host: &Option) -> Result { Docker::connect_with_http_defaults() @@ -78,34 +80,49 @@ pub async fn connect_to_docker(docker_host: &Option) -> Result { return Err(Error::UnsupportedURISchemeError { uri: host.to_string(), - } - .into()); + }); } }?; match check_docker_connection(&connection).await { - Ok(_) => return Ok(connection), - // If we aren't able to connect with the defaults, or with the provided docker_host, we try with the default docker desktop socket since that is a common use case for developers - Err(_) => { - let default_docker_desktop_host = - format!("{}/.docker/run/docker.sock", home_dir().unwrap().display()); - println!("Failed to connect to DOCKER_HOST: {host}.\nTrying to connect to the default Docker Desktop socket at {default_docker_desktop_host}."); - let connection = Docker::connect_with_unix( - &default_docker_desktop_host, - DEFAULT_TIMEOUT, - API_DEFAULT_VERSION, - )?; - match check_docker_connection(&connection).await { - Ok(_) => return Ok(connection), - Err(err) => return Err(err)?, + Ok(()) => Ok(connection), + // If we aren't able to connect with the defaults, or with the provided docker_host + // try to connect with the default docker desktop socket since that is a common use case for devs + Err(_e) => { + // if on unix, try to connect to the default docker desktop socket + #[cfg(unix)] + { + let docker_desktop_connection = try_docker_desktop_socket(&host)?; + match check_docker_connection(&docker_desktop_connection).await { + Ok(()) => Ok(docker_desktop_connection), + Err(err) => Err(err)?, + } + } + + #[cfg(windows)] + { + Err(_e)?; } } } } +#[cfg(unix)] +fn try_docker_desktop_socket(host: &str) -> Result { + let default_docker_desktop_host = + format!("{}/.docker/run/docker.sock", home_dir().unwrap().display()); + println!("Failed to connect to DOCKER_HOST: {host}.\nTrying to connect to the default Docker Desktop socket at {default_docker_desktop_host}."); + + Docker::connect_with_unix( + &default_docker_desktop_host, + DEFAULT_TIMEOUT, + API_DEFAULT_VERSION, + ) +} + // When bollard is not able to connect to the docker daemon, it returns a generic ConnectionRefused error // This method attempts to connect to the docker daemon and returns a more specific error message -pub async fn check_docker_connection(docker: &Docker) -> Result<(), bollard::errors::Error> { +async fn check_docker_connection(docker: &Docker) -> Result<(), bollard::errors::Error> { // This is a bit hacky, but the `client_addr` field is not directly accessible from the `Docker` struct, but we can access it from the debug string representation of the `Docker` struct let docker_debug_string = format!("{docker:#?}"); let start_of_client_addr = docker_debug_string.find("client_addr: ").unwrap();