feat: Implement --get command to retrieve items by ID or tag with compression handling
This commit is contained in:
@@ -4,3 +4,51 @@ use clap::Command;
|
||||
|
||||
use crate::db;
|
||||
use crate::compression::{CompressionType, get_engine};
|
||||
use anyhow::Context;
|
||||
use clap::Command;
|
||||
use crate::db::Item;
|
||||
use crate::db::Meta;
|
||||
use crate::compression::CompressionEngine;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn mode_get(
|
||||
cmd: &mut Command,
|
||||
args: crate::Args,
|
||||
ids: &mut Vec<i64>,
|
||||
tags: &mut Vec<String>,
|
||||
conn: &mut crate::db::Connection,
|
||||
data_path: PathBuf,
|
||||
) -> anyhow::Result<()> {
|
||||
if !ids.is_empty() && !tags.is_empty() {
|
||||
cmd.error(clap::error::ErrorKind::InvalidValue, "Both ID and tags given, you must supply exactly one ID or at least one tag when using --get").exit();
|
||||
} else if ids.len() > 1 {
|
||||
cmd.error(clap::error::ErrorKind::InvalidValue, "More than one ID given, you must supply exactly one ID or at least one tag when using --get").exit();
|
||||
}
|
||||
|
||||
let mut meta: std::collections::HashMap<String, String> = std::collections::HashMap::new();
|
||||
for item in args.item.meta.iter() {
|
||||
let item = item.clone();
|
||||
meta.insert(item.key, item.value);
|
||||
}
|
||||
|
||||
let item_maybe = match tags.is_empty() && meta.is_empty() {
|
||||
true => match ids.iter().next() {
|
||||
Some(item_id) => crate::db::get_item(conn, *item_id)?,
|
||||
None => crate::db::get_item_last(conn)?
|
||||
},
|
||||
false => crate::db::get_item_matching(conn, tags, &meta)?
|
||||
};
|
||||
|
||||
if let Some(item) = item_maybe {
|
||||
let mut item_path = data_path.clone();
|
||||
item_path.push(item.id.unwrap().to_string());
|
||||
|
||||
let compression_type = crate::compression::CompressionType::from_str(&item.compression)?;
|
||||
let compression_engine = crate::compression::get_engine(compression_type)?;
|
||||
compression_engine.cat(item_path.clone())?;
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("Unable to find matching item in database"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user