use anyhow::{Context, Result, anyhow};
use std::process::{Command, Stdio};
use super::gh_query::render_gh_credential_query;
use super::{GitCredentialMaterial, GitCredentialQuery};
pub(super) fn should_delegate_to_gh_cli(
query: &GitCredentialQuery,
credentials: &GitCredentialMaterial,
) -> bool {
query
.host
.as_deref()
.or(credentials.host.as_deref())
.unwrap_or_default()
.trim()
.eq_ignore_ascii_case("github.com")
}
pub(super) fn emit_credentials_via_gh_cli(
query: &GitCredentialQuery,
credentials: &GitCredentialMaterial,
) -> Result<()> {
let mut child = Command::new("gh")
.args(["auth", "git-credential", "get"])
.env("GH_TOKEN", &credentials.password)
.stdin(Stdio::piped())
.stdout(Stdio::inherit())
.stderr(Stdio::piped())
.spawn()
.context("Failed to spawn gh auth git-credential")?;
if let Some(mut stdin) = child.stdin.take() {
use std::io::Write;
let payload = render_gh_credential_query(query, credentials);
stdin
.write_all(payload.as_bytes())
.context("Failed to write Git credential request to gh")?;
}
let output = child
.wait_with_output()
.context("Failed to read gh auth git-credential output")?;
if output.status.success() {
return Ok(());
}
Err(anyhow!(
"gh auth git-credential failed: {}",
String::from_utf8_lossy(&output.stderr).trim()
))
}