Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Dec 6, 2023
1 parent 46fa951 commit 30f3a2f
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions cmd/soroban-cli/src/commands/network/stop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::process::Command;

#[derive(thiserror::Error, Debug)]
pub enum Error {}
pub enum Error {
#[error("Failed to execute docker command: {error}")]
CommandError { error: std::io::Error },
#[error("Failed to find docker container: {error}")]
ContainerNotFoundErr { error: String },
}

const CONTAINER_NAME: &str = "stellar";

Expand All @@ -18,27 +23,29 @@ impl Cmd {
.clone()
.unwrap_or(String::from(CONTAINER_NAME));
let docker_command = build_docker_command(&container);
run_docker_command(&docker_command);
Ok(())
run_docker_command(&docker_command)
}
}

fn build_docker_command(container_name: &str) -> String {
format!("docker stop {container_name}")
}

fn run_docker_command(docker_command: &str) {
fn run_docker_command(docker_command: &str) -> Result<(), Error> {
println!("Running docker command: `{docker_command}`");
let output = Command::new("sh")
.args(["-c", &docker_command])
.output()
.expect("Failed to execute command");

if output.status.success() {
let result = String::from_utf8_lossy(&output.stdout);
println!("Docker image stopped: {result}");
} else {
let result = String::from_utf8_lossy(&output.stderr);
eprintln!("Error executing Docker command: {result}");
}
.map_err(|err| Error::CommandError { error: err })
.and_then(|output| {
if output.status.success() {
let result = String::from_utf8_lossy(&output.stdout);
println!("Docker image stopped: {result}");
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
Err(Error::ContainerNotFoundErr { error: stderr })
}
});
output
}

0 comments on commit 30f3a2f

Please sign in to comment.