diff --git a/Cargo.lock b/Cargo.lock index 12b188e..3c424bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -279,6 +279,27 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" +[[package]] +name = "cli" +version = "0.1.0" +dependencies = [ + "chrono", + "clap", + "directories", + "figment", + "futures", + "lib_sync_core", + "serde", + "serde_json", + "sqlx", + "tabled", + "thiserror", + "tokio", + "tracing", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "colorchoice" version = "1.0.3" @@ -1341,27 +1362,6 @@ dependencies = [ "getrandom 0.2.16", ] -[[package]] -name = "readwise-bulk-upload" -version = "0.1.0" -dependencies = [ - "chrono", - "clap", - "directories", - "figment", - "futures", - "lib_sync_core", - "serde", - "serde_json", - "sqlx", - "tabled", - "thiserror", - "tokio", - "tracing", - "tracing-core", - "tracing-subscriber", -] - [[package]] name = "redox_syscall" version = "0.5.12" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 410a10d..c056a52 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,8 +1,12 @@ [package] -name = "readwise-bulk-upload" +name = "cli" version = "0.1.0" edition = "2024" +[[bin]] +name = "readwise" +path = "bin/readwise/main.rs" + [dependencies] lib_sync_core = {path = "../lib_sync_core"} diff --git a/cli/src/readwise.rs b/cli/bin/readwise/external_interface.rs similarity index 100% rename from cli/src/readwise.rs rename to cli/bin/readwise/external_interface.rs diff --git a/cli/bin/readwise/main.rs b/cli/bin/readwise/main.rs new file mode 100644 index 0000000..bdfc584 --- /dev/null +++ b/cli/bin/readwise/main.rs @@ -0,0 +1,76 @@ +use clap::{CommandFactory, Parser}; +use directories::ProjectDirs; +use figment::{ + Figment, + providers::{Env, Serialized}, +}; +use lib_sync_core::task_manager::{TaskManager, TaskStatus}; +use cli::config::{Command, Config}; +use cli::{Error, Result}; +use std::fs::File; +use tabled::Table; +use tracing_subscriber; +use crate::external_interface::DocumentPayload; + +mod external_interface; + +#[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 project_dir = ProjectDirs::from("", "", "synchronizator_readwise").ok_or( + lib_sync_core::error::Error::Unhandled("Could not get standard directories"), + )?; + + let task_manager = TaskManager::new(project_dir.data_dir()).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 = serde_json::from_reader(file)?; + + task_manager.load_tasks(documents).await?; + } + Command::Query => { + let tasks = task_manager + .get_tasks::(None, Some(25)) + .await?; + + println!("{}", Table::new(tasks)); + } + Command::Run => { + task_manager + .run_tasks::(|task| { + println!("{}", task.get_key()); + + TaskStatus::Completed + }) + .await?; + } + Command::None => { + Config::command().print_help()?; + } + } + + Ok(()) +} diff --git a/cli/src/lib.rs b/cli/src/lib.rs index acba89d..dabf39f 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1,5 +1,4 @@ pub mod config; -pub mod readwise; mod error; pub use error::*; \ No newline at end of file diff --git a/cli/src/main.rs b/cli/src/main.rs index 23acee5..75c0be7 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,15 +1,10 @@ -use clap::{CommandFactory, Parser}; +use clap::Parser; use figment::{ providers::{Env, Serialized}, Figment, }; -use readwise_bulk_upload::config::{Command, Config}; -use readwise_bulk_upload::readwise::DocumentPayload; -use lib_sync_core::task_manager::{TaskManager, TaskStatus}; -use readwise_bulk_upload::{Error, Result}; -use std::fs::File; -use directories::ProjectDirs; -use tabled::Table; +use cli::config::Config; +use cli::Result; use tracing_subscriber; #[tokio::main] @@ -24,47 +19,5 @@ async fn main() -> Result<()> { .with_max_level(args.log_level()) .init(); - run(&cli.command).await?; - - Ok(()) -} - -async fn run(command: &Command) -> Result<()> { - let project_dir = ProjectDirs::from("", "", env!("CARGO_PKG_NAME")) - .ok_or(lib_sync_core::error::Error::Unhandled("Could not get standard directories"))?; - - let task_manager = TaskManager::new(project_dir.data_dir()).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 = serde_json::from_reader(file)?; - - - task_manager.load_tasks(documents).await?; - } - Command::Query => { - let tasks = task_manager.get_tasks::(None, Some(25)).await?; - - println!("{}", Table::new(tasks)); - } - Command::Run => { - task_manager.run_tasks::(|task| { - println!("{}", task.get_key()); - - TaskStatus::Completed - }).await?; - } - Command::None => { - Config::command().print_help()?; - } - } - Ok(()) }