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 generate;
|
||||||
pub mod init;
|
pub mod init;
|
||||||
pub mod rate;
|
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(", "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ use anyhow::{bail, Context, Result};
|
|||||||
use clap::{CommandFactory, Parser, Subcommand};
|
use clap::{CommandFactory, Parser, Subcommand};
|
||||||
use clap_complete::{generate, Shell};
|
use clap_complete::{generate, Shell};
|
||||||
|
|
||||||
|
use crate::commands::{commit_paths, resolve_query};
|
||||||
use crate::parse::{base32_decode_lenient, guess_mime, parse_month_year};
|
use crate::parse::{base32_decode_lenient, guess_mime, parse_month_year};
|
||||||
use crate::prompt::{prompt, prompt_keep, prompt_keep_opt, prompt_optional, prompt_secret, prompt_yesno};
|
use crate::prompt::{prompt, prompt_keep, prompt_keep_opt, prompt_optional, prompt_secret, prompt_yesno};
|
||||||
|
|
||||||
@@ -814,16 +815,6 @@ fn build_totp_item(
|
|||||||
Ok(item)
|
Ok(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
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(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cmd_get(query: String, show: bool, copy: bool) -> Result<()> {
|
fn cmd_get(query: String, show: bool, copy: bool) -> Result<()> {
|
||||||
use relicario_core::ItemCore;
|
use relicario_core::ItemCore;
|
||||||
use zeroize::Zeroizing;
|
use zeroize::Zeroizing;
|
||||||
@@ -910,24 +901,6 @@ fn cmd_get(query: String, show: bool, copy: bool) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
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(", "))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn copy_to_clipboard_then_clear(secret: &zeroize::Zeroizing<String>) -> Result<()> {
|
fn copy_to_clipboard_then_clear(secret: &zeroize::Zeroizing<String>) -> Result<()> {
|
||||||
use arboard::Clipboard;
|
use arboard::Clipboard;
|
||||||
let mut cb = Clipboard::new().context("failed to access clipboard")?;
|
let mut cb = Clipboard::new().context("failed to access clipboard")?;
|
||||||
|
|||||||
Reference in New Issue
Block a user