initial commit
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
use std::{path::Path, process::Command};
|
||||
|
||||
use directories::BaseDirs;
|
||||
|
||||
pub fn is_container_running(container_name: &str) -> bool {
|
||||
// Run the `docker ps` command and capture the output
|
||||
let output = Command::new("docker")
|
||||
.args(["ps", "--format", "{{.Names}}"])
|
||||
.output()
|
||||
.expect("Failed to execute command");
|
||||
|
||||
// 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);
|
||||
|
||||
if running {
|
||||
println!("Container '{}' is running already.", container_name);
|
||||
}
|
||||
running
|
||||
}
|
||||
|
||||
pub fn start_docker_compose(service_name: &str, container_path: &str) {
|
||||
// Run the `docker-compose up` command
|
||||
|
||||
let base_dir: BaseDirs = BaseDirs::new().unwrap();
|
||||
let absolute = base_dir.home_dir().join(container_path);
|
||||
let compose_path = Path::new(&absolute);
|
||||
|
||||
// Check if the file exists before attempting to use it
|
||||
if compose_path.exists() {
|
||||
let status = Command::new("docker-compose")
|
||||
.current_dir(compose_path)
|
||||
.args(["up", "-d"])
|
||||
.status()
|
||||
.expect("Failed to execute command");
|
||||
|
||||
// Check if the command was successful
|
||||
if status.success() {
|
||||
println!(
|
||||
"Docker Compose service '{}' started successfully.",
|
||||
service_name
|
||||
);
|
||||
} else {
|
||||
eprintln!("Failed to start Docker Compose service '{}'.", service_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user