cli: extract pause and start subcommands
This commit is contained in:
parent
4abf8a9341
commit
f3090eebe9
4 changed files with 84 additions and 50 deletions
|
@ -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<usize>,
|
||||
},
|
||||
Start(self::start::Args),
|
||||
Pause,
|
||||
Finish,
|
||||
Status,
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use crate::CurrentTaskInfo;
|
||||
|
||||
pub struct Request {
|
||||
pub current_task_info: Option<CurrentTaskInfo>,
|
||||
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");
|
||||
}
|
|
@ -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<usize>,
|
||||
}
|
||||
|
||||
pub struct Request {
|
||||
args: Args,
|
||||
tasks: Vec<Task>,
|
||||
current_task_info: Option<CurrentTaskInfo>,
|
||||
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");
|
||||
*/
|
||||
}
|
54
src/main.rs
54
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,
|
||||
|
|
Loading…
Reference in a new issue