extract-cli #9

Merged
pleshevskiy merged 7 commits from extract-cli into main 2022-08-05 22:07:16 +03:00
4 changed files with 84 additions and 50 deletions
Showing only changes of commit f3090eebe9 - Show all commits

View file

@ -15,6 +15,8 @@
//! //!
pub mod finish; pub mod finish;
pub mod pause;
pub mod start;
pub mod status; pub mod status;
#[derive(clap::Parser)] #[derive(clap::Parser)]
@ -52,12 +54,7 @@ pub enum SubCommand {
priority: Priority, priority: Priority,
}, },
List, List,
Start { Start(self::start::Args),
#[clap(short, long)]
open: bool,
idx: Option<usize>,
},
Pause, Pause,
Finish, Finish,
Status, Status,

View file

@ -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");
}

View file

@ -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");
*/
}

View file

@ -188,50 +188,16 @@ fn main() {
file.write_all(&serde_json::to_vec(&tasks).unwrap()) file.write_all(&serde_json::to_vec(&tasks).unwrap())
.unwrap(); .unwrap();
} }
cli::SubCommand::Start { idx, open } => { cli::SubCommand::Start(args) => cli::start::execute(cli::start::Request {
let idx = idx.unwrap_or(1); args,
if idx == 0 || idx > tasks.len() { tasks,
panic!("invalid index"); current_task_info,
} current_task_info_file_path,
let task = &tasks[idx - 1]; }),
cli::SubCommand::Pause => cli::pause::execute(cli::pause::Request {
current_task_info.replace(CurrentTaskInfo { current_task_info,
task_idx: idx, current_task_info_file_path,
task: task.clone(), }),
});
let mut file = std::fs::File::create(&current_task_info_file_path).unwrap();
file.write_all(&serde_json::to_vec(&current_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(&current_task_info_file_path).unwrap();
file.write_all(&serde_json::to_vec(&current_task_info).unwrap())
.unwrap();
println!("paused");
}
cli::SubCommand::Finish => cli::finish::execute(cli::finish::Request { cli::SubCommand::Finish => cli::finish::execute(cli::finish::Request {
tasks, tasks,
current_task_info, current_task_info,