refactor(cli): sweep 15 bail("git X") sites to use git_run with context labels

This commit is contained in:
adlee-was-taken
2026-05-08 22:10:25 -04:00
parent f3cdbed7b6
commit 97c8f994e1
6 changed files with 50 additions and 46 deletions

View File

@@ -282,8 +282,7 @@ pub(super) fn cmd_backup_restore(input: PathBuf, target: PathBuf) -> Result<()>
} }
} else { } else {
// No history bundled — start a fresh git repo. // No history bundled — start a fresh git repo.
let status = crate::helpers::git_command(&target, &["init"]).status()?; crate::helpers::git_run(&target, &["init"], "backup restore: git init")?;
if !status.success() { anyhow::bail!("git init failed"); }
// .gitignore — exclude reference image if present. // .gitignore — exclude reference image if present.
if target.join("reference.jpg").exists() { if target.join("reference.jpg").exists() {

View File

@@ -104,20 +104,17 @@ pub fn cmd_device(action: DeviceAction) -> Result<()> {
fs::write(&devices_path, serde_json::to_string_pretty(&devices)?)?; fs::write(&devices_path, serde_json::to_string_pretty(&devices)?)?;
// Commit the update. // Commit the update.
let status = crate::helpers::git_command( crate::helpers::git_run(
&root, &root,
&["add", ".relicario/devices.json"], &["add", ".relicario/devices.json"],
) &format!("device register \"{name}\": git add .relicario/devices.json"),
.status()?; )?;
if !status.success() {
anyhow::bail!("git add .relicario/devices.json failed");
}
let msg = format!("device: register {}", name); let msg = format!("device: register {}", name);
let status = crate::helpers::git_command(&root, &["commit", "-m", &msg]) crate::helpers::git_run(
.status()?; &root,
if !status.success() { &["commit", "-m", &msg],
anyhow::bail!("git commit failed"); &format!("device register \"{name}\": git commit"),
} )?;
eprintln!("Device '{}' registered.", name); eprintln!("Device '{}' registered.", name);
eprintln!("Signing public key:"); eprintln!("Signing public key:");
@@ -209,16 +206,17 @@ pub fn cmd_device(action: DeviceAction) -> Result<()> {
".relicario/devices.json", ".relicario/devices.json",
".relicario/revoked.json", ".relicario/revoked.json",
]; ];
let status = crate::helpers::git_command(&root, &add_args).status()?; crate::helpers::git_run(
if !status.success() { &root,
anyhow::bail!("git add failed"); &add_args,
} &format!("device revoke \"{name}\": git add devices.json + revoked.json"),
)?;
let msg = format!("device: revoke {}", name); let msg = format!("device: revoke {}", name);
let status = crate::helpers::git_command(&root, &["commit", "-m", &msg]) crate::helpers::git_run(
.status()?; &root,
if !status.success() { &["commit", "-m", &msg],
anyhow::bail!("git commit failed"); &format!("device revoke \"{name}\": git commit"),
} )?;
eprintln!("Device '{}' revoked.", name); eprintln!("Device '{}' revoked.", name);
eprintln!("Revoked signing key: {}", device.public_key); eprintln!("Revoked signing key: {}", device.public_key);

View File

@@ -90,16 +90,16 @@ pub fn cmd_init(image: PathBuf, output: PathBuf) -> Result<()> {
fs::write(root.join(".gitignore"), gitignore)?; fs::write(root.join(".gitignore"), gitignore)?;
// git init + initial commit via hardened wrapper. // git init + initial commit via hardened wrapper.
let status = crate::helpers::git_command(&root, &["init"]).status()?; crate::helpers::git_run(&root, &["init"], "init: git init")?;
if !status.success() { anyhow::bail!("git init failed"); }
let _ = crate::helpers::git_command(&root, &[ let _ = crate::helpers::git_command(&root, &[
"add", ".gitignore", ".relicario/params.json", "add", ".gitignore", ".relicario/params.json",
".relicario/salt", "manifest.enc", "settings.enc", ".relicario/salt", "manifest.enc", "settings.enc",
]).status()?; ]).status()?;
let status = crate::helpers::git_command(&root, &[ crate::helpers::git_run(
"commit", "-m", "init: new Relicario vault (format v2)", &root,
]).status()?; &["commit", "-m", "init: new Relicario vault (format v2)"],
if !status.success() { anyhow::bail!("git commit failed"); } "init: git commit",
)?;
eprintln!("Vault initialized at {}", root.display()); eprintln!("Vault initialized at {}", root.display());
eprintln!("Reference image: {}", output.display()); eprintln!("Reference image: {}", output.display());

View File

@@ -32,10 +32,12 @@ pub(crate) fn commit_paths(
) -> Result<()> { ) -> Result<()> {
let mut args: Vec<&str> = vec!["add"]; let mut args: Vec<&str> = vec!["add"];
args.extend_from_slice(paths); args.extend_from_slice(paths);
let status = crate::helpers::git_command(vault.root(), &args).status()?; crate::helpers::git_run(vault.root(), &args, &format!("commit \"{message}\": git add"))?;
if !status.success() { anyhow::bail!("git add failed"); } crate::helpers::git_run(
let status = crate::helpers::git_command(vault.root(), &["commit", "-m", message]).status()?; vault.root(),
if !status.success() { anyhow::bail!("git commit failed"); } &["commit", "-m", message],
&format!("commit \"{message}\": git commit"),
)?;
Ok(()) Ok(())
} }

View File

@@ -4,10 +4,8 @@ use anyhow::Result;
pub fn cmd_sync() -> Result<()> { pub fn cmd_sync() -> Result<()> {
let root = crate::helpers::vault_dir()?; let root = crate::helpers::vault_dir()?;
let pull = crate::helpers::git_command(&root, &["pull", "--rebase"]).status()?; crate::helpers::git_run(&root, &["pull", "--rebase"], "sync: git pull --rebase")?;
if !pull.success() { anyhow::bail!("git pull --rebase failed"); } crate::helpers::git_run(&root, &["push"], "sync: git push")?;
let push = crate::helpers::git_command(&root, &["push"]).status()?;
if !push.success() { anyhow::bail!("git push failed"); }
eprintln!("Sync complete."); eprintln!("Sync complete.");
Ok(()) Ok(())
} }

View File

@@ -78,11 +78,13 @@ pub fn cmd_purge(query: String) -> Result<()> {
vault.save_manifest(&manifest)?; vault.save_manifest(&manifest)?;
crate::refresh_groups_cache(vault.root(), &manifest); crate::refresh_groups_cache(vault.root(), &manifest);
let status = crate::helpers::git_command(vault.root(), &["add", "manifest.enc"]).status()?; let purge_ctx = format!("purge \"{}\" ({})", title, id.as_str());
if !status.success() { anyhow::bail!("git add manifest.enc failed"); } crate::helpers::git_run(vault.root(), &["add", "manifest.enc"], &format!("{purge_ctx}: git add manifest.enc"))?;
let status = crate::helpers::git_command(vault.root(), crate::helpers::git_run(
&["commit", "-m", &format!("purge: {} ({})", title, id.as_str())]).status()?; vault.root(),
if !status.success() { anyhow::bail!("git commit failed"); } &["commit", "-m", &format!("purge: {} ({})", title, id.as_str())],
&format!("{purge_ctx}: git commit"),
)?;
Ok(()) Ok(())
} }
@@ -121,11 +123,16 @@ pub fn cmd_trash_empty() -> Result<()> {
} }
vault.save_manifest(&manifest)?; vault.save_manifest(&manifest)?;
let status = crate::helpers::git_command(vault.root(), &["add", "manifest.enc"]).status()?; crate::helpers::git_run(
if !status.success() { anyhow::bail!("git add manifest.enc failed"); } vault.root(),
let status = crate::helpers::git_command(vault.root(), &["add", "manifest.enc"],
&["commit", "-m", &format!("trash empty: purged {} item(s)", purged_titles.len())]).status()?; "trash empty: git add manifest.enc",
if !status.success() { anyhow::bail!("git commit failed"); } )?;
crate::helpers::git_run(
vault.root(),
&["commit", "-m", &format!("trash empty: purged {} item(s)", purged_titles.len())],
"trash empty: git commit",
)?;
eprintln!("Emptied trash: {} item(s)", purged_titles.len()); eprintln!("Emptied trash: {} item(s)", purged_titles.len());
Ok(()) Ok(())