implemented proper error handling, added README
This commit is contained in:
+7
-14
@@ -3,18 +3,15 @@ use std::{
|
||||
process::{Command, ExitStatus},
|
||||
};
|
||||
|
||||
use anyhow::{bail, Context, Result};
|
||||
use directories::BaseDirs;
|
||||
|
||||
fn is_container_running(container_name: &str) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
// Run the `docker ps` command and capture the output
|
||||
fn is_container_running(container_name: &str) -> Result<bool> {
|
||||
let output = Command::new("docker")
|
||||
.args(["ps", "--format", "{{.Names}}"])
|
||||
.output()?;
|
||||
|
||||
// Convert the output to a string
|
||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
||||
|
||||
// Check if the container name is in the list of running containers
|
||||
let running: bool = output_str.lines().any(|line| line.trim() == container_name);
|
||||
|
||||
Ok(running)
|
||||
@@ -24,11 +21,11 @@ pub fn start_docker_compose(
|
||||
service_name: &str,
|
||||
container_path: &str,
|
||||
start: bool,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let base_dir: BaseDirs = BaseDirs::new().unwrap();
|
||||
) -> Result<()> {
|
||||
let base_dir: BaseDirs = BaseDirs::new().context("Could not determine home directory")?;
|
||||
let absolute = base_dir.home_dir().join(container_path);
|
||||
let compose_path = Path::new(&absolute);
|
||||
println!("{}", compose_path.to_str().unwrap());
|
||||
println!("{}", compose_path.to_str().context("Invalid path")?);
|
||||
|
||||
let running: bool = is_container_running(service_name)?;
|
||||
|
||||
@@ -49,9 +46,7 @@ pub fn start_docker_compose(
|
||||
service_name
|
||||
);
|
||||
} else {
|
||||
return Err(
|
||||
format!("Failed to start Docker Compose service '{}'.", service_name).into(),
|
||||
);
|
||||
bail!("Failed to start Docker Compose service '{}'.", service_name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,9 +62,7 @@ pub fn start_docker_compose(
|
||||
service_name
|
||||
);
|
||||
} else {
|
||||
return Err(
|
||||
format!("Failed to stop Docker Compose service '{}'.", service_name).into(),
|
||||
);
|
||||
bail!("Failed to stop Docker Compose service '{}'.", service_name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user