From 6748cbb183e8cbb19af0a5f037a25887d878f487 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Tue, 16 Aug 2022 16:17:26 +0300 Subject: [PATCH] cli/finish: add --open argument Closes #15 --- src/cli.rs | 2 +- src/cli/finish.rs | 37 ++++++++++++++++++++++++++----------- src/main.rs | 4 ++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 5ee785e..1d31687 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -40,6 +40,6 @@ pub enum SubCommand { List, Start(self::start::Args), Pause, - Finish, + Finish(self::finish::Args), Status, } diff --git a/src/cli/finish.rs b/src/cli/finish.rs index 451b782..f628be2 100644 --- a/src/cli/finish.rs +++ b/src/cli/finish.rs @@ -1,17 +1,32 @@ use crate::repo::{self, Repository}; -pub fn execute(repo: impl Repository) { - match repo.finish_task() { +#[derive(clap::Args)] +pub struct Args { + #[clap(short, long)] + open: bool, +} + +pub fn execute(repo: impl Repository, args: Args) { + let task = match repo.finish_task() { Err(repo::Error::NotFound) => { - eprintln!("You can use the finish subcommand only when you have an active task") - } - Err(err) => eprintln!("Cannot finish the task: {}", err), - Ok(task) => { - println!("The task was finished successfully"); - println!(" {}", task.name); - if let Some(link) = task.link { - println!(" link: {}", link); - } + return eprintln!("You can use the finish subcommand only when you have an active task") } + Err(err) => return eprintln!("Cannot finish the task: {}", err), + Ok(task) => task, + }; + + println!("The task was finished successfully"); + println!(" {}", task.name); + if let Some(link) = task.link.as_ref() { + println!(" link: {}", link); + } + + if let (Some(link), true) = (task.link.as_ref(), args.open) { + log::debug!("opening link..."); + std::process::Command::new("xdg-open") + .arg(link) + .spawn() + .expect("failed to start"); + log::debug!("opened"); } } diff --git a/src/main.rs b/src/main.rs index 5d1cd75..de94339 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,8 +62,8 @@ fn main() { cli::SubCommand::Pause => { cli::pause::execute(repo); } - cli::SubCommand::Finish => { - cli::finish::execute(repo); + cli::SubCommand::Finish(args) => { + cli::finish::execute(repo, args); } cli::SubCommand::Status => { cli::status::execute(repo);