feat: extract digest and compression type handling into common command functions
This commit is contained in:
committed by
Andrew Phillips (aider)
parent
947ed55fb7
commit
d1edd20845
@@ -1,15 +1,19 @@
|
||||
use humansize::{BINARY, FormatSizeOptions};
|
||||
use clap::Command;
|
||||
use log::debug;
|
||||
use prettytable::format::TableFormat;
|
||||
use clap::error::ErrorKind;
|
||||
use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use rusqlite::Connection;
|
||||
use std::str::FromStr;
|
||||
use crate::db::Item;
|
||||
use crate::db::Meta;
|
||||
use crate::db::store_meta;
|
||||
use crate::compression_engine::CompressionType;
|
||||
use crate::digest_engine::DigestType;
|
||||
use crate::digest_engine::get_digest_type_meta;
|
||||
use crate::Args;
|
||||
|
||||
pub fn get_meta_from_env() -> HashMap<String, String> {
|
||||
debug!("MAIN: Getting meta from KEEP_META_*");
|
||||
@@ -97,6 +101,11 @@ pub fn get_format_box_chars_no_border_line_separator() -> TableFormat {
|
||||
.build()
|
||||
}
|
||||
|
||||
|
||||
pub fn get_digest_type_meta(digest_type: DigestType) -> String {
|
||||
format!("digest_{}", digest_type.to_string().to_lowercase())
|
||||
}
|
||||
|
||||
pub fn store_item_digest_value(
|
||||
conn: &mut Connection,
|
||||
item: Item,
|
||||
@@ -113,3 +122,41 @@ pub fn store_item_digest_value(
|
||||
store_meta(conn, digest_meta)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cmd_args_digest_type(cmd: &mut Command, args: Args) -> DigestType {
|
||||
let digest_name = args
|
||||
.item
|
||||
.digest
|
||||
.clone()
|
||||
.unwrap_or(DigestType::Sha256.to_string());
|
||||
|
||||
let digest_type_opt = DigestType::from_str(&digest_name);
|
||||
if digest_type_opt.is_err() {
|
||||
cmd.error(
|
||||
ErrorKind::InvalidValue,
|
||||
format!("Unknown digest type: {}", digest_name),
|
||||
)
|
||||
.exit();
|
||||
}
|
||||
|
||||
digest_type_opt.unwrap()
|
||||
}
|
||||
|
||||
pub fn cmd_args_compression_type(cmd: &mut Command, args: Args) -> CompressionType {
|
||||
let compression_name = args
|
||||
.item
|
||||
.compression
|
||||
.clone()
|
||||
.unwrap_or(CompressionType::LZ4.to_string());
|
||||
|
||||
let compression_type_opt = CompressionType::from_str(&compression_name);
|
||||
if compression_type_opt.is_err() {
|
||||
cmd.error(
|
||||
ErrorKind::InvalidValue,
|
||||
format!("Unknown compression type: {}", compression_name),
|
||||
)
|
||||
.exit();
|
||||
}
|
||||
|
||||
compression_type_opt.unwrap()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user