feat: custom command builder

Implemented custom command builder with nicer api suited for my needs.
Also returned error if there are no containers in
`scripts::postgres::get_containers`.
This commit is contained in:
2023-06-02 17:18:25 +02:00
parent e3306c494d
commit 316b37cd05
7 changed files with 132 additions and 87 deletions

View File

@@ -2,63 +2,3 @@ pub mod django;
pub mod docker;
pub mod docker_compose;
pub mod postgres;
use std::{
ffi::OsStr,
fs::{File, OpenOptions},
path::PathBuf,
process::{Command, Stdio},
};
use anyhow::{Context, Result};
struct DockerCommand {
command: Command,
}
impl DockerCommand {
fn new<T>(program: T) -> Self
where
T: AsRef<OsStr>,
{
DockerCommand {
command: Command::new(program),
}
}
fn docker() -> Self {
Self::new("docker")
}
fn docker_compose() -> Self {
Self::new("docker").args("compose -f docker/local/docker-compose.yaml")
}
fn args(mut self, args: &str) -> Self {
self.command.args(args.split_whitespace());
self
}
fn stdout(mut self) -> Result<String> {
Ok(String::from_utf8(self.command.output()?.stdout)?)
}
fn spawn_wait(mut self) -> Result<()> {
self.command.spawn()?.wait()?;
Ok(())
}
fn write_stdout(mut self, stdio: Stdio) -> Result<()> {
self.command.stdout(stdio);
self.spawn_wait()?;
Ok(())
}
}
fn safe_create_file(path: PathBuf) -> Result<File, std::io::Error> {
OpenOptions::new().write(true).create_new(true).open(path)
}
fn create_file(path: PathBuf) -> Result<File, std::io::Error> {
OpenOptions::new().write(true).create(true).open(path)
}