This commit is contained in:
Sefa Eyeoglu 2022-09-24 09:12:50 +00:00 committed by GitHub
commit a022bdddf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,7 +27,7 @@ use thiserror::Error;
use crate::{error_eprintln, info_println, warn_eprintln}; use crate::{error_eprintln, info_println, warn_eprintln};
const LINUX_SERVICE_NAME: &str = "espanso"; const LINUX_SERVICE_NAME: &str = "espanso.service";
const LINUX_SERVICE_CONTENT: &str = include_str!("../../res/linux/systemd.service"); const LINUX_SERVICE_CONTENT: &str = include_str!("../../res/linux/systemd.service");
#[allow(clippy::transmute_bytes_to_str)] #[allow(clippy::transmute_bytes_to_str)]
const LINUX_SERVICE_FILENAME: &str = formatcp!("{}.service", LINUX_SERVICE_NAME); const LINUX_SERVICE_FILENAME: &str = formatcp!("{}.service", LINUX_SERVICE_NAME);
@ -35,6 +35,10 @@ const LINUX_SERVICE_FILENAME: &str = formatcp!("{}.service", LINUX_SERVICE_NAME)
pub fn register() -> Result<()> { pub fn register() -> Result<()> {
let service_file = get_service_file_path()?; let service_file = get_service_file_path()?;
if !service_file.exists() && is_present() {
// probably installed by system package
info_println!("skipping installation of already provided service");
} else {
if service_file.exists() { if service_file.exists() {
warn_eprintln!("service file already exists, this operation will overwrite it"); warn_eprintln!("service file already exists, this operation will overwrite it");
} }
@ -46,6 +50,7 @@ pub fn register() -> Result<()> {
.replace("{{{espanso_path}}}", &espanso_path.to_string_lossy()); .replace("{{{espanso_path}}}", &espanso_path.to_string_lossy());
std::fs::write(service_file, service_content)?; std::fs::write(service_file, service_content)?;
}
info_println!("enabling systemd service"); info_println!("enabling systemd service");
@ -118,6 +123,17 @@ pub enum UnregisterError {
SystemdCallFailed(anyhow::Error), SystemdCallFailed(anyhow::Error),
} }
pub fn is_present() -> bool {
let res = Command::new("systemctl")
.args(&["--user", "cat", LINUX_SERVICE_NAME])
.output();
if let Ok(output) = res {
output.status.success()
} else {
false
}
}
pub fn is_registered() -> bool { pub fn is_registered() -> bool {
let res = Command::new("systemctl") let res = Command::new("systemctl")
.args(&["--user", "is-enabled", LINUX_SERVICE_NAME]) .args(&["--user", "is-enabled", LINUX_SERVICE_NAME])