Files
zesdex/src/tool/git_cred.rs
T
asepharyana 29a9fae3f6 ci: add GitHub Actions workflows with semantic-release auto-versioning
chore: fix all 702 clippy warnings across codebase
- auto-fix 475 via cargo clippy --fix
- fix remaining 227 manually: uninlined_format_args, redundant_closure, match_same_arms,
  underscore_binding, format_push_string, items_after_statements, needless_pass_by_value,
  clone_on_copy, case_sensitive_extension, single_match/let-else, write_with_newline,
  and other clippy lints
2026-07-13 08:12:12 +07:00

62 lines
2.2 KiB
Rust

//! Tool wrapper around `git credential` for store/get/erase operations.
use serde_json::{json, Value};
use anyhow::{Result, anyhow};
use std::process::Command;
use super::Tool;
use super::ToolCtx;
/// Tool that shells out to `git credential <op>` to store, retrieve, or erase credentials.
pub struct GitCred;
impl Tool for GitCred {
fn name(&self) -> &'static str {
"git_cred"
}
fn description(&self) -> &'static str {
"Interact with git credential helper (store, get, erase credentials)"
}
fn parameters(&self) -> Value {
json!({
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": ["store", "get", "erase"],
"description": "Git credential operation"
}
},
"required": ["operation"]
})
}
/// Run `git credential <operation>`, forwarding stdin-less invocation to the git binary.
///
/// Flow: extract `operation` arg → spawn `git credential <operation>` → capture output.
///
/// Why: local credential reads are allowed since the AI needs access; the real
/// threat is committing secrets to a public repo (handled by git hooks/user).
///
/// Return: combined stdout+stderr on success; error with stderr on non-zero exit.
fn run(&self, _ctx: &ToolCtx, args: &Value) -> Result<String> {
let operation = args.get("operation")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow!("missing required argument: operation"))?;
let output = Command::new("git")
.arg("credential")
.arg(operation)
.output()
.map_err(|e| anyhow!("git credential failed: {e}"))?;
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
Ok(format!("{stdout}{stderr}"))
} else {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
anyhow::bail!("git credential '{}' failed: {}", operation, stderr.trim())
}
}
}