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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod docker;
|
||||
Vendored
+42
@@ -0,0 +1,42 @@
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use directories::BaseDirs;
|
||||
|
||||
const DOCKER_SERVICE: &str = "DOCKER_SERVICE";
|
||||
const DOCKER_DIR: &str = "DOCKER_DIR";
|
||||
|
||||
pub struct DevToolsConf {
|
||||
pub container_service: String,
|
||||
pub container_dir: String,
|
||||
}
|
||||
pub fn load_config() -> Result<DevToolsConf, Box<dyn std::error::Error>> {
|
||||
let base_dir: BaseDirs = match BaseDirs::new() {
|
||||
Some(dirs) => dirs,
|
||||
None => {
|
||||
return Err("No config folder found.".into());
|
||||
}
|
||||
};
|
||||
let config_file_path: PathBuf = base_dir.config_dir().join("rcc/").join("dev");
|
||||
|
||||
match dotenv::from_path(config_file_path.as_path()) {
|
||||
Ok(env) => env,
|
||||
Err(_) => {
|
||||
return Err(format!(
|
||||
"Could not load env file {}",
|
||||
config_file_path.as_path().to_str().unwrap()
|
||||
)
|
||||
.into());
|
||||
}
|
||||
};
|
||||
|
||||
let container_service: String =
|
||||
env::var(DOCKER_SERVICE).map_err(|_| format!("{} missing in env file.", DOCKER_SERVICE))?;
|
||||
let container_dir: String =
|
||||
env::var("DOCKER_DIR").map_err(|_| format!("{} missing in env file.", DOCKER_DIR))?;
|
||||
|
||||
Ok(DevToolsConf {
|
||||
container_service,
|
||||
container_dir,
|
||||
})
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
pub mod config;
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
use container::docker::{is_container_running, start_docker_compose};
|
||||
use env::config::{load_config, DevToolsConf};
|
||||
|
||||
mod container;
|
||||
mod env;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config: DevToolsConf = load_config()?;
|
||||
let is_running: bool = is_container_running(&config.container_service);
|
||||
if !is_running {
|
||||
start_docker_compose(&config.container_service, &config.container_dir);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user