fix(cli): detect & prefer system-provided service
Instead of always creating our own service file for the user, detect if there is a service already present, and skip installing our own service file.
This commit is contained in:
parent
b2356abe69
commit
3b48a9ac9c
|
@ -35,18 +35,23 @@ const LINUX_SERVICE_FILENAME: &str = formatcp!("{}.service", LINUX_SERVICE_NAME)
|
|||
pub fn register() -> Result<()> {
|
||||
let service_file = get_service_file_path()?;
|
||||
|
||||
if service_file.exists() {
|
||||
warn_eprintln!("service file already exists, this operation will overwrite it");
|
||||
if !service_file.exists() && is_present() {
|
||||
// probably installed by system package
|
||||
info_println!("skipping installation of already provided service");
|
||||
} else {
|
||||
if service_file.exists() {
|
||||
warn_eprintln!("service file already exists, this operation will overwrite it");
|
||||
}
|
||||
|
||||
info_println!("creating service file in {:?}", service_file);
|
||||
let espanso_path = get_binary_path().expect("unable to get espanso executable path");
|
||||
|
||||
let service_content = String::from(LINUX_SERVICE_CONTENT)
|
||||
.replace("{{{espanso_path}}}", &espanso_path.to_string_lossy());
|
||||
|
||||
std::fs::write(service_file, service_content)?;
|
||||
}
|
||||
|
||||
info_println!("creating service file in {:?}", service_file);
|
||||
let espanso_path = get_binary_path().expect("unable to get espanso executable path");
|
||||
|
||||
let service_content = String::from(LINUX_SERVICE_CONTENT)
|
||||
.replace("{{{espanso_path}}}", &espanso_path.to_string_lossy());
|
||||
|
||||
std::fs::write(service_file, service_content)?;
|
||||
|
||||
info_println!("enabling systemd service");
|
||||
|
||||
match Command::new("systemctl")
|
||||
|
@ -118,6 +123,17 @@ pub enum UnregisterError {
|
|||
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 {
|
||||
let res = Command::new("systemctl")
|
||||
.args(&["--user", "is-enabled", LINUX_SERVICE_NAME])
|
||||
|
|
Loading…
Reference in New Issue
Block a user