- Add --update mode to modify tags and metadata for existing items by ID
- Add --meta key=value flag to set metadata during save/update
- Add --meta key (bare) to delete metadata keys or filter by existence
- Add --meta-plugin/-M name:{json} flag for plugin options via CLI
- Env meta plugin now uses options from --meta-plugin instead of only env vars
- Stream decompressed content to diff via /dev/fd pipes (no temp files)
- Wire --list-format CLI arg to settings (was parsed but ignored)
- Allow --info to accept tags (was restricted to numeric IDs only)
- Change DB meta filtering to HashMap<String, Option<String>> for exact match + key existence
- Fix fcntl error checking in diff pre_exec
- Fix README inaccuracies (delete by tag, nonexistent --digest flag, meta plugin key names)
301 lines
9.6 KiB
Rust
301 lines
9.6 KiB
Rust
use crate::config;
|
|
use crate::modes::common::{OutputFormat, format_size};
|
|
use crate::services::types::ItemWithMeta;
|
|
use anyhow::{Context, Result, anyhow};
|
|
use clap::Command;
|
|
use clap::error::ErrorKind;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::path::PathBuf;
|
|
|
|
use crate::services::item_service::ItemService;
|
|
use chrono::prelude::*;
|
|
use comfy_table::{Attribute, Cell};
|
|
|
|
/// Displays detailed information about an item or the last item if no ID/tags specified.
|
|
///
|
|
/// Supports table, JSON, or YAML output formats. Validates input (at most one ID, no mixing IDs/tags).
|
|
/// Uses ItemService to fetch the item and displays via helpers.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `cmd` - Mutable Clap command for error handling and exiting on invalid args.
|
|
/// * `settings` - Application settings for output formatting and human-readable sizes.
|
|
/// * `ids` - Mutable vector of item IDs (at most one; cleared if tags used).
|
|
/// * `tags` - Mutable vector of tags (mutually exclusive with IDs).
|
|
/// * `conn` - Mutable database connection for querying items.
|
|
/// * `data_path` - Path to data directory for file metadata.
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// `Ok(())` on success, or `Err(anyhow::Error)` if item not found or DB query fails.
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// * Clap errors if invalid args (e.g., multiple IDs).
|
|
/// * Anyhow error if no matching item found.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```ignore
|
|
/// // Example usage requires Command, Settings, Connection, and PathBuf instances
|
|
/// mode_info(&mut cmd, &settings, &mut vec![123], &mut vec![], &mut conn, data_path)?;
|
|
/// ```
|
|
pub fn mode_info(
|
|
cmd: &mut Command,
|
|
settings: &config::Settings,
|
|
ids: &mut [i64],
|
|
tags: &mut [String],
|
|
conn: &mut rusqlite::Connection,
|
|
data_path: PathBuf,
|
|
) -> Result<()> {
|
|
// For --info, we can use either IDs or tags, but not both
|
|
if !ids.is_empty() && !tags.is_empty() {
|
|
cmd.error(
|
|
ErrorKind::InvalidValue,
|
|
"Both ID and tags given, you must supply either IDs or tags when using --info",
|
|
)
|
|
.exit();
|
|
} else if ids.len() > 1 {
|
|
cmd.error(
|
|
ErrorKind::InvalidValue,
|
|
"More than one ID given, you must supply exactly one ID when using --info",
|
|
)
|
|
.exit();
|
|
}
|
|
// If both are empty, find_item will find the last item
|
|
|
|
let item_service = ItemService::new(data_path.clone());
|
|
let meta_filter: std::collections::HashMap<String, Option<String>> = settings
|
|
.meta
|
|
.iter()
|
|
.map(|(k, v)| (k.clone(), v.clone()))
|
|
.collect();
|
|
let item_with_meta = item_service
|
|
.find_item(conn, ids, tags, &meta_filter)
|
|
.map_err(|e| anyhow!("Unable to find matching item in database: {}", e))?;
|
|
|
|
show_item(item_with_meta, settings, data_path)
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
/// Structured representation of item information for JSON/YAML output.
|
|
///
|
|
/// This struct serializes item details including ID, timestamp, sizes, compression, tags, and metadata
|
|
/// for non-table output formats.
|
|
///
|
|
/// # Fields
|
|
///
|
|
/// * `id` - The unique item ID.
|
|
/// * `timestamp` - Formatted timestamp string.
|
|
/// * `path` - Full file path to the item.
|
|
/// * `stream_size` - Original uncompressed size in bytes (optional).
|
|
/// * `stream_size_formatted` - Human-readable stream size.
|
|
/// * `compression` - Compression type used.
|
|
/// * `file_size` - Compressed file size in bytes (optional).
|
|
/// * `file_size_formatted` - Human-readable file size.
|
|
/// * `tags` - List of associated tags.
|
|
/// * `meta` - Metadata key-value pairs.
|
|
pub struct ItemInfo {
|
|
id: i64,
|
|
timestamp: String,
|
|
path: String,
|
|
stream_size: Option<u64>,
|
|
stream_size_formatted: String,
|
|
compression: String,
|
|
file_size: Option<u64>,
|
|
file_size_formatted: String,
|
|
tags: Vec<String>,
|
|
meta: std::collections::HashMap<String, String>,
|
|
}
|
|
|
|
/// Displays item information in table format or delegates to structured output.
|
|
///
|
|
/// Builds a comfy-table for tabular display or calls structured helper for JSON/YAML.
|
|
/// Handles file size via metadata and formats tags/meta accordingly.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `item_with_meta` - Item with associated metadata and tags.
|
|
/// * `settings` - Application settings for formatting (e.g., human-readable sizes).
|
|
/// * `data_path` - Path to data directory for calculating compressed file size.
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// `Ok(())` on success, or `Err(anyhow::Error)` if path resolution fails.
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// * Anyhow error if item path cannot be stringified.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```ignore
|
|
/// // Example usage requires ItemWithMeta, Settings, and PathBuf instances
|
|
/// show_item(item_with_meta, &settings, data_path)?;
|
|
/// ```
|
|
fn show_item(
|
|
item_with_meta: ItemWithMeta,
|
|
settings: &config::Settings,
|
|
data_path: PathBuf,
|
|
) -> Result<()> {
|
|
let output_format = crate::modes::common::settings_output_format(settings);
|
|
|
|
if output_format != OutputFormat::Table {
|
|
return show_item_structured(item_with_meta, settings, data_path, output_format);
|
|
}
|
|
|
|
let item = item_with_meta.item;
|
|
let item_id = item.id.context("Item missing ID")?;
|
|
let item_tags: Vec<String> = item_with_meta.tags.iter().map(|t| t.name.clone()).collect();
|
|
|
|
let mut table = crate::modes::common::create_table(false);
|
|
|
|
// Add all the rows
|
|
table.add_row(vec![
|
|
Cell::new("ID").add_attribute(Attribute::Bold),
|
|
Cell::new(item_id.to_string()),
|
|
]);
|
|
|
|
let timestamp_str = item.ts.with_timezone(&Local).format("%F %T %Z").to_string();
|
|
table.add_row(vec![
|
|
Cell::new("Timestamp").add_attribute(Attribute::Bold),
|
|
Cell::new(×tamp_str),
|
|
]);
|
|
|
|
let mut item_path_buf = data_path.clone();
|
|
item_path_buf.push(item_id.to_string());
|
|
let path_str = item_path_buf
|
|
.to_str()
|
|
.ok_or_else(|| anyhow::anyhow!("non-UTF-8 item path"))?
|
|
.to_string();
|
|
table.add_row(vec![
|
|
Cell::new("Path").add_attribute(Attribute::Bold),
|
|
Cell::new(&path_str),
|
|
]);
|
|
|
|
let size_str = match item.size {
|
|
Some(size) => format_size(size as u64, settings.human_readable),
|
|
None => "Missing".to_string(),
|
|
};
|
|
table.add_row(vec![
|
|
Cell::new("Stream Size").add_attribute(Attribute::Bold),
|
|
Cell::new(&size_str),
|
|
]);
|
|
|
|
table.add_row(vec![
|
|
Cell::new("Compression").add_attribute(Attribute::Bold),
|
|
Cell::new(&item.compression),
|
|
]);
|
|
|
|
let file_size_str = match item_path_buf.metadata() {
|
|
Ok(metadata) => format_size(metadata.len(), settings.human_readable),
|
|
Err(_) => "Missing".to_string(),
|
|
};
|
|
table.add_row(vec![
|
|
Cell::new("File Size").add_attribute(Attribute::Bold),
|
|
Cell::new(&file_size_str),
|
|
]);
|
|
|
|
let tags_str = item_tags.join(" ");
|
|
table.add_row(vec![
|
|
Cell::new("Tags").add_attribute(Attribute::Bold),
|
|
Cell::new(&tags_str),
|
|
]);
|
|
|
|
// Add meta rows
|
|
for meta in item_with_meta.meta {
|
|
let meta_name = format!("Meta: {}", &meta.name);
|
|
table.add_row(vec![
|
|
Cell::new(&meta_name).add_attribute(Attribute::Bold),
|
|
Cell::new(&meta.value),
|
|
]);
|
|
}
|
|
|
|
println!(
|
|
"{}",
|
|
crate::modes::common::trim_lines_end(&table.trim_fmt())
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
/// Displays item information in structured JSON or YAML format.
|
|
///
|
|
/// Serializes ItemInfo and prints pretty-formatted output. Handles file metadata for sizes.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `item_with_meta` - Item with metadata and tags.
|
|
/// * `settings` - Settings for size formatting (human-readable).
|
|
/// * `data_path` - Data path for compressed file size calculation.
|
|
/// * `output_format` - JSON or YAML (Table is unreachable here).
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// `Ok(())` on success, or `Err(anyhow::Error)` if serialization or path fails.
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// * Serde errors during JSON/YAML serialization.
|
|
/// * Anyhow error if file metadata unavailable.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```ignore
|
|
/// // Example usage requires ItemWithMeta, Settings, PathBuf, and OutputFormat instances
|
|
/// show_item_structured(item_with_meta, &settings, data_path, OutputFormat::Json)?;
|
|
/// ```
|
|
fn show_item_structured(
|
|
item_with_meta: ItemWithMeta,
|
|
settings: &config::Settings,
|
|
data_path: PathBuf,
|
|
output_format: OutputFormat,
|
|
) -> Result<()> {
|
|
let item_tags: Vec<String> = item_with_meta.tags.iter().map(|t| t.name.clone()).collect();
|
|
let meta_map = item_with_meta.meta_as_map();
|
|
let item = item_with_meta.item;
|
|
let item_id = item.id.context("Item missing ID")?;
|
|
|
|
let mut item_path_buf = data_path.clone();
|
|
item_path_buf.push(item_id.to_string());
|
|
|
|
let file_size = item_path_buf.metadata().map(|m| m.len()).ok();
|
|
let file_size_formatted = match file_size {
|
|
Some(size) => format_size(size, settings.human_readable),
|
|
None => "Missing".to_string(),
|
|
};
|
|
|
|
let stream_size_formatted = match item.size {
|
|
Some(size) => format_size(size as u64, settings.human_readable),
|
|
None => "Missing".to_string(),
|
|
};
|
|
|
|
let item_info = ItemInfo {
|
|
id: item_id,
|
|
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,
|
|
compression: item.compression,
|
|
file_size,
|
|
file_size_formatted,
|
|
tags: item_tags,
|
|
meta: meta_map,
|
|
};
|
|
|
|
match output_format {
|
|
OutputFormat::Json => {
|
|
println!("{}", serde_json::to_string_pretty(&item_info)?);
|
|
}
|
|
OutputFormat::Yaml => {
|
|
println!("{}", serde_yaml::to_string(&item_info)?);
|
|
}
|
|
OutputFormat::Table => unreachable!(),
|
|
}
|
|
|
|
Ok(())
|
|
}
|