Initial commit

This commit is contained in:
Andrew Phillips
2023-07-25 17:33:50 -03:00
commit b9bf5a831e
6 changed files with 1089 additions and 0 deletions

224
src/main.rs Normal file
View File

@@ -0,0 +1,224 @@
use std::str::FromStr;
use std::path::PathBuf;
use clap::error::ErrorKind;
use clap::*;
use log::*;
pub mod compression;
pub mod db;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[command(flatten)]
mode: ModeArgs,
#[command(flatten)]
item: ItemArgs,
#[command(flatten)]
options: OptionsArgs,
#[arg()]
ids_or_tags: Vec<NumberOrString>
}
#[derive(Parser, Debug)]
struct ModeArgs {
#[arg(group("mode"), help_heading("Mode"), short, long, conflicts_with_all(["get", "list", "update", "delete", "status"]))]
save: bool,
#[arg(group("mode"), help_heading("Mode"), short, long, conflicts_with_all(["save", "list", "update", "delete", "status"]))]
get: bool,
#[arg(group("mode"), help_heading("Mode"), short, long, conflicts_with_all(["save", "get", "update", "delete", "status"]))]
list: bool,
#[arg(group("mode"), help_heading("Mode"), short, long, conflicts_with_all(["save", "get", "list", "delete", "status"]), requires("ids_or_tags"))]
update: bool,
#[arg(group("mode"), help_heading("Mode"), short, long, conflicts_with_all(["save", "get", "list", "update", "status"]), requires("ids_or_tags"))]
delete: bool,
#[arg(group("mode"), help_heading("Mode"), short('S'), long, conflicts_with_all(["save", "get", "list", "update", "delete"]))]
status: bool
}
#[derive(Parser, Debug)]
struct ItemArgs {
#[arg(help_heading("Item"), short, long, conflicts_with("get"), conflicts_with("list"))]
comment: Option<String>,
#[arg(help_heading("Item"), short('C'), long, conflicts_with("get"), conflicts_with("list"), env("KEEP_COMPRESS"))]
compress: Option<String>,
}
#[derive(Parser, Debug)]
struct OptionsArgs {
#[arg(help_heading("Options"), long, env("KEEP_DIR"))]
dir: Option<PathBuf>,
#[arg(help_heading("Options"), short, long)]
force: bool,
#[arg(help_heading("Options"), short, long, action = clap::ArgAction::Count, conflicts_with("quiet"))]
verbose: u8,
#[arg(help_heading("Options"), short, long)]
quiet: bool,
}
#[derive(Debug,Clone)]
enum NumberOrString {
Number(u32),
Str(String),
}
#[derive(Debug,PartialEq)]
enum KeepModes {
Unknown,
Save,
Get,
List,
Update,
Delete,
Status
}
impl FromStr for NumberOrString {
type Err = &'static str; // The actual type doesn't matter since we never error, but it must implement `Display`
fn from_str(s: &str) -> Result<Self, Self::Err>
{
Ok (s.parse::<u32>()
.map(NumberOrString::Number)
.unwrap_or_else(|_| NumberOrString::Str (s.to_string())))
}
}
fn main() {
let mut cmd = Args::command();
let args = Args::parse();
stderrlog::new()
.module(module_path!())
.quiet(args.options.quiet)
.verbosity(usize::from(args.options.verbose + 2))
.timestamp(stderrlog::Timestamp::Second)
.init()
.unwrap();
debug!("Start");
let ids = &mut Vec::new();
let tags = &mut Vec::new();
for v in args.ids_or_tags.iter() {
match v.clone() {
NumberOrString::Number(num) => ids.push(num),
NumberOrString::Str(str) => tags.push(str),
}
}
let mut mode: KeepModes = KeepModes::Unknown;
if args.mode.save {
mode = KeepModes::Save;
} else if args.mode.get {
mode = KeepModes::Get;
} else if args.mode.list {
mode = KeepModes::List;
} else if args.mode.delete {
mode = KeepModes::Delete;
} else if args.mode.update {
mode = KeepModes::Update;
} else if args.mode.status {
mode = KeepModes::Status;
}
if mode == KeepModes::Unknown {
if ! ids.is_empty() {
mode = KeepModes::Get;
} else {
mode = KeepModes::Save;
}
}
debug!("args: {:?}", args);
debug!("ids: {:?}", ids);
debug!("tags: {:?}", tags);
debug!("mode: {:?}", mode);
match mode {
KeepModes::Save => mode_save(&mut cmd, args, ids, tags),
KeepModes::Get => mode_get(&mut cmd, args, ids, tags),
KeepModes::List => mode_list(&mut cmd, args, ids, tags),
KeepModes::Update => mode_update(&mut cmd, args, ids, tags),
KeepModes::Delete => mode_delete(&mut cmd, args, ids, tags),
KeepModes::Status => mode_status(&mut cmd, args),
_ => todo!()
}
}
fn mode_save(cmd: &mut Command, args: Args, ids: &mut Vec<u32>, tags: &mut Vec<String>) {
if ! ids.is_empty() {
cmd.error(ErrorKind::InvalidValue, "ID given, you cannot supply IDs when using --save").exit();
}
if tags.is_empty() {
tags.push("none".to_string());
}
let compression_type = compression::get_compression(args.item.compress);
}
fn mode_get(cmd: &mut Command, args: Args, ids: &mut Vec<u32>, tags: &mut Vec<String>) {
if ids.is_empty() && tags.is_empty() {
cmd.error(ErrorKind::InvalidValue, "No ID or tags given, ou must supply one ID or atleast one tag when using --get").exit();
} else if ! ids.is_empty() && ! tags.is_empty() {
cmd.error(ErrorKind::InvalidValue, "Both ID and tags given, you must supply one ID or atleast one tag when using --get").exit();
} else if ids.len() > 1 {
cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply one ID or atleast one tag when using --get").exit();
}
}
fn mode_list(cmd: &mut Command, args: Args, ids: &mut Vec<u32>, tags: &mut Vec<String>) {
if ! ids.is_empty() {
cmd.error(ErrorKind::InvalidValue, "ID given, you can only supply tags when using --list").exit();
}
}
fn mode_update(cmd: &mut Command, args: Args, ids: &mut Vec<u32>, tags: &mut Vec<String>) {
if ids.is_empty() {
cmd.error(ErrorKind::InvalidValue, "No ID given, you must supply one ID when using --update").exit();
}
}
fn mode_delete(cmd: &mut Command, args: Args, ids: &mut Vec<u32>, tags: &mut Vec<String>) {
if ids.is_empty() {
cmd.error(ErrorKind::InvalidValue, "No ID given, you must supply one ID when using --delete").exit();
} else if ! tags.is_empty() {
cmd.error(ErrorKind::InvalidValue, "Tags given, you must supply one ID when using --delete").exit();
} else if ids.len() > 1 {
cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply one ID when using --delete").exit();
}
}
fn mode_status_show_compression() {
let compression_types = compression::supported_compression_types();
println!("compression_types:");
for compression_type in compression_types.into_iter() {
println!(" {}", compression_type);
}
}
fn mode_status(cmd: &mut Command, args: Args) {
mode_status_show_compression();
}