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

5
.idea/vcs.xml generated
View file

@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GitSharedSettings">
<option name="FORCE_PUSH_PROHIBITED_PATTERNS">
<list />
</option>
</component>
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" /> <mapping directory="" vcs="Git" />
</component> </component>

View file

@ -1,4 +1,4 @@
use clap::{Parser, ValueEnum}; use clap::{Parser, Subcommand, ValueEnum};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use std::path::PathBuf; use std::path::PathBuf;
@ -57,9 +57,30 @@ impl Into<LevelFilter> 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)] #[derive(Debug, Parser, Serialize, Deserialize)]
pub struct Config { pub struct Config {
path: PathBuf, #[command(subcommand)]
#[serde(skip)]
pub command: Command,
#[arg( #[arg(
long, long,
short = 'v', short = 'v',
@ -71,9 +92,6 @@ pub struct Config {
} }
impl Config { impl Config {
pub fn path(&self) -> &PathBuf {
&self.path
}
pub fn log_level(&self) -> LevelFilter { pub fn log_level(&self) -> LevelFilter {
self.log_level.clone().into() self.log_level.clone().into()

View file

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