From 93be1c835278acdd68ffca06a29621ba1ea2dc1e Mon Sep 17 00:00:00 2001 From: mace Date: Sat, 20 Jan 2024 12:01:41 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + Cargo.lock | 237 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 ++ src/container/docker.rs | 49 +++++++++ src/container/mod.rs | 1 + src/env/config.rs | 42 +++++++ src/env/mod.rs | 1 + src/main.rs | 15 +++ 8 files changed, 356 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/container/docker.rs create mode 100644 src/container/mod.rs create mode 100644 src/env/config.rs create mode 100644 src/env/mod.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..616dee2 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,237 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "dev_tools" +version = "0.1.0" +dependencies = [ + "directories", + "dotenv", +] + +[[package]] +name = "directories" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys", +] + +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + +[[package]] +name = "getrandom" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" + +[[package]] +name = "libredox" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8" +dependencies = [ + "bitflags 2.4.2", + "libc", + "redox_syscall", +] + +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + +[[package]] +name = "proc-macro2" +version = "1.0.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "redox_users" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + +[[package]] +name = "syn" +version = "2.0.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cb17e2e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "dev_tools" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +directories = "5.0.1" +dotenv = "0.15.0" diff --git a/src/container/docker.rs b/src/container/docker.rs new file mode 100644 index 0000000..15ac5f6 --- /dev/null +++ b/src/container/docker.rs @@ -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); + } + } +} diff --git a/src/container/mod.rs b/src/container/mod.rs new file mode 100644 index 0000000..18c5028 --- /dev/null +++ b/src/container/mod.rs @@ -0,0 +1 @@ +pub mod docker; diff --git a/src/env/config.rs b/src/env/config.rs new file mode 100644 index 0000000..52eec2c --- /dev/null +++ b/src/env/config.rs @@ -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> { + 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, + }) +} diff --git a/src/env/mod.rs b/src/env/mod.rs new file mode 100644 index 0000000..ef68c36 --- /dev/null +++ b/src/env/mod.rs @@ -0,0 +1 @@ +pub mod config; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d3888f5 --- /dev/null +++ b/src/main.rs @@ -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> { + 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(()) +}