From 2eabdf90bf7c90675a81b58bbc0f427e4a5a31c3 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 10 Oct 2021 11:29:23 +0200 Subject: [PATCH] feat(core): port 'espanso status' subcommand. Fix #775 --- espanso/src/cli/service/mod.rs | 14 ++++++++++++++ espanso/src/main.rs | 14 ++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/espanso/src/cli/service/mod.rs b/espanso/src/cli/service/mod.rs index 0e710cb..c80e418 100644 --- a/espanso/src/cli/service/mod.rs +++ b/espanso/src/cli/service/mod.rs @@ -95,6 +95,8 @@ fn service_main(args: CliModuleArgs) -> i32 { return start_main(&paths, &paths_overrides, sub_args); } else if cli_args.subcommand_matches("stop").is_some() { return stop_main(&paths); + } else if cli_args.subcommand_matches("status").is_some() { + return status_main(&paths); } else if let Some(sub_args) = cli_args.subcommand_matches("restart") { stop_main(&paths); return start_main(&paths, &paths_overrides, sub_args); @@ -152,3 +154,15 @@ fn stop_main(paths: &Paths) -> i32 { SERVICE_SUCCESS } + +fn status_main(paths: &Paths) -> i32 { + let lock_file = acquire_worker_lock(&paths.runtime); + if lock_file.is_some() { + error_eprintln!("espanso is not running"); + return SERVICE_NOT_RUNNING; + } + drop(lock_file); + + info_println!("espanso is running"); + SERVICE_SUCCESS +} diff --git a/espanso/src/main.rs b/espanso/src/main.rs index 02b5671..d0897ed 100644 --- a/espanso/src/main.rs +++ b/espanso/src/main.rs @@ -85,6 +85,10 @@ lazy_static! { subcommand: "stop".to_owned(), forward_into: "service".to_owned(), }, + CliAlias { + subcommand: "status".to_owned(), + forward_into: "service".to_owned(), + }, CliAlias { subcommand: "install".to_owned(), forward_into: "package".to_owned(), @@ -171,6 +175,8 @@ fn main() { .about("Restart the espanso service") .name("restart"); let stop_subcommand = SubCommand::with_name("stop").about("Stop espanso service"); + let status_subcommand = + SubCommand::with_name("status").about("Check if the espanso daemon is running or not."); let mut clap_instance = App::new("espanso") .version(VERSION) @@ -299,12 +305,6 @@ For example, specifying 'email' is equivalent to 'match/email.yml'."#)) ), ), ) - // .subcommand(SubCommand::with_name("start") - // .about("Start the daemon spawning a new process in the background.")) - // .subcommand(SubCommand::with_name("stop") - // .about("Stop the espanso daemon.")) - // .subcommand(SubCommand::with_name("restart") - // .about("Restart the espanso daemon.")) // .subcommand(SubCommand::with_name("status") // .about("Check if the espanso daemon is running or not.")) .subcommand( @@ -346,11 +346,13 @@ For example, specifying 'email' is equivalent to 'match/email.yml'."#)) .subcommand(start_subcommand.clone()) .subcommand(restart_subcommand.clone()) .subcommand(stop_subcommand.clone()) + .subcommand(status_subcommand.clone()) .about("Register and manage 'espanso' as a system service."), ) .subcommand(start_subcommand) .subcommand(restart_subcommand) .subcommand(stop_subcommand) + .subcommand(status_subcommand) // .subcommand(SubCommand::with_name("match") // .about("List and execute matches from the CLI") // .subcommand(SubCommand::with_name("list")