Add automatic Systemd service path update after changing espanso path (for example after an update). Fix #199
This commit is contained in:
parent
1db3aee017
commit
f95457f769
17
src/main.rs
17
src/main.rs
|
@ -47,6 +47,7 @@ use crate::package::{PackageManager, InstallResult, UpdateResult, RemoveResult,
|
|||
use std::sync::atomic::AtomicBool;
|
||||
use crate::package::git::GitPackageResolver;
|
||||
use crate::package::zip::ZipPackageResolver;
|
||||
use crate::sysdaemon::{verify, VerifyResult};
|
||||
|
||||
mod ui;
|
||||
mod edit;
|
||||
|
@ -452,10 +453,17 @@ fn start_daemon(config_set: ConfigSet) {
|
|||
|
||||
if config_set.default.use_system_agent && !force_unmanaged {
|
||||
// Make sure espanso is currently registered in systemd
|
||||
let res = Command::new("systemctl")
|
||||
.args(&["--user", "is-enabled", "espanso.service"])
|
||||
.output();
|
||||
if !res.unwrap().status.success() {
|
||||
let res = verify();
|
||||
match res {
|
||||
VerifyResult::EnabledAndValid => {
|
||||
// Do nothing, everything is ok!
|
||||
},
|
||||
VerifyResult::EnabledButInvalidPath => {
|
||||
eprintln!("Updating espanso service file with new path...");
|
||||
unregister_main(config_set.clone());
|
||||
register_main(config_set);
|
||||
},
|
||||
VerifyResult::NotEnabled => {
|
||||
use dialoguer::Confirmation;
|
||||
if Confirmation::new()
|
||||
.with_text("espanso must be registered to systemd (user level) first. Do you want to proceed?")
|
||||
|
@ -471,6 +479,7 @@ fn start_daemon(config_set: ConfigSet) {
|
|||
|
||||
std::process::exit(4);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// Start the espanso service
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
// This functions are used to register/unregister espanso from the system daemon manager.
|
||||
|
||||
use crate::config::ConfigSet;
|
||||
use crate::sysdaemon::VerifyResult::{EnabledAndValid, NotEnabled, EnabledButInvalidPath};
|
||||
|
||||
// INSTALLATION
|
||||
|
||||
|
@ -181,6 +182,54 @@ pub fn register(config_set: ConfigSet) {
|
|||
}
|
||||
}
|
||||
|
||||
pub enum VerifyResult {
|
||||
EnabledAndValid,
|
||||
EnabledButInvalidPath,
|
||||
NotEnabled,
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn verify() -> VerifyResult {
|
||||
use regex::Regex;
|
||||
use std::process::{Command, ExitStatus};
|
||||
|
||||
// Check if espanso service is already registered
|
||||
let res = Command::new("systemctl")
|
||||
.args(&["--user", "is-enabled", "espanso"])
|
||||
.output();
|
||||
if let Ok(res) = res {
|
||||
let output = String::from_utf8_lossy(res.stdout.as_slice());
|
||||
let output = output.trim();
|
||||
if !res.status.success() || output != "enabled" {
|
||||
return NotEnabled
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref ExecPathRegex: Regex = Regex::new("ExecStart=(?P<path>.*?)\\s").unwrap();
|
||||
}
|
||||
|
||||
// Check if the currently registered path is valid
|
||||
let res = Command::new("systemctl")
|
||||
.args(&["--user", "cat", "espanso"])
|
||||
.output();
|
||||
if let Ok(res) = res {
|
||||
let output = String::from_utf8_lossy(res.stdout.as_slice());
|
||||
let output = output.trim();
|
||||
if res.status.success() {
|
||||
let caps = ExecPathRegex.captures(output).unwrap();
|
||||
let path = caps.get(1).map_or("", |m| m.as_str());
|
||||
let espanso_path = std::env::current_exe().expect("Could not get espanso executable path");
|
||||
|
||||
if espanso_path.to_string_lossy() != path {
|
||||
return EnabledButInvalidPath
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EnabledAndValid
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn unregister(config_set: ConfigSet) {
|
||||
use std::process::{Command, ExitStatus};
|
||||
|
|
Loading…
Reference in New Issue
Block a user