From 3c455b6d4b466e3aa4f1ef17ab83437ebaea04e2 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sat, 9 Oct 2021 15:31:39 +0200 Subject: [PATCH] feat(core): implement restart subcommand --- espanso/src/cli/service/mod.rs | 91 ++++++++++++++++++++-------------- espanso/src/main.rs | 10 ++++ 2 files changed, 64 insertions(+), 37 deletions(-) diff --git a/espanso/src/cli/service/mod.rs b/espanso/src/cli/service/mod.rs index eb250df..ea590e0 100644 --- a/espanso/src/cli/service/mod.rs +++ b/espanso/src/cli/service/mod.rs @@ -30,6 +30,8 @@ use crate::{ #[cfg(target_os = "macos")] mod macos; +use clap::ArgMatches; +use espanso_path::Paths; #[cfg(target_os = "macos")] use macos::*; @@ -90,48 +92,63 @@ fn service_main(args: CliModuleArgs) -> i32 { return SERVICE_NOT_REGISTERED; } } else if let Some(sub_args) = cli_args.subcommand_matches("start") { - let lock_file = acquire_worker_lock(&paths.runtime); - if lock_file.is_none() { - error_eprintln!("espanso is already running!"); - return SERVICE_ALREADY_RUNNING; - } - drop(lock_file); - - if sub_args.is_present("unmanaged") && !cfg!(target_os = "windows") { - // Unmanaged service - #[cfg(unix)] - { - if let Err(err) = fork_daemon(&paths_overrides) { - error_eprintln!("unable to start service (unmanaged): {}", err); - return SERVICE_FAILURE; - } - } - #[cfg(windows)] - { - unreachable!(); - } - } else { - // Managed service - if let Err(err) = start_service() { - error_eprintln!("unable to start service: {}", err); - return SERVICE_FAILURE; - } else { - info_println!("espanso started correctly!"); - } - } + return start_main(&paths, sub_args); } else if cli_args.subcommand_matches("stop").is_some() { - 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); + return stop_main(&paths); + } else if let Some(sub_args) = cli_args.subcommand_matches("restart") { + stop_main(&paths); + return start_main(&paths, sub_args); + } - if let Err(err) = stop::terminate_worker(&paths.runtime) { - error_eprintln!("unable to stop espanso: {}", err); + SERVICE_SUCCESS +} + +fn start_main(paths: &Paths, args: &ArgMatches) -> i32 { + let lock_file = acquire_worker_lock(&paths.runtime); + if lock_file.is_none() { + error_eprintln!("espanso is already running!"); + return SERVICE_ALREADY_RUNNING; + } + drop(lock_file); + + if args.is_present("unmanaged") && !cfg!(target_os = "windows") { + // Unmanaged service + #[cfg(unix)] + { + if let Err(err) = fork_daemon(&paths_overrides) { + error_eprintln!("unable to start service (unmanaged): {}", err); + return SERVICE_FAILURE; + } + } + #[cfg(windows)] + { + unreachable!(); + } + } else { + // Managed service + if let Err(err) = start_service() { + error_eprintln!("unable to start service: {}", err); return SERVICE_FAILURE; + } else { + info_println!("espanso started correctly!"); } } SERVICE_SUCCESS } + +fn stop_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); + + if let Err(err) = stop::terminate_worker(&paths.runtime) { + error_eprintln!("unable to stop espanso: {}", err); + return SERVICE_FAILURE; + } + + SERVICE_SUCCESS +} diff --git a/espanso/src/main.rs b/espanso/src/main.rs index 4ea4050..02b5671 100644 --- a/espanso/src/main.rs +++ b/espanso/src/main.rs @@ -77,6 +77,10 @@ lazy_static! { subcommand: "start".to_owned(), forward_into: "service".to_owned(), }, + CliAlias { + subcommand: "restart".to_owned(), + forward_into: "service".to_owned(), + }, CliAlias { subcommand: "stop".to_owned(), forward_into: "service".to_owned(), @@ -162,6 +166,10 @@ fn main() { .takes_value(false) .help("Run espanso as an unmanaged service (avoid system manager)"), ); + let restart_subcommand = start_subcommand + .clone() + .about("Restart the espanso service") + .name("restart"); let stop_subcommand = SubCommand::with_name("stop").about("Stop espanso service"); let mut clap_instance = App::new("espanso") @@ -336,10 +344,12 @@ For example, specifying 'email' is equivalent to 'match/email.yml'."#)) .about("Check if espanso is registered as a system service"), ) .subcommand(start_subcommand.clone()) + .subcommand(restart_subcommand.clone()) .subcommand(stop_subcommand.clone()) .about("Register and manage 'espanso' as a system service."), ) .subcommand(start_subcommand) + .subcommand(restart_subcommand) .subcommand(stop_subcommand) // .subcommand(SubCommand::with_name("match") // .about("List and execute matches from the CLI")