Skip to content

Commit

Permalink
0.3.0 - replace setup_panic with
Browse files Browse the repository at this point in the history
  • Loading branch information
lwlee2608 committed Aug 17, 2023
1 parent aa456c1 commit d129d1e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rusty-docker-compose"
version = "0.2.2"
version = "0.3.0"
edition = "2021"
authors = ["Jason Lee<[email protected]>"]
description = "A Rust library to manage Docker Compose for integration testing, providing control over container lifecycles and logging."
Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
68 changes: 39 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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);
});
})
Expand All @@ -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();
}
}

0 comments on commit d129d1e

Please sign in to comment.