mod commands;
mod config;
mod storage;
mod tui;
use clap::{Parser, Subcommand};
use commands::{save, search};
use std::process;
#[derive(Parser, Debug)]
#[command(version,about,long_about=None)]
struct Args {
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
Search {
pattern: String,
},
Save {
val: String,
},
}
fn main() {
let db_path_buf = config::get_database_path();
let db_path_str = db_path_buf.to_str().expect("Path contains invalid unicode");
let mut posts = storage::read_it(db_path_str).unwrap_or_else(|err| {
eprintln!("unable to locate/create jsonfile {:?}", err);
process::exit(1)
});
let args = Args::parse();
match args.command {
Command::Search { pattern } => {
search::execute(&pattern, &posts).unwrap();
}
Command::Save { val } => {
save::execute(&val, &mut posts, db_path_str).unwrap_or_else(|err| {
eprintln!(
"unable to add the command to the database , error: {:?}",
err
)
})
}
}
}