use crate::db::Item; use crate::modes::common::{format_size, OutputFormat}; use crate::config; use anyhow::anyhow; 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::modes::common::get_format_box_chars_no_border_line_separator; use chrono::prelude::*; use is_terminal::IsTerminal; use prettytable::format; use prettytable::{Attr, Cell, Row, Table}; pub fn mode_info( cmd: &mut Command, settings: &config::Settings, ids: &mut Vec, tags: &mut Vec, conn: &mut rusqlite::Connection, data_path: PathBuf, ) -> anyhow::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 { cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply exactly one ID or atleast one tag when using --info").exit(); } let mut meta: std::collections::HashMap = std::collections::HashMap::new(); // Collect metadata from environment variables for (key, value) in std::env::vars() { if key.starts_with("KEEP_META_") && key != "KEEP_META_PLUGINS" { let meta_name = key.strip_prefix("KEEP_META_").unwrap(); meta.insert(meta_name.to_string(), value); } } 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)?, }; match item_maybe { Some(item) => show_item(item, settings, conn, data_path), None => Err(anyhow!("Unable to find matching item in database")), } } #[derive(Serialize, Deserialize)] struct ItemInfo { id: i64, timestamp: String, path: String, stream_size: Option, stream_size_formatted: String, compression: String, file_size: Option, file_size_formatted: String, tags: Vec, meta: std::collections::HashMap, } fn show_item( item: Item, // Using the provided struct definition 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 = crate::db::get_item_tags(conn, &item)? .into_iter() .map(|x| x.name) .collect(); 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); } let mut table = Table::new(); if std::io::stdout().is_terminal() { table.set_format(get_format_box_chars_no_border_line_separator()); } else { table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR); } table.add_row(Row::new(vec![ Cell::new("ID").with_style(Attr::Bold), Cell::new(&item_id.to_string()), ])); let ts_cell = Cell::new(&item.ts.with_timezone(&Local).format("%F %T %Z").to_string()); table.add_row(Row::new(vec![ Cell::new("Timestamp").with_style(Attr::Bold), ts_cell, ])); let mut item_path_buf = data_path.clone(); // Renamed to avoid conflict if item_path is used later item_path_buf.push(item.id.unwrap().to_string()); // Again, consider safer unwrap table.add_row(Row::new(vec![ Cell::new("Path").with_style(Attr::Bold), Cell::new(item_path_buf.to_str().expect("Unable to get item path")), ])); let size_cell = match item.size { Some(size) => Cell::new(format_size(size as u64, settings.human_readable).as_str()), None => Cell::new("Missing") .with_style(Attr::ForegroundColor(prettytable::color::RED)) .with_style(Attr::Bold), }; table.add_row(Row::new(vec![ Cell::new("Stream Size").with_style(Attr::Bold), 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()), ])); let file_size_cell = match item_path_buf.metadata() { Ok(metadata) => { Cell::new(format_size(metadata.len(), settings.human_readable).as_str()) } Err(_) => Cell::new("Missing") .with_style(Attr::ForegroundColor(prettytable::color::RED)) .with_style(Attr::Bold), }; table.add_row(Row::new(vec![ Cell::new("File Size").with_style(Attr::Bold), file_size_cell, ])); table.add_row(Row::new(vec![ Cell::new("Tags").with_style(Attr::Bold), Cell::new(&item_tags.join(" ")), ])); for meta in crate::db::get_item_meta(conn, &item)? { let meta_name = format!("Meta: {}", &meta.name); table.add_row(Row::new(vec![ Cell::new(meta_name.as_str()).with_style(Attr::Bold), Cell::new(&meta.value), ])); } table.printstd(); Ok(()) } fn show_item_structured( item: Item, settings: &config::Settings, conn: &mut rusqlite::Connection, data_path: PathBuf, output_format: OutputFormat, ) -> anyhow::Result<()> { let item_id = item.id.unwrap(); let item_tags: Vec = crate::db::get_item_tags(conn, &item)? .into_iter() .map(|x| x.name) .collect(); 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 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 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(()) }