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

@@ -1,25 +1,31 @@
use super::{docker_compose, DockerCommand};
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use std::{
fs::File,
path::{Path, PathBuf},
process::Stdio,
};
use super::docker_compose;
use crate::command_builder::CommandBuilder;
fn get_containers() -> Result<[String; 2]> {
// get db container
// FIX: we assume we are running db in service named "postgresbd"
let db_container = DockerCommand::docker_compose()
let db_container = CommandBuilder::docker_compose()
.args("ps -q postgresdb")
.stdout()?
.exec_get_stdout()?
.trim()
.to_string();
let no_result = db_container.is_empty();
if no_result {
return Err(anyhow!("no container"));
}
// get all containers and filter out db container
let app_containers = DockerCommand::docker_compose()
let app_containers = CommandBuilder::docker_compose()
.args("ps -q")
.stdout()?
.exec_get_stdout()?
.split_whitespace()
.filter(|x| x != &db_container)
.collect::<Vec<&str>>()
@@ -46,7 +52,7 @@ pub fn import(file: &Path) -> Result<()> {
format!("exec {db_container} pg_restore -U db --dbname=db /tmp/dbdump"),
];
for command in commands {
DockerCommand::docker().args(&command).spawn_wait()?;
CommandBuilder::docker().args(&command).exec()?;
}
println!("restarting containers");
@@ -65,9 +71,9 @@ pub fn dump(file: &PathBuf) -> Result<()> {
let stdout = Stdio::from(file);
let command = format!("exec {db_container} pg_dump -U db --format=c db");
DockerCommand::docker()
CommandBuilder::docker()
.args(&command)
.write_stdout(stdout)?;
.exec_redirect_stdout(stdout)?;
Ok(())
}