refactor: move mode_update to src/modes/update.rs
This commit is contained in:
@@ -317,7 +317,7 @@ fn main() -> Result<(), Error> {
|
||||
KeepModes::Get => crate::modes::get::mode_get(&mut cmd, args, ids, tags, &mut conn, data_path)?,
|
||||
KeepModes::Diff => mode_diff(&mut cmd, args, ids, tags, &mut conn, data_path)?,
|
||||
KeepModes::List => mode_list(&mut cmd, args, ids, tags, &mut conn, data_path)?,
|
||||
KeepModes::Update => mode_update(&mut cmd, args, ids, tags, &mut conn, data_path)?,
|
||||
KeepModes::Update => crate::modes::update::mode_update(&mut cmd, args, ids, tags, &mut conn, data_path)?,
|
||||
KeepModes::Info => mode_info(&mut cmd, args, ids, tags, &mut conn, data_path)?,
|
||||
KeepModes::Delete => crate::modes::delete::mode_delete(&mut cmd, args, ids, tags, &mut conn, data_path)?,
|
||||
KeepModes::Status => mode_status(&mut cmd, args, data_path, db_path)?,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod common;
|
||||
pub mod get;
|
||||
pub mod delete;
|
||||
pub mod update;
|
||||
|
||||
67
src/modes/update.rs
Normal file
67
src/modes/update.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use anyhow::{Context, Result, anyhow};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::db;
|
||||
use clap::Command;
|
||||
use log::{debug, warn};
|
||||
use clap::error::ErrorKind;
|
||||
use rusqlite::Connection;
|
||||
|
||||
pub fn mode_update(
|
||||
cmd: &mut Command,
|
||||
args: crate::Args,
|
||||
ids: &mut Vec<i64>,
|
||||
tags: &mut Vec<String>,
|
||||
conn: &mut Connection,
|
||||
data_path: PathBuf,
|
||||
) -> Result<()> {
|
||||
if ids.is_empty() {
|
||||
cmd.error(ErrorKind::InvalidValue, "No ID given, you must supply exactly one ID when using --update").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 --update").exit();
|
||||
}
|
||||
|
||||
let item_id = ids.iter().next().expect("Unable to determine item id");
|
||||
let item_maybe = db::get_item(conn, *item_id)?;
|
||||
|
||||
let mut item = item_maybe.expect("Unable to find item in database");
|
||||
debug!("MAIN: Found item {:?}", item);
|
||||
|
||||
if ! tags.is_empty() {
|
||||
debug!("MAIN: Updating item tags");
|
||||
db::set_item_tags(conn, item.clone(), tags)?;
|
||||
}
|
||||
|
||||
if item.size.is_none() {
|
||||
info!("Updating unknown stream size");
|
||||
let mut item_path = data_path.clone();
|
||||
item_path.push(item.id.unwrap().to_string());
|
||||
let item_file_metadata = item_path.metadata();
|
||||
|
||||
if item_file_metadata.is_ok() {
|
||||
debug!("MAIN: Updating stream size of {:?}", item_path);
|
||||
let compression_type = CompressionType::from_str(&item.compression)?;
|
||||
let compression_engine = crate::compression::get_engine(compression_type).expect("Unable to get compression engine");
|
||||
let size = compression_engine.size(item_path)? as i64;
|
||||
item.size = Some(size);
|
||||
db::update_item(&conn, item.clone())?;
|
||||
} else {
|
||||
debug!("MAIN: Unable to update size of item due to missing file {:?}", item_path);
|
||||
}
|
||||
}
|
||||
|
||||
if args.item.meta.len() > 0 {
|
||||
debug!("MAIN: Updating item meta");
|
||||
for kv in args.item.meta.iter() {
|
||||
let meta = db::Meta {
|
||||
id: item.id.unwrap(),
|
||||
name: kv.key.to_string(),
|
||||
value: kv.value.to_string()
|
||||
};
|
||||
db::store_meta(conn, meta)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user