refactor: move library into it's own crate
This commit is contained in:
parent
91d702088d
commit
b31502fb37
13 changed files with 138 additions and 34 deletions
65
src/main.rs
65
src/main.rs
|
|
@ -1,65 +0,0 @@
|
|||
use clap::{CommandFactory, Parser};
|
||||
use figment::{
|
||||
providers::{Env, Serialized},
|
||||
Figment,
|
||||
};
|
||||
use readwise_bulk_upload::config::{Command, Config};
|
||||
use readwise_bulk_upload::readwise::DocumentPayload;
|
||||
use readwise_bulk_upload::task_manager::{TaskManager, TaskStatus};
|
||||
use readwise_bulk_upload::{Error, Result};
|
||||
use std::fs::File;
|
||||
use tabled::Table;
|
||||
use tracing_subscriber;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let cli = Config::parse();
|
||||
let args: Config = Figment::new()
|
||||
.merge(Serialized::defaults(&cli))
|
||||
.merge(Env::prefixed("APP_"))
|
||||
.extract()?;
|
||||
|
||||
tracing_subscriber::fmt()
|
||||
.with_max_level(args.log_level())
|
||||
.init();
|
||||
|
||||
run(&cli.command).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn run(command: &Command) -> Result<()> {
|
||||
let task_manager = TaskManager::new().await?;
|
||||
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)?;
|
||||
|
||||
|
||||
task_manager.load_tasks(documents).await?;
|
||||
}
|
||||
Command::Query => {
|
||||
let tasks = task_manager.get_tasks::<DocumentPayload>(None, Some(25)).await?;
|
||||
|
||||
println!("{}", Table::new(tasks));
|
||||
}
|
||||
Command::Run => {
|
||||
task_manager.run_tasks::<DocumentPayload>(|task| {
|
||||
println!("{}", task.get_key());
|
||||
|
||||
TaskStatus::Completed
|
||||
}).await?;
|
||||
}
|
||||
Command::None => {
|
||||
Config::command().print_help()?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue