pray-core 1.22.0

Core library for Prayfile, the package manager for the language placed before inference
Documentation
use crate::git_process::git_command;
use crate::{PrayError, PrayResult};
use std::path::Path;

pub(crate) fn run_git_success(root: &Path, arguments: &[&str]) -> PrayResult<()> {
    let output = run_git_command(root, arguments)?;
    if output.status.success() {
        Ok(())
    } else {
        Err(command_error("git", output))
    }
}

pub(crate) fn run_git_command(root: &Path, arguments: &[&str]) -> PrayResult<std::process::Output> {
    run_git_command_with_env(root, arguments, &[])
}

pub(crate) fn run_git_command_with_env(
    root: &Path,
    arguments: &[&str],
    extra_env: &[(&str, &str)],
) -> PrayResult<std::process::Output> {
    let mut command = git_command(root);
    command
        .env("GIT_TERMINAL_PROMPT", "0")
        .stdin(std::process::Stdio::null())
        .args(["-c", "protocol.file.allow=always"])
        .args(arguments);
    for (key, value) in extra_env {
        command.env(key, value);
    }
    command
        .output()
        .map_err(|error| PrayError::Unsupported(format!("failed to run `git`: {error}")))
}

pub(crate) fn command_error(program: &str, output: std::process::Output) -> PrayError {
    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
    let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
    let mut message = format!("{program} failed with status {}", output.status);
    if !stderr.is_empty() {
        message.push_str(&format!(": {stderr}"));
    } else if !stdout.is_empty() {
        message.push_str(&format!(": {stdout}"));
    }
    PrayError::Resolution(message)
}