use color_eyre::eyre;
use crate::{
toolchain::{Installation, Toolchain, ToolchainError},
utils::{run_command, which},
};
#[derive(Debug, Default)]
pub struct Brew {}
impl Brew {
pub async fn install(&self, name: &str) -> eyre::Result<()> {
run_command("brew", ["install", name]).await?;
Ok(())
}
pub async fn install_cask(&self, cask: &str) -> eyre::Result<()> {
run_command("brew", ["install", "--cask", cask]).await?;
Ok(())
}
}
impl Toolchain for Brew {
type Installation = BrewInstallation;
async fn check(&self) -> Result<(), crate::toolchain::ToolchainError<Self::Installation>> {
if which("brew").await.is_ok() {
Ok(())
} else if cfg!(target_os = "macos") {
Err(ToolchainError::fixable(BrewInstallation))
} else {
Err(ToolchainError::unfixable(
"Homebrew is only supported on macOS",
"Why did you try to use Homebrew on a non-macOS system?",
))
}
}
}
#[derive(Debug)]
pub struct BrewInstallation;
impl Installation for BrewInstallation {
type Error = eyre::Report;
async fn install(&self) -> Result<(), Self::Error> {
run_command(
"sh",
[
"-c",
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)",
],
)
.await?;
Ok(())
}
}