use std::process::Command;
use anyhow::{Context, bail};
use crate::config::{PINNED_TOOLCHAIN, RUSTUP_TOOLCHAIN_ENV, TOOLCHAIN_ENV};
pub fn pinned_toolchain() -> String {
std::env::var(TOOLCHAIN_ENV).unwrap_or_else(|_| PINNED_TOOLCHAIN.to_owned())
}
pub fn rustc_version(toolchain: &str) -> anyhow::Result<String> {
let output = Command::new("rustc")
.env(RUSTUP_TOOLCHAIN_ENV, toolchain)
.arg("--version")
.output()
.context("failed to run `rustc` (is rustup on PATH?)")?;
if !output.status.success() {
bail!(
"toolchain `{toolchain}` is not available ({})",
output.status
);
}
Ok(String::from_utf8(output.stdout)
.context("`rustc --version` produced non-UTF-8 output")?
.trim()
.to_owned())
}