feat(core): implement restart subcommand

This commit is contained in:
Federico Terzi 2021-10-09 15:31:39 +02:00
parent 5a75a04d5a
commit 3c455b6d4b
2 changed files with 64 additions and 37 deletions

View File

@ -30,6 +30,8 @@ use crate::{
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
mod macos; mod macos;
use clap::ArgMatches;
use espanso_path::Paths;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
use macos::*; use macos::*;
@ -90,6 +92,18 @@ fn service_main(args: CliModuleArgs) -> i32 {
return SERVICE_NOT_REGISTERED; return SERVICE_NOT_REGISTERED;
} }
} else if let Some(sub_args) = cli_args.subcommand_matches("start") { } else if let Some(sub_args) = cli_args.subcommand_matches("start") {
return start_main(&paths, sub_args);
} else if cli_args.subcommand_matches("stop").is_some() {
return stop_main(&paths);
} else if let Some(sub_args) = cli_args.subcommand_matches("restart") {
stop_main(&paths);
return start_main(&paths, sub_args);
}
SERVICE_SUCCESS
}
fn start_main(paths: &Paths, args: &ArgMatches) -> i32 {
let lock_file = acquire_worker_lock(&paths.runtime); let lock_file = acquire_worker_lock(&paths.runtime);
if lock_file.is_none() { if lock_file.is_none() {
error_eprintln!("espanso is already running!"); error_eprintln!("espanso is already running!");
@ -97,7 +111,7 @@ fn service_main(args: CliModuleArgs) -> i32 {
} }
drop(lock_file); drop(lock_file);
if sub_args.is_present("unmanaged") && !cfg!(target_os = "windows") { if args.is_present("unmanaged") && !cfg!(target_os = "windows") {
// Unmanaged service // Unmanaged service
#[cfg(unix)] #[cfg(unix)]
{ {
@ -119,7 +133,11 @@ fn service_main(args: CliModuleArgs) -> i32 {
info_println!("espanso started correctly!"); info_println!("espanso started correctly!");
} }
} }
} else if cli_args.subcommand_matches("stop").is_some() {
SERVICE_SUCCESS
}
fn stop_main(paths: &Paths) -> i32 {
let lock_file = acquire_worker_lock(&paths.runtime); let lock_file = acquire_worker_lock(&paths.runtime);
if lock_file.is_some() { if lock_file.is_some() {
error_eprintln!("espanso is not running!"); error_eprintln!("espanso is not running!");
@ -131,7 +149,6 @@ fn service_main(args: CliModuleArgs) -> i32 {
error_eprintln!("unable to stop espanso: {}", err); error_eprintln!("unable to stop espanso: {}", err);
return SERVICE_FAILURE; return SERVICE_FAILURE;
} }
}
SERVICE_SUCCESS SERVICE_SUCCESS
} }

View File

@ -77,6 +77,10 @@ lazy_static! {
subcommand: "start".to_owned(), subcommand: "start".to_owned(),
forward_into: "service".to_owned(), forward_into: "service".to_owned(),
}, },
CliAlias {
subcommand: "restart".to_owned(),
forward_into: "service".to_owned(),
},
CliAlias { CliAlias {
subcommand: "stop".to_owned(), subcommand: "stop".to_owned(),
forward_into: "service".to_owned(), forward_into: "service".to_owned(),
@ -162,6 +166,10 @@ fn main() {
.takes_value(false) .takes_value(false)
.help("Run espanso as an unmanaged service (avoid system manager)"), .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 stop_subcommand = SubCommand::with_name("stop").about("Stop espanso service");
let mut clap_instance = App::new("espanso") 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"), .about("Check if espanso is registered as a system service"),
) )
.subcommand(start_subcommand.clone()) .subcommand(start_subcommand.clone())
.subcommand(restart_subcommand.clone())
.subcommand(stop_subcommand.clone()) .subcommand(stop_subcommand.clone())
.about("Register and manage 'espanso' as a system service."), .about("Register and manage 'espanso' as a system service."),
) )
.subcommand(start_subcommand) .subcommand(start_subcommand)
.subcommand(restart_subcommand)
.subcommand(stop_subcommand) .subcommand(stop_subcommand)
// .subcommand(SubCommand::with_name("match") // .subcommand(SubCommand::with_name("match")
// .about("List and execute matches from the CLI") // .about("List and execute matches from the CLI")