use anyhow::{bail, Context};
use mz_ore::option::OptionExt;
use tokio::process::Command;
use crate::action::{ControlFlow, State};
use crate::parser::BuiltinCommand;
use crate::util::text;
pub async fn run_execute(
mut cmd: BuiltinCommand,
state: &State,
) -> Result<ControlFlow, anyhow::Error> {
let command = cmd.args.string("command")?;
cmd.args.done()?;
let expected_output = cmd.input.join("\n");
let output = Command::new("psql")
.args([
"--no-psqlrc",
"--pset",
"footer=off",
"--command",
&command,
&format!(
"postgres://{}@{}",
state.materialize.user, state.materialize.sql_addr
),
])
.output()
.await
.context("execution of `psql` failed")?;
if !output.status.success() {
bail!(
"psql reported failure with exit code {}: {}",
output.status.code().display_or("unknown"),
String::from_utf8_lossy(&output.stderr),
);
}
let stdout = text::trim_trailing_space(&String::from_utf8_lossy(&output.stdout));
if expected_output != stdout {
text::print_diff(&expected_output, &*stdout);
bail!("psql returned unexpected output (diff above)");
}
Ok(ControlFlow::Continue)
}