feature: Create command system to handle actions
This commit is contained in:
parent
7248388260
commit
71c11eaa84
3 changed files with 58 additions and 20 deletions
5
.idea/vcs.xml
generated
5
.idea/vcs.xml
generated
|
|
@ -1,5 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GitSharedSettings">
|
||||
<option name="FORCE_PUSH_PROHIBITED_PATTERNS">
|
||||
<list />
|
||||
</option>
|
||||
</component>
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
|
|
|
|||
|
|
@ -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<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)]
|
||||
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()
|
||||
|
|
|
|||
45
src/main.rs
45
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<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(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue