204 lines
5.7 KiB
Rust
204 lines
5.7 KiB
Rust
mod args;
|
|
mod config;
|
|
mod modes;
|
|
|
|
use std::path::PathBuf;
|
|
use anyhow::{Context, Error, Result, anyhow};
|
|
use clap::*;
|
|
use clap::error::ErrorKind;
|
|
use log::*;
|
|
|
|
extern crate directories;
|
|
use directories::ProjectDirs;
|
|
|
|
extern crate prettytable;
|
|
|
|
|
|
extern crate lazy_static;
|
|
|
|
pub mod compression_engine;
|
|
pub mod db;
|
|
pub mod plugins;
|
|
pub mod meta_plugin;
|
|
pub mod common;
|
|
//pub mod item;
|
|
|
|
extern crate term;
|
|
extern crate serde_json;
|
|
extern crate serde_yaml;
|
|
extern crate serde;
|
|
|
|
use args::{Args, NumberOrString};
|
|
use config::{Config, Settings};
|
|
|
|
/**
|
|
* Main function to handle command-line arguments and execute the appropriate mode.
|
|
*/
|
|
fn main() -> Result<(), Error> {
|
|
use std::fs;
|
|
let proj_dirs = ProjectDirs::from("gt0.ca", "Andrew Phillips", "Keep");
|
|
|
|
let mut cmd = Args::command();
|
|
let mut 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!("MAIN: Start");
|
|
|
|
// Determine default data directory
|
|
let default_dir = match proj_dirs {
|
|
Some(ref proj_dirs) => proj_dirs.data_dir().to_path_buf(),
|
|
None => return Err(anyhow!("Unable to determine data directory")),
|
|
};
|
|
|
|
// Create unified settings using the new config system
|
|
let settings = Settings::new(&args, default_dir)?;
|
|
|
|
debug!("MAIN: Loaded settings: {:?}", settings);
|
|
|
|
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),
|
|
}
|
|
}
|
|
|
|
tags.sort();
|
|
tags.dedup();
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
enum KeepModes {
|
|
Unknown,
|
|
Save,
|
|
Get,
|
|
Diff,
|
|
List,
|
|
Delete,
|
|
Update,
|
|
Info,
|
|
Status,
|
|
Server,
|
|
}
|
|
|
|
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.diff {
|
|
mode = KeepModes::Diff;
|
|
} 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.info {
|
|
mode = KeepModes::Info;
|
|
} else if args.mode.status {
|
|
mode = KeepModes::Status;
|
|
} else if args.mode.server {
|
|
mode = KeepModes::Server;
|
|
}
|
|
|
|
if mode == KeepModes::Unknown {
|
|
if !ids.is_empty() {
|
|
mode = KeepModes::Get;
|
|
} else {
|
|
mode = KeepModes::Save;
|
|
}
|
|
}
|
|
|
|
// Validate output format usage
|
|
if let Some(output_format_str) = &settings.output_format {
|
|
if output_format_str != "table" && mode != KeepModes::Info && mode != KeepModes::Status && mode != KeepModes::List {
|
|
cmd.error(
|
|
ErrorKind::InvalidValue,
|
|
"--output-format can only be used with --info, --status, or --list modes"
|
|
).exit();
|
|
}
|
|
}
|
|
|
|
// Validate human-readable usage
|
|
if settings.human_readable && mode != KeepModes::List && mode != KeepModes::Info {
|
|
cmd.error(
|
|
ErrorKind::InvalidValue,
|
|
"--human-readable can only be used with --list and --info modes"
|
|
).exit();
|
|
}
|
|
|
|
// Validate server password usage
|
|
if settings.server_password.is_some() && mode != KeepModes::Server {
|
|
cmd.error(
|
|
ErrorKind::InvalidValue,
|
|
"--server-password can only be used with --server mode"
|
|
).exit();
|
|
}
|
|
|
|
debug!("MAIN: args: {:?}", args);
|
|
debug!("MAIN: ids: {:?}", ids);
|
|
debug!("MAIN: tags: {:?}", tags);
|
|
debug!("MAIN: mode: {:?}", mode);
|
|
debug!("MAIN: settings: {:?}", settings);
|
|
|
|
unsafe {
|
|
libc::umask(0o077);
|
|
}
|
|
|
|
let data_path = settings.dir.clone();
|
|
let mut db_path = data_path.clone();
|
|
db_path.push("keep-1.db");
|
|
|
|
debug!("MAIN: Data directory: {:?}", data_path);
|
|
debug!("MAIN: DB file: {:?}", db_path);
|
|
|
|
fs::create_dir_all(data_path.clone()).context("Problem creating data directory")?;
|
|
debug!("MAIN: Data directory created or already exists");
|
|
|
|
let mut conn = db::open(db_path.clone()).context("Problem opening database")?;
|
|
debug!("MAIN: DB opened successfully");
|
|
|
|
match mode {
|
|
KeepModes::Save => {
|
|
crate::modes::save::mode_save(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
|
}
|
|
KeepModes::Get => {
|
|
crate::modes::get::mode_get(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
|
}
|
|
KeepModes::Diff => {
|
|
crate::modes::diff::mode_diff(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
|
}
|
|
KeepModes::List => {
|
|
crate::modes::list::mode_list(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
|
}
|
|
KeepModes::Update => {
|
|
crate::modes::update::mode_update(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
|
}
|
|
KeepModes::Info => {
|
|
crate::modes::info::mode_info(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
|
}
|
|
KeepModes::Delete => {
|
|
crate::modes::delete::mode_delete(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
|
}
|
|
KeepModes::Status => {
|
|
crate::modes::status::mode_status(&mut cmd, &settings, &config, data_path, db_path)?
|
|
}
|
|
KeepModes::Server => {
|
|
crate::modes::server::mode_server(&mut cmd, &settings, &config, &mut conn, data_path)?
|
|
}
|
|
KeepModes::Unknown => todo!(),
|
|
}
|
|
|
|
Ok(())
|
|
}
|