feat: docker: create, read logs and delete containers

This commit is contained in:
Aravinth Manivannan 2023-09-05 18:42:43 +05:30
parent 7ac5019dc2
commit bfa92145a9
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

42
src/docker.rs Normal file
View 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}"));
}
}