refactor(cli): hoist commit_paths + resolve_query into commands/mod.rs
This commit is contained in:
@@ -9,3 +9,37 @@
|
||||
pub mod generate;
|
||||
pub mod init;
|
||||
pub mod rate;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
pub(crate) fn commit_paths(
|
||||
vault: &crate::session::UnlockedVault,
|
||||
message: &str,
|
||||
paths: &[&str],
|
||||
) -> Result<()> {
|
||||
let mut args: Vec<&str> = vec!["add"];
|
||||
args.extend_from_slice(paths);
|
||||
let status = crate::helpers::git_command(vault.root(), &args).status()?;
|
||||
if !status.success() { anyhow::bail!("git add failed"); }
|
||||
let status = crate::helpers::git_command(vault.root(), &["commit", "-m", message]).status()?;
|
||||
if !status.success() { anyhow::bail!("git commit failed"); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_query<'a>(
|
||||
manifest: &'a relicario_core::Manifest,
|
||||
query: &str,
|
||||
) -> Result<&'a relicario_core::ManifestEntry> {
|
||||
if let Some(entry) = manifest.items.values().find(|e| e.id.as_str() == query) {
|
||||
return Ok(entry);
|
||||
}
|
||||
let hits: Vec<_> = manifest.search(query);
|
||||
match hits.len() {
|
||||
0 => anyhow::bail!("no item matches `{query}`"),
|
||||
1 => Ok(hits[0]),
|
||||
_ => {
|
||||
let titles: Vec<&str> = hits.iter().map(|e| e.title.as_str()).collect();
|
||||
anyhow::bail!("ambiguous — {} matches: {}", hits.len(), titles.join(", "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user