From 71c11eaa84988b5907017ca15d6ba696c5e0b6d7 Mon Sep 17 00:00:00 2001 From: aleidk Date: Sun, 11 May 2025 17:09:23 -0400 Subject: [PATCH] feature: Create command system to handle actions --- .idea/vcs.xml | 5 +++++ src/config.rs | 28 +++++++++++++++++++++++----- src/main.rs | 45 ++++++++++++++++++++++++++++++--------------- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 35eb1dd..8d3e42f 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,5 +1,10 @@ + + + diff --git a/src/config.rs b/src/config.rs index 680fdf7..3fb8d52 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use clap::{Parser, ValueEnum}; +use clap::{Parser, Subcommand, ValueEnum}; use serde::{Deserialize, Serialize}; use std::fmt; use std::path::PathBuf; @@ -57,9 +57,30 @@ impl Into for VerbosityLevel { } } +#[derive(Debug, Subcommand)] +#[clap(rename_all = "snake_case")] +pub enum Command { + /// Load task into the database from [path] + LoadTasks{ + /// Path to the file + path: PathBuf, + }, + #[clap(skip)] + None, +} + +impl Default for Command { + fn default() -> Self { + Command::None + } +} + #[derive(Debug, Parser, Serialize, Deserialize)] pub struct Config { - path: PathBuf, + #[command(subcommand)] + #[serde(skip)] + pub command: Command, + #[arg( long, short = 'v', @@ -71,9 +92,6 @@ pub struct Config { } impl Config { - pub fn path(&self) -> &PathBuf { - &self.path - } pub fn log_level(&self) -> LevelFilter { self.log_level.clone().into() diff --git a/src/main.rs b/src/main.rs index 985c0e4..b1fb8e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = 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 = serde_json::from_reader(file)?; + + let task_manager = TaskManager::new().await?; + + task_manager.load_tasks(documents).await?; + } + Command::None => { + Config::command().print_help()?; + } + } Ok(()) }