refactor: update info mode to use ItemService
Co-authored-by: aider (openai/andrew/openrouter/google/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
@@ -1,17 +1,15 @@
|
||||
use crate::db::Item;
|
||||
use crate::modes::common::{format_size, OutputFormat};
|
||||
use crate::config;
|
||||
use anyhow::anyhow;
|
||||
use crate::core::types::ItemWithMeta;
|
||||
use crate::modes::common::{format_size, OutputFormat};
|
||||
use anyhow::{anyhow, Result};
|
||||
use serde_json;
|
||||
use serde_yaml;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use clap::Command;
|
||||
use clap::error::ErrorKind;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::compression_engine::CompressionType;
|
||||
use crate::db::{get_item, get_item_last, get_item_matching};
|
||||
use crate::core::item_service::ItemService;
|
||||
use crate::modes::common::get_format_box_chars_no_border_line_separator;
|
||||
use chrono::prelude::*;
|
||||
use is_terminal::IsTerminal;
|
||||
@@ -25,7 +23,7 @@ pub fn mode_info(
|
||||
tags: &mut Vec<String>,
|
||||
conn: &mut rusqlite::Connection,
|
||||
data_path: PathBuf,
|
||||
) -> anyhow::Result<()> {
|
||||
) -> Result<()> {
|
||||
if !ids.is_empty() && !tags.is_empty() {
|
||||
cmd.error(ErrorKind::InvalidValue, "Both ID and tags given, you must supply exactly one ID or atleast one tag when using --info").exit();
|
||||
} else if ids.len() > 1 {
|
||||
@@ -41,18 +39,12 @@ pub fn mode_info(
|
||||
}
|
||||
}
|
||||
|
||||
let item_maybe = match tags.is_empty() && meta.is_empty() {
|
||||
true => match ids.iter().next() {
|
||||
Some(item_id) => get_item(conn, *item_id)?,
|
||||
None => get_item_last(conn)?,
|
||||
},
|
||||
false => get_item_matching(conn, tags, &meta)?,
|
||||
};
|
||||
let item_service = ItemService::new(data_path.clone());
|
||||
let item_with_meta = item_service
|
||||
.find_item(conn, ids, tags, &meta)
|
||||
.map_err(|e| anyhow!("Unable to find matching item in database: {}", e))?;
|
||||
|
||||
match item_maybe {
|
||||
Some(item) => show_item(item, settings, conn, data_path),
|
||||
None => Err(anyhow!("Unable to find matching item in database")),
|
||||
}
|
||||
show_item(item_with_meta, settings, data_path)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -70,24 +62,20 @@ struct ItemInfo {
|
||||
}
|
||||
|
||||
fn show_item(
|
||||
item: Item, // Using the provided struct definition
|
||||
item_with_meta: ItemWithMeta,
|
||||
settings: &config::Settings,
|
||||
conn: &mut rusqlite::Connection,
|
||||
data_path: PathBuf,
|
||||
) -> anyhow::Result<()> {
|
||||
let item_id = item.id.unwrap(); // Consider using if let or expect for Option
|
||||
|
||||
let item_tags: Vec<String> = crate::db::get_item_tags(conn, &item)?
|
||||
.into_iter()
|
||||
.map(|x| x.name)
|
||||
.collect();
|
||||
|
||||
) -> Result<()> {
|
||||
let output_format = crate::modes::common::settings_output_format(settings);
|
||||
|
||||
if output_format != OutputFormat::Table {
|
||||
return show_item_structured(item, settings, conn, data_path, output_format);
|
||||
return show_item_structured(item_with_meta, settings, data_path, output_format);
|
||||
}
|
||||
|
||||
let item = item_with_meta.item;
|
||||
let item_id = item.id.unwrap();
|
||||
let item_tags: Vec<String> = item_with_meta.tags.into_iter().map(|t| t.name).collect();
|
||||
|
||||
let mut table = Table::new();
|
||||
if std::io::stdout().is_terminal() {
|
||||
table.set_format(get_format_box_chars_no_border_line_separator());
|
||||
@@ -125,13 +113,9 @@ fn show_item(
|
||||
size_cell,
|
||||
]));
|
||||
|
||||
// compression_type is CompressionType due to '?'
|
||||
let compression_type_val = CompressionType::from_str(&item.compression)
|
||||
.map_err(|e| anyhow!("Failed to parse compression type: {}", e))?;
|
||||
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new("Compression").with_style(Attr::Bold),
|
||||
Cell::new(&compression_type_val.to_string()),
|
||||
Cell::new(&item.compression),
|
||||
]));
|
||||
|
||||
let file_size_cell = match item_path_buf.metadata() {
|
||||
@@ -152,7 +136,7 @@ fn show_item(
|
||||
Cell::new(&item_tags.join(" ")),
|
||||
]));
|
||||
|
||||
for meta in crate::db::get_item_meta(conn, &item)? {
|
||||
for meta in item_with_meta.meta {
|
||||
let meta_name = format!("Meta: {}", &meta.name);
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new(meta_name.as_str()).with_style(Attr::Bold),
|
||||
@@ -165,17 +149,14 @@ fn show_item(
|
||||
}
|
||||
|
||||
fn show_item_structured(
|
||||
item: Item,
|
||||
item_with_meta: ItemWithMeta,
|
||||
settings: &config::Settings,
|
||||
conn: &mut rusqlite::Connection,
|
||||
data_path: PathBuf,
|
||||
output_format: OutputFormat,
|
||||
) -> anyhow::Result<()> {
|
||||
) -> Result<()> {
|
||||
let item = item_with_meta.item;
|
||||
let item_id = item.id.unwrap();
|
||||
let item_tags: Vec<String> = crate::db::get_item_tags(conn, &item)?
|
||||
.into_iter()
|
||||
.map(|x| x.name)
|
||||
.collect();
|
||||
let item_tags: Vec<String> = item_with_meta.tags.into_iter().map(|t| t.name).collect();
|
||||
|
||||
let mut item_path_buf = data_path.clone();
|
||||
item_path_buf.push(item_id.to_string());
|
||||
@@ -191,14 +172,15 @@ fn show_item_structured(
|
||||
None => "Missing".to_string(),
|
||||
};
|
||||
|
||||
let mut meta_map = std::collections::HashMap::new();
|
||||
for meta in crate::db::get_item_meta(conn, &item)? {
|
||||
meta_map.insert(meta.name, meta.value);
|
||||
}
|
||||
let meta_map = item_with_meta.meta_as_map();
|
||||
|
||||
let item_info = ItemInfo {
|
||||
id: item_id,
|
||||
timestamp: item.ts.with_timezone(&chrono::Local).format("%F %T %Z").to_string(),
|
||||
timestamp: item
|
||||
.ts
|
||||
.with_timezone(&chrono::Local)
|
||||
.format("%F %T %Z")
|
||||
.to_string(),
|
||||
path: item_path_buf.to_str().unwrap_or("").to_string(),
|
||||
stream_size: item.size.map(|s| s as u64),
|
||||
stream_size_formatted,
|
||||
|
||||
Reference in New Issue
Block a user