feat: docker: create, read logs and delete containers
This commit is contained in:
parent
7ac5019dc2
commit
bfa92145a9
1 changed files with 42 additions and 0 deletions
42
src/docker.rs
Normal file
42
src/docker.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use std::process::Command;
|
||||
|
||||
pub struct Docker;
|
||||
|
||||
impl Docker {
|
||||
pub fn version() -> String {
|
||||
let version = Command::new("docker")
|
||||
.arg("--version")
|
||||
.output()
|
||||
.expect("unable to obtain Docker version");
|
||||
let x = String::from_utf8(version.stdout).unwrap();
|
||||
let x: Vec<&str> = x.split("Docker version ").collect();
|
||||
x.get(1).unwrap().to_string()
|
||||
}
|
||||
|
||||
pub fn run_container(name: &str, img: &str) {
|
||||
Command::new("docker")
|
||||
.args(["run", "--name", name, img])
|
||||
.spawn()
|
||||
.expect("unable to obtain Docker version");
|
||||
}
|
||||
|
||||
pub fn get_logs(name: &str) -> String {
|
||||
let output = Command::new("docker")
|
||||
.args(["logs", name])
|
||||
.output()
|
||||
.expect("unable to get logs");
|
||||
String::from_utf8(output.stdout).unwrap()
|
||||
}
|
||||
|
||||
pub fn rm_container(name: &str, force: bool) {
|
||||
let args = if force {
|
||||
vec!["rm", name]
|
||||
} else {
|
||||
vec!["rm", "--force", name]
|
||||
};
|
||||
Command::new("docker")
|
||||
.args(args)
|
||||
.spawn()
|
||||
.expect(&format!("unable to remove docker container {name}"));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue