use crate::fs::TempFs;
use crate::INCLUDE_FRONTEND;
use std::env;
use std::path::Path;
use std::process::Command;
pub(crate) fn build_backend(file_manager: &TempFs) {
let frontend: bool = match env::var(INCLUDE_FRONTEND) {
Ok(val) => val == "true",
Err(_) => false,
};
let mut docker_args = vec!["-f", &file_manager.backend_compose];
if frontend {
docker_args.push("-f");
docker_args.push(&file_manager.frontend_compose);
}
docker_args.push("build");
Command::new("docker-compose")
.args(docker_args)
.status()
.expect("Failed to execute docker-compose command.");
}
pub(crate) fn upgrade(file_manager: &TempFs) {
let frontend: bool = match env::var(INCLUDE_FRONTEND) {
Ok(val) => val == "true",
Err(_) => false,
};
let mut docker_args = vec!["-f", &file_manager.backend_compose];
if frontend {
docker_args.push("-f");
docker_args.push(&file_manager.frontend_compose);
}
let mut down_args = docker_args.clone();
down_args.push("down");
let mut pull_args = docker_args.clone();
pull_args.push("pull");
Command::new("docker-compose")
.args(down_args)
.status()
.expect("Failed to execute command - compose down command");
Command::new("docker-compose")
.args(pull_args)
.status()
.expect("Failed to execute command - compose build --pull");
}
pub(crate) fn start_service(frontend: &bool, file_manager: &TempFs) {
let mut cmd = Command::new("docker-compose");
if *frontend {
cmd.args([
"-f",
&file_manager.backend_compose,
"-f",
&file_manager.frontend_compose,
"up",
"-d",
])
.status()
.expect("Failed to execute command");
} else {
cmd.args(["-f", &file_manager.backend_compose, "up", "-d"])
.status()
.expect("Failed to execute command");
}
}
pub(crate) fn stop_service(frontend: &bool, file_manager: &TempFs) {
if Path::new(&file_manager.backend_compose).exists() {
let mut cmd = Command::new("docker-compose");
if *frontend {
cmd.args([
"-f",
&file_manager.backend_compose,
"-f",
&file_manager.frontend_compose,
"down",
])
.status()
.expect("Failed to execute command");
} else {
cmd.args(["-f", &file_manager.backend_compose, "down"])
.status()
.expect("Failed to execute command");
}
}
}