style: reorder imports and reformat code for consistency

This commit is contained in:
Andrew Phillips
2025-05-10 10:06:33 -03:00
committed by Andrew Phillips (aider)
parent c936326ac3
commit 9feec61759
13 changed files with 492 additions and 356 deletions

View File

@@ -1,8 +1,8 @@
use std::env;
use std::collections::HashMap;
use regex::Regex;
use humansize::{FormatSizeOptions, BINARY};
use log::debug;
use regex::Regex;
use std::collections::HashMap;
use std::env;
pub fn get_meta_from_env() -> HashMap<String, String> {
debug!("MAIN: Getting meta from KEEP_META_*");
@@ -19,15 +19,14 @@ pub fn get_meta_from_env() -> HashMap<String, String> {
}
pub fn format_size_human_readable(size: u64) -> String {
let options = FormatSizeOptions::from(BINARY)
.decimal_places(1);
let options = FormatSizeOptions::from(BINARY).decimal_places(1);
humansize::format_size(size, options)
}
pub fn format_size(size: u64, human_readable: bool) -> String {
match human_readable {
true => format_size_human_readable(size),
false => size.to_string()
false => size.to_string(),
}
}

View File

@@ -1,11 +1,11 @@
use anyhow::{Context, Result, anyhow};
use anyhow::{anyhow, Context, Result};
use std::fs;
use std::path::PathBuf;
use crate::db;
use clap::error::ErrorKind;
use clap::Command;
use log::{debug, warn};
use clap::error::ErrorKind;
use rusqlite::Connection;
pub fn mode_delete(
@@ -17,9 +17,17 @@ pub fn mode_delete(
data_path: PathBuf,
) -> Result<()> {
if ids.is_empty() {
cmd.error(ErrorKind::InvalidValue, "No ID given, you must supply atleast one ID when using --delete").exit();
} else if ! tags.is_empty() {
cmd.error(ErrorKind::InvalidValue, "Tags given but not supported, you must supply atleast one ID when using --delete").exit();
cmd.error(
ErrorKind::InvalidValue,
"No ID given, you must supply atleast one ID when using --delete",
)
.exit();
} else if !tags.is_empty() {
cmd.error(
ErrorKind::InvalidValue,
"Tags given but not supported, you must supply atleast one ID when using --delete",
)
.exit();
}
for item_id in ids.iter() {
@@ -30,7 +38,8 @@ pub fn mode_delete(
let mut item_path = data_path.clone();
item_path.push(item_id.to_string());
fs::remove_file(&item_path).context(anyhow!("Unable to remove item file {:?}", item_path))?;
fs::remove_file(&item_path)
.context(anyhow!("Unable to remove item file {:?}", item_path))?;
} else {
warn!("Unable to find item {item_id} in database");
}

View File

@@ -1,10 +1,9 @@
use anyhow::anyhow;
use clap::Command;
use crate::compression::CompressionType;
use std::str::FromStr;
use clap::Command;
use std::path::PathBuf;
use std::str::FromStr;
pub fn mode_get(
cmd: &mut Command,
@@ -29,9 +28,9 @@ pub fn mode_get(
let item_maybe = match tags.is_empty() && meta.is_empty() {
true => match ids.iter().next() {
Some(item_id) => crate::db::get_item(conn, *item_id)?,
None => crate::db::get_item_last(conn)?
None => crate::db::get_item_last(conn)?,
},
false => crate::db::get_item_matching(conn, tags, &meta)?
false => crate::db::get_item_matching(conn, tags, &meta)?,
};
if let Some(item) = item_maybe {

View File

@@ -1,5 +1,5 @@
pub mod common;
pub mod get;
pub mod delete;
pub mod update;
pub mod get;
pub mod status;
pub mod update;

View File

@@ -1,39 +1,27 @@
use std::io;
use std::path::PathBuf;
use anyhow::{Context, Result, anyhow};
use rusqlite::Connection;
use gethostname::gethostname;
use strum::IntoEnumIterator;
use clap::error::ErrorKind;
use anyhow::{anyhow, Context, Result};
use clap::*;
use log::*;
use is_terminal::IsTerminal;
use chrono::prelude::*;
use std::os::fd::FromRawFd;
use std::process::Stdio;
use nix::fcntl::{FdFlag};
use nix::unistd::{close, pipe};
use nix::Error as NixError;
use std::path::PathBuf;
use strum::IntoEnumIterator;
use crate::compression::CompressionType;
use strum::EnumString;
use crate::compression::program::CompressionEngineProgram;
use crate::compression::COMPRESSION_PROGRAMS;
use crate::compression::default_type;
use crate::db;
use crate::modes::common::{format_size, string_column};
use crate::compression::program::CompressionEngineProgram;
use crate::compression::CompressionType;
use crate::compression::COMPRESSION_PROGRAMS;
use std::str::FromStr;
use crate::compression::CompressionEngine;
use prettytable::{Table, Row, Cell, Attr};
use prettytable::format;
use prettytable::format::{TableFormat, Alignment};
use prettytable::row;
use prettytable::color;
use crate::FORMAT_BOX_CHARS_NO_BORDER_LINE_SEPARATOR;
use crate::FORMAT_NO_BORDER_LINE_SEPARATOR;
use prettytable::color;
use prettytable::row;
use prettytable::{Attr, Cell, Row, Table};
pub fn mode_status(_cmd: &mut Command, args: crate::Args, data_path: PathBuf, db_path: PathBuf) -> Result<()> {
pub fn mode_status(
_cmd: &mut Command,
args: crate::Args,
data_path: PathBuf,
db_path: PathBuf,
) -> Result<()> {
let mut path_table = Table::new();
if std::io::stdout().is_terminal() {
@@ -49,15 +37,24 @@ pub fn mode_status(_cmd: &mut Command, args: crate::Args, data_path: PathBuf, db
path_table.add_row(Row::new(vec![
Cell::new("Data"),
Cell::new(&data_path.into_os_string().into_string().expect("Unable to convert data path to string"))
Cell::new(
&data_path
.into_os_string()
.into_string()
.expect("Unable to convert data path to string"),
),
]));
path_table.add_row(Row::new(vec![
Cell::new("Database"),
Cell::new(&db_path.into_os_string().into_string().expect("Unable to convert DB path to string"))
Cell::new(
&db_path
.into_os_string()
.into_string()
.expect("Unable to convert DB path to string"),
),
]));
let mut compression_table = Table::new();
if std::io::stdout().is_terminal() {
compression_table.set_format(*FORMAT_BOX_CHARS_NO_BORDER_LINE_SEPARATOR);
@@ -73,38 +70,40 @@ pub fn mode_status(_cmd: &mut Command, args: crate::Args, data_path: PathBuf, db
b->"Compress",
b->"Decompress"));
let default_type = match args.item.compression {
Some(compression_name) => FromStr::from_str(&compression_name)
.context(anyhow!("Invalid compression type {}", compression_name))?,
None => default_type()
None => default_type(),
};
for compression_type in CompressionType::iter() {
let compression_program: CompressionEngineProgram = match &COMPRESSION_PROGRAMS[compression_type.clone()] {
Some(compression_program) => compression_program.clone(),
None => CompressionEngineProgram {
program: "".to_string(),
compress: Vec::new(),
decompress: Vec::new(),
supported: true
}
};
let compression_program: CompressionEngineProgram =
match &COMPRESSION_PROGRAMS[compression_type.clone()] {
Some(compression_program) => compression_program.clone(),
None => CompressionEngineProgram {
program: "".to_string(),
compress: Vec::new(),
decompress: Vec::new(),
supported: true,
},
};
let is_default = compression_type == default_type;
compression_table.add_row(Row::new(vec![
Cell::new(&compression_type.to_string()),
match compression_program.supported {
true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)),
false => Cell::new("No").with_style(Attr::ForegroundColor(color::RED))
true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)),
false => Cell::new("No").with_style(Attr::ForegroundColor(color::RED)),
},
match is_default {
true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)),
false => Cell::new("No")
true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)),
false => Cell::new("No"),
},
match compression_program.program.eq("") {
true => Cell::new("<INTERNAL>").with_style(Attr::ForegroundColor(color::BRIGHT_BLACK)),
true => {
Cell::new("<INTERNAL>").with_style(Attr::ForegroundColor(color::BRIGHT_BLACK))
}
false => Cell::new(&compression_program.program),
},
Cell::new(&compression_program.compress.join(" ")),

View File

@@ -3,10 +3,10 @@ use std::path::PathBuf;
use std::str::FromStr;
use crate::db;
use clap::Command;
use clap::error::ErrorKind;
use log::{debug, info};
use crate::CompressionType;
use clap::error::ErrorKind;
use clap::Command;
use log::{debug, info};
use rusqlite::Connection;
pub fn mode_update(
@@ -18,7 +18,11 @@ pub fn mode_update(
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();
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();
}
@@ -29,7 +33,7 @@ pub fn mode_update(
let mut item = item_maybe.expect("Unable to find item in database");
debug!("MAIN: Found item {:?}", item);
if ! tags.is_empty() {
if !tags.is_empty() {
debug!("MAIN: Updating item tags");
db::set_item_tags(conn, item.clone(), tags)?;
}
@@ -43,12 +47,16 @@ pub fn mode_update(
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 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);
debug!(
"MAIN: Unable to update size of item due to missing file {:?}",
item_path
);
}
}
@@ -58,7 +66,7 @@ pub fn mode_update(
let meta = db::Meta {
id: item.id.unwrap(),
name: kv.key.to_string(),
value: kv.value.to_string()
value: kv.value.to_string(),
};
db::store_meta(conn, meta)?;
}