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"?>
|
<?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>
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
|
|
||||||
45
src/main.rs
45
src/main.rs
|
|
@ -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,18 +19,32 @@ 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?;
|
||||||
Error::Runtime(format!(
|
|
||||||
r#"The file "{}" could not be open"#,
|
Ok(())
|
||||||
args.path().display()
|
}
|
||||||
))
|
|
||||||
})?;
|
async fn run(command: &Command) -> Result<()> {
|
||||||
|
|
||||||
let documents: Vec<DocumentPayload> = serde_json::from_reader(file)?;
|
match command {
|
||||||
|
Command::LoadTasks { path } => {
|
||||||
let task_manager = TaskManager::new().await?;
|
let file = File::open(path).map_err(|_| {
|
||||||
|
Error::Runtime(format!(
|
||||||
task_manager.load_tasks(documents).await?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue