feat(core): show already running warning if multiple instances are present

This commit is contained in:
Federico Terzi 2021-07-23 22:15:18 +02:00
parent 56502fd0e5
commit e9d90929a6
7 changed files with 46 additions and 10 deletions

View File

@ -25,10 +25,14 @@ use thiserror::Error;
use crate::cli::PathsOverrides;
use crate::cli::util::CommandExt;
pub fn launch_daemon(paths_overrides: &PathsOverrides) -> Result<()> {
pub fn launch_daemon(paths_overrides: &PathsOverrides, show_welcome: bool) -> Result<()> {
let espanso_exe_path = std::env::current_exe()?;
let mut command = Command::new(&espanso_exe_path.to_string_lossy().to_string());
command.args(&["daemon", "--show-welcome"]);
let mut args = vec!["daemon"];
if show_welcome {
args.push("--show-welcome");
}
command.args(&args);
command.with_paths_overrides(paths_overrides);
let mut child = command.spawn()?;

View File

@ -18,7 +18,7 @@
*/
use self::util::MigrationError;
use crate::exit_code::{LAUNCHER_CONFIG_DIR_POPULATION_FAILURE, LAUNCHER_SUCCESS};
use crate::{exit_code::{LAUNCHER_ALREADY_RUNNING, LAUNCHER_CONFIG_DIR_POPULATION_FAILURE, LAUNCHER_SUCCESS}, lock::acquire_daemon_lock};
use crate::preferences::Preferences;
use log::error;
@ -45,10 +45,18 @@ pub fn new() -> CliModule {
#[cfg(feature = "modulo")]
fn launcher_main(args: CliModuleArgs) -> i32 {
use espanso_modulo::wizard::{MigrationResult, WizardHandlers, WizardOptions};
let paths = args.paths.expect("missing paths in launcher main");
// TODO: should we create a non-gui wizard? We can also use it for the non-modulo versions of espanso
let paths = args.paths.expect("missing paths in launcher main");
// If espanso is already running, show a warning
let lock_file = acquire_daemon_lock(&paths.runtime);
if lock_file.is_none() {
util::show_already_running_warning().expect("unable to show already running warning");
return LAUNCHER_ALREADY_RUNNING;
}
drop(lock_file);
let paths_overrides = args
.paths_overrides
.expect("missing paths overrides in launcher main");
@ -56,6 +64,7 @@ fn launcher_main(args: CliModuleArgs) -> i32 {
let preferences =
crate::preferences::get_default(&paths.runtime).expect("unable to initialize preferences");
let is_first_start = !preferences.has_completed_wizard();
let is_welcome_page_enabled = !preferences.has_completed_wizard();
@ -175,7 +184,7 @@ fn launcher_main(args: CliModuleArgs) -> i32 {
espanso_mac_utils::convert_to_background_app();
}
daemon::launch_daemon(&paths_overrides).expect("failed to launch daemon");
daemon::launch_daemon(&paths_overrides, is_first_start).expect("failed to launch daemon");
}
LAUNCHER_SUCCESS

View File

@ -96,4 +96,14 @@ pub fn add_espanso_to_path() -> Result<()> {
pub enum AddToPathError {
#[error("unexpected error, 'espanso env-path register' returned a non-zero exit code.")]
NonZeroExitCode,
}
}
pub fn show_already_running_warning() -> Result<()> {
let espanso_exe_path = std::env::current_exe()?;
let mut command = Command::new(&espanso_exe_path.to_string_lossy().to_string());
command.args(&["modulo", "welcome", "--already-running"]);
let mut child = command.spawn()?;
child.wait()?;
Ok(())
}

View File

@ -53,8 +53,8 @@ fn modulo_main(args: CliModuleArgs) -> i32 {
return search::search_main(matches, &icon_paths);
}
if let Some(_) = cli_args.subcommand_matches("welcome") {
return welcome::welcome_main(&paths, &icon_paths);
if let Some(matches) = cli_args.subcommand_matches("welcome") {
return welcome::welcome_main(matches, &paths, &icon_paths);
}
if let Some(_) = cli_args.subcommand_matches("troubleshoot") {

View File

@ -17,12 +17,13 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use clap::{ArgMatches};
use crate::icon::IconPaths;
use crate::preferences::Preferences;
use espanso_modulo::welcome::*;
use espanso_path::Paths;
pub fn welcome_main(paths: &Paths, icon_paths: &IconPaths) -> i32 {
pub fn welcome_main(matches: &ArgMatches, paths: &Paths, icon_paths: &IconPaths) -> i32 {
let preferences =
crate::preferences::get_default(&paths.runtime).expect("unable to initialize preferences");
@ -30,6 +31,8 @@ pub fn welcome_main(paths: &Paths, icon_paths: &IconPaths) -> i32 {
preferences.set_should_display_welcome(!dont_show);
});
let is_already_running = matches.is_present("already-running");
espanso_modulo::welcome::show(WelcomeOptions{
window_icon_path: icon_paths
.wizard_icon
@ -39,6 +42,7 @@ pub fn welcome_main(paths: &Paths, icon_paths: &IconPaths) -> i32 {
.tray_explain_image
.as_ref()
.map(|path| path.to_string_lossy().to_string()),
is_already_running,
handlers: WelcomeHandlers {
dont_show_again_changed: Some(dont_show_again_handler),
},

View File

@ -43,6 +43,7 @@ pub const ADD_TO_PATH_FAILURE: i32 = 1;
pub const LAUNCHER_SUCCESS: i32 = 0;
pub const LAUNCHER_CONFIG_DIR_POPULATION_FAILURE: i32 = 1;
pub const LAUNCHER_ALREADY_RUNNING: i32 = 2;
pub const SERVICE_SUCCESS: i32 = 0;
pub const SERVICE_FAILURE: i32 = 1;

View File

@ -222,7 +222,15 @@ fn main() {
),
)
.subcommand(SubCommand::with_name("troubleshoot").about("Display the troubleshooting GUI"))
.subcommand(SubCommand::with_name("welcome").about("Display the welcome screen")),
.subcommand(
SubCommand::with_name("welcome")
.about("Display the welcome screen")
.arg(
Arg::with_name("already-running")
.long("already-running")
.takes_value(false),
),
),
)
// .subcommand(SubCommand::with_name("start")
// .about("Start the daemon spawning a new process in the background."))