diff --git a/Cargo.toml b/Cargo.toml index 8029eea..7121a58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rusty-docker-compose" -version = "0.2.2" +version = "0.3.0" edition = "2021" authors = ["Jason Lee"] description = "A Rust library to manage Docker Compose for integration testing, providing control over container lifecycles and logging." diff --git a/README.md b/README.md index 02e5dff..fc8301b 100644 --- a/README.md +++ b/README.md @@ -14,23 +14,35 @@ Add `rusty-docker-compose` as a dependency in your `Cargo.toml` file: ```toml [dependencies] -rusty-docker-compose = "0.2.2" +rusty-docker-compose = "0.3.0" ``` ## Usage Here's a basic example of how to use rusty-docker-compose: +```rust +use rusty_docker_compose::DockerComposeCmd; + +let docker_compose_cmd = DockerComposeCmd::new( + "tests/docker-compose.yaml", + "target/docker_logs", +); + +docker_compose_cmd.up(); +// ... run your tests ... +docker_compose_cmd.down(); +``` + +Alternatively, you can use `DockerCompose` to automatically start up and tear down the docker compose when it goes out of scope: ```rust use rusty_docker_compose::DockerCompose; -let docker_compose = DockerCompose::new( +let _docker_compose = DockerCompose::new( "tests/docker-compose.yaml", "target/docker_logs", ); -docker_compose.up(); // ... run your tests ... -docker_compose.down(); ``` For more detailed examples, please refer to the documentation. \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 64bc92e..8f175f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,30 +4,20 @@ use std::process::{Command, Stdio}; use std::thread; #[derive(Clone)] -pub struct DockerCompose { +pub struct DockerComposeCmd { file: String, logs_dir: String, } -impl DockerCompose { - pub fn new(file: &str, logs_dir: &str) -> DockerCompose { - DockerCompose { +impl DockerComposeCmd { + pub fn new(file: &str, logs_dir: &str) -> DockerComposeCmd { + DockerComposeCmd { file: file.to_string(), - logs_dir: logs_dir.to_string(), + logs_dir: logs_dir.to_string(), } } - fn setup_shutdown(&self) { - let self_clone = self.clone(); - let default_hook = std::panic::take_hook(); - std::panic::set_hook(Box::new(move |panic_info| { - default_hook(panic_info); - self_clone.down(); - })); - } - pub fn up(&self) { - self.setup_shutdown(); let output = Command::new("docker-compose") .arg("-f") .arg(self.file.clone()) @@ -63,20 +53,21 @@ impl DockerCompose { let file_path = std::path::PathBuf::from(file_name); let docker_compose_file = self.file.clone(); thread::spawn(move || { - let follow_container_log = |container: String, file_path: std::path::PathBuf| { - let file = File::create(file_path).unwrap(); - let _ = Command::new("docker-compose") - .arg("-f") - .arg(docker_compose_file) - .arg("logs") - .arg("--follow") - .arg("--no-log-prefix") - .arg(&container) - .stdout(Stdio::from(file)) - .spawn() - .unwrap(); - }; - + let follow_container_log = + |container: String, file_path: std::path::PathBuf| { + let file = File::create(file_path).unwrap(); + let _ = Command::new("docker-compose") + .arg("-f") + .arg(docker_compose_file) + .arg("logs") + .arg("--follow") + .arg("--no-log-prefix") + .arg(&container) + .stdout(Stdio::from(file)) + .spawn() + .unwrap(); + }; + follow_container_log(container, file_path); }); }) @@ -93,4 +84,23 @@ impl DockerCompose { .output() .expect("Failed to execute command"); } +} + + +pub struct DockerCompose { + cmd: DockerComposeCmd, +} + +impl DockerCompose { + pub fn new(file: &str, logs_dir: &str) -> DockerCompose { + let cmd = DockerComposeCmd::new(file, logs_dir); + cmd.up(); + DockerCompose { cmd } + } +} + +impl Drop for DockerCompose { + fn drop(&mut self) { + self.cmd.down(); + } } \ No newline at end of file