use anyhow::Error;
use std::process::{Command, ExitStatus};
pub fn channel(exe: &std::path::Path, repo: &std::path::Path) -> Result<String, Error> {
let mut cmd = Command::new(exe);
let cmd = cmd
.arg("channel")
.arg("--no-prompt")
.arg("--repository")
.arg(repo);
let output = cmd.output()?;
check_exit_status(cmd, output.status)?;
Ok(String::from_utf8(output.stdout)?)
}
fn check_exit_status(cmd: &Command, exit_status: ExitStatus) -> Result<(), Error> {
if exit_status.success() {
Ok(())
} else {
Err(Error::from(ExecutionError {
command: format!("{:?}", cmd),
exit_code: exit_status.code(),
}))
}
}
/// Execution of a [`Command`] finished with an unsuccessful error code.
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[error("Received an exit code of {} from {}", exit_code.unwrap_or(1), command)]
pub struct ExecutionError {
pub command: String,
pub exit_code: Option<i32>,
}