use std::{
io,
process::{Command, Stdio},
};
use crate::config::Config;
pub struct Buck2Command {
command: Command,
}
impl Buck2Command {
pub fn new() -> Self {
let config = Config::load();
let mut command = Command::new(&config.buck2_binary);
command.stdout(Stdio::inherit()).stderr(Stdio::inherit());
Self { command }
}
pub fn subcommand(mut self, subcmd: &str) -> Self {
self.command.arg(subcmd);
self
}
pub fn arg<S: AsRef<str>>(mut self, arg: S) -> Self {
self.command.arg(arg.as_ref());
self
}
pub fn verbosity(mut self, level: u8) -> Self {
match level {
1 => self.command.arg("-v=3"),
2 => self.command.arg("-v=4"),
_ => &mut self.command,
};
self
}
pub fn status(mut self) -> io::Result<std::process::ExitStatus> {
self.command.status()
}
pub fn output(mut self) -> io::Result<std::process::Output> {
self.command
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
}
pub fn execute(self) -> io::Result<()> {
let status = self.status()?;
if !status.success() {
return Err(io::Error::other("Buck2 command failed"));
}
Ok(())
}
pub fn build() -> Self {
Self::new().subcommand("build")
}
pub fn test() -> Self {
Self::new().subcommand("test")
}
pub fn init() -> Self {
Self::new().subcommand("init")
}
pub fn clean() -> Self {
Self::new().subcommand("clean")
}
pub fn root() -> Self {
Self::new().subcommand("root")
}
pub fn targets() -> Self {
Self::new().subcommand("targets")
}
pub fn uquery() -> Self {
Self::new().subcommand("uquery")
}
}
impl Default for Buck2Command {
fn default() -> Self {
Self::new()
}
}