From f3090eebe912e201cd8ea2d4bcbcb60a837007ae Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 5 Aug 2022 17:58:54 +0300 Subject: [PATCH] cli: extract pause and start subcommands --- src/cli.rs | 9 +++----- src/cli/pause.rs | 19 +++++++++++++++++ src/cli/start.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 54 +++++++++--------------------------------------- 4 files changed, 84 insertions(+), 50 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 1a53729..8f55086 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -15,6 +15,8 @@ //! pub mod finish; +pub mod pause; +pub mod start; pub mod status; #[derive(clap::Parser)] @@ -52,12 +54,7 @@ pub enum SubCommand { priority: Priority, }, List, - Start { - #[clap(short, long)] - open: bool, - - idx: Option, - }, + Start(self::start::Args), Pause, Finish, Status, diff --git a/src/cli/pause.rs b/src/cli/pause.rs index e69de29..221a356 100644 --- a/src/cli/pause.rs +++ b/src/cli/pause.rs @@ -0,0 +1,19 @@ +use std::path::PathBuf; + +use crate::CurrentTaskInfo; + +pub struct Request { + pub current_task_info: Option, + pub current_task_info_file_path: PathBuf, +} + +pub fn execute(mut req: Request) { + if req.current_task_info.take().is_none() { + panic!("You can use the pause subcommand only when you have an active task"); + } + + let mut file = std::fs::File::create(&req.current_task_info_file_path).unwrap(); + file.write_all(&serde_json::to_vec(&req.current_task_info).unwrap()) + .unwrap(); + println!("paused"); +} diff --git a/src/cli/start.rs b/src/cli/start.rs index e69de29..43f6386 100644 --- a/src/cli/start.rs +++ b/src/cli/start.rs @@ -0,0 +1,52 @@ +use crate::{CurrentTaskInfo, Task}; +use std::{io::Write, path::PathBuf}; + +#[derive(clap::Args)] +pub struct Args { + #[clap(short, long)] + open: bool, + + idx: Option, +} + +pub struct Request { + args: Args, + tasks: Vec, + current_task_info: Option, + current_task_info_file_path: PathBuf, +} + +pub fn execute(mut req: Request) { + let idx = req.args.idx.unwrap_or(1); + if idx == 0 || idx > req.tasks.len() { + panic!("invalid index"); + } + let task = &req.tasks[idx - 1]; + + req.current_task_info.replace(CurrentTaskInfo { + task_idx: idx, + task: task.clone(), + }); + + let mut file = std::fs::File::create(&req.current_task_info_file_path).unwrap(); + file.write_all(&serde_json::to_vec(&req.current_task_info).unwrap()) + .unwrap(); + println!("started"); + + if let (Some(link), true) = (task.link.as_ref(), req.args.open) { + log::debug!("opening link..."); + std::process::Command::new("xdg-open") + .arg(link) + .spawn() + .expect("failed to start"); + log::debug!("opened"); + } + + /* + println!("starting..."); + println!("-- on start hook found"); + println!("-- running..."); + println!("> curl ..."); + println!("-- status was changed to \"In progress\" successfuly"); + */ +} diff --git a/src/main.rs b/src/main.rs index 8e16d5a..b3eb303 100644 --- a/src/main.rs +++ b/src/main.rs @@ -188,50 +188,16 @@ fn main() { file.write_all(&serde_json::to_vec(&tasks).unwrap()) .unwrap(); } - cli::SubCommand::Start { idx, open } => { - let idx = idx.unwrap_or(1); - if idx == 0 || idx > tasks.len() { - panic!("invalid index"); - } - let task = &tasks[idx - 1]; - - current_task_info.replace(CurrentTaskInfo { - task_idx: idx, - task: task.clone(), - }); - - let mut file = std::fs::File::create(¤t_task_info_file_path).unwrap(); - file.write_all(&serde_json::to_vec(¤t_task_info).unwrap()) - .unwrap(); - println!("started"); - - if let (Some(link), true) = (task.link.as_ref(), open) { - log::debug!("opening link..."); - std::process::Command::new("xdg-open") - .arg(link) - .spawn() - .expect("failed to start"); - log::debug!("opened"); - } - - /* - println!("starting..."); - println!("-- on start hook found"); - println!("-- running..."); - println!("> curl ..."); - println!("-- status was changed to \"In progress\" successfuly"); - */ - } - cli::SubCommand::Pause => { - if current_task_info.take().is_none() { - panic!("You can use the pause subcommand only when you have an active task"); - } - - let mut file = std::fs::File::create(¤t_task_info_file_path).unwrap(); - file.write_all(&serde_json::to_vec(¤t_task_info).unwrap()) - .unwrap(); - println!("paused"); - } + cli::SubCommand::Start(args) => cli::start::execute(cli::start::Request { + args, + tasks, + current_task_info, + current_task_info_file_path, + }), + cli::SubCommand::Pause => cli::pause::execute(cli::pause::Request { + current_task_info, + current_task_info_file_path, + }), cli::SubCommand::Finish => cli::finish::execute(cli::finish::Request { tasks, current_task_info,