feat(core): launch daemon from launcher process

This commit is contained in:
Federico Terzi 2021-06-27 20:20:05 +02:00
parent dd4d93bc94
commit 5b547fec9f
4 changed files with 100 additions and 4 deletions

View File

@ -0,0 +1,70 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* espanso is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use std::process::Command;
use anyhow::Result;
use thiserror::Error;
use crate::cli::PathsOverrides;
pub fn launch_daemon(paths_overrides: &PathsOverrides) -> 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"]);
// We only inject the paths that were explicitly overrided because otherwise
// the migration process might create some incompatibilities.
// For example, after the migration the "packages" dir could have been
// moved to a different one, and that might cause the daemon to crash
// if we inject the old packages dir that was automatically resolved.
if let Some(config_override) = &paths_overrides.config {
command.env(
"ESPANSO_CONFIG_DIR",
config_override.to_string_lossy().to_string(),
);
}
if let Some(packages_override) = &paths_overrides.packages {
command.env(
"ESPANSO_PACKAGE_DIR",
packages_override.to_string_lossy().to_string(),
);
}
if let Some(runtime_override) = &paths_overrides.runtime {
command.env(
"ESPANSO_RUNTIME_DIR",
runtime_override.to_string_lossy().to_string(),
);
}
let mut child = command.spawn()?;
let result = child.wait()?;
if result.success() {
Ok(())
} else {
Err(DaemonError::NonZeroExitCode.into())
}
}
#[derive(Error, Debug)]
pub enum DaemonError {
#[error("unexpected error, 'espanso daemon' returned a non-zero exit code.")]
NonZeroExitCode,
}

View File

@ -23,6 +23,7 @@ use crate::preferences::Preferences;
use super::{CliModule, CliModuleArgs};
mod accessibility;
mod daemon;
mod util;
// TODO: test also with modulo feature disabled
@ -45,6 +46,7 @@ fn launcher_main(args: CliModuleArgs) -> i32 {
// 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");
let paths_overrides = args.paths_overrides.expect("missing paths overrides in launcher main");
let icon_paths = crate::icon::load_icon_paths(&paths.runtime).expect("unable to load icon paths");
let preferences =
@ -116,14 +118,14 @@ fn launcher_main(args: CliModuleArgs) -> i32 {
// This page could also be used when the user starts espanso, but an instance is already running.
// Only show the wizard if a panel should be displayed
if is_welcome_page_enabled
let should_launch_daemon = if is_welcome_page_enabled
|| is_move_bundle_page_enabled
|| is_legacy_version_page_enabled
|| is_migrate_page_enabled
|| is_add_path_page_enabled
|| is_accessibility_page_enabled
{
espanso_modulo::wizard::show(WizardOptions {
let successful = espanso_modulo::wizard::show(WizardOptions {
version: crate::VERSION.to_string(),
is_welcome_page_enabled,
is_move_bundle_page_enabled,
@ -152,10 +154,18 @@ fn launcher_main(args: CliModuleArgs) -> i32 {
on_completed: Some(on_completed_handler),
},
});
}
successful
} else {
true
};
// TODO: initialize config directory if not present
if should_launch_daemon {
daemon::launch_daemon(&paths_overrides).expect("failed to launch daemon");
}
0
}

View File

@ -17,6 +17,8 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use std::path::PathBuf;
use clap::ArgMatches;
use espanso_config::{config::ConfigStore, matches::store::MatchStore};
use espanso_path::Paths;
@ -66,6 +68,7 @@ pub struct CliModuleArgs {
pub match_store: Option<Box<dyn MatchStore>>,
pub is_legacy_config: bool,
pub paths: Option<Paths>,
pub paths_overrides: Option<PathsOverrides>,
pub cli_args: Option<ArgMatches<'static>>,
}
@ -76,7 +79,14 @@ impl Default for CliModuleArgs {
match_store: None,
is_legacy_config: false,
paths: None,
paths_overrides: None,
cli_args: None,
}
}
}
pub struct PathsOverrides {
pub config: Option<PathBuf>,
pub runtime: Option<PathBuf>,
pub packages: Option<PathBuf>,
}

View File

@ -33,7 +33,7 @@ use simplelog::{
CombinedLogger, ConfigBuilder, LevelFilter, SharedLogger, TermLogger, TerminalMode, WriteLogger,
};
use crate::cli::LogMode;
use crate::cli::{LogMode, PathsOverrides};
mod cli;
mod engine;
@ -390,6 +390,12 @@ fn main() {
force_runtime_path.as_deref(),
);
cli_args.paths_overrides = Some(PathsOverrides {
config: force_config_path,
packages: force_package_path,
runtime: force_runtime_path,
});
info!("reading configs from: {:?}", paths.config);
info!("reading packages from: {:?}", paths.packages);
info!("using runtime dir: {:?}", paths.runtime);