feature: Create command system to handle actions

This commit is contained in:
Alexander Navarro 2025-05-11 17:09:23 -04:00
parent 7248388260
commit 71c11eaa84
3 changed files with 58 additions and 20 deletions

View file

@ -1,5 +1,5 @@
use clap::Parser;
use readwise_bulk_upload::config::Config;
use clap::{CommandFactory, Parser};
use readwise_bulk_upload::config::{Command, Config};
use readwise_bulk_upload::readwise::DocumentPayload;
use readwise_bulk_upload::sql::TaskManager;
use readwise_bulk_upload::{Error, Result};
@ -9,8 +9,9 @@ use figment::{Figment, providers::{Serialized, Env}};
#[tokio::main]
async fn main() -> Result<()> {
let cli = Config::parse();
let args: Config = Figment::new()
.merge(Serialized::defaults(Config::parse()))
.merge(Serialized::defaults(&cli))
.merge(Env::prefixed("APP_"))
.extract()?;
@ -18,18 +19,32 @@ async fn main() -> Result<()> {
.with_max_level(args.log_level())
.init();
let file = File::open(args.path()).map_err(|_| {
Error::Runtime(format!(
r#"The file "{}" could not be open"#,
args.path().display()
))
})?;
let documents: Vec<DocumentPayload> = serde_json::from_reader(file)?;
let task_manager = TaskManager::new().await?;
task_manager.load_tasks(documents).await?;
run(&cli.command).await?;
Ok(())
}
async fn run(command: &Command) -> Result<()> {
match command {
Command::LoadTasks { path } => {
let file = File::open(path).map_err(|_| {
Error::Runtime(format!(
r#"The file "{}" could not be open"#,
path.display()
))
})?;
let documents: Vec<DocumentPayload> = serde_json::from_reader(file)?;
let task_manager = TaskManager::new().await?;
task_manager.load_tasks(documents).await?;
}
Command::None => {
Config::command().print_help()?;
}
}
Ok(())
}