Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ pub enum ServiceManagerKind {

impl ServiceManagerKind {
/// Looks up the kind of service management platform native to the operating system
pub fn native() -> io::Result<ServiceManagerKind> {
pub fn native() -> io::Result<Self> {
cfg_if! {
if #[cfg(target_os = "macos")] {
Ok(ServiceManagerKind::Launchd)
Ok(Self::Launchd)
} else if #[cfg(target_os = "windows")] {
use super::{ServiceManager, TypedServiceManager};

Expand Down
30 changes: 12 additions & 18 deletions src/launchd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl ServiceManager for LaunchdServiceManager {
match which::which(LAUNCHCTL) {
Ok(_) => Ok(true),
Err(which::Error::CannotFindBinaryPath) => Ok(false),
Err(x) => Err(io::Error::new(io::ErrorKind::Other, x)),
Err(x) => Err(io::Error::other(x)),
}
}

Expand All @@ -121,7 +121,7 @@ impl ServiceManager for LaunchdServiceManager {
ctx.working_directory.clone(),
ctx.environment.clone(),
ctx.autostart,
ctx.disable_restart_on_failure
ctx.disable_restart_on_failure,
),
};

Expand Down Expand Up @@ -208,24 +208,18 @@ impl ServiceManager for LaunchdServiceManager {
}
} else {
// We have access to the full service label, so it impossible to get the failed status, or it must be input error.
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Command failed with exit code {}: {}",
output.status.code().unwrap_or(-1),
out
),
));
}
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
return Err(io::Error::other(format!(
"Command failed with exit code {}: {}",
output.status.code().unwrap_or(-1),
String::from_utf8_lossy(&output.stderr)
),
));
out
)));
}
} else {
return Err(io::Error::other(format!(
"Command failed with exit code {}: {}",
output.status.code().unwrap_or(-1),
String::from_utf8_lossy(&output.stderr)
)));
}
}
out = Cow::Owned(String::from_utf8_lossy(&output.stdout).to_string());
Expand Down
38 changes: 14 additions & 24 deletions src/openrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl ServiceManager for OpenRcServiceManager {
match which::which(RC_SERVICE) {
Ok(_) => Ok(true),
Err(which::Error::CannotFindBinaryPath) => Ok(false),
Err(x) => Err(io::Error::new(io::ErrorKind::Other, x)),
Err(x) => Err(io::Error::other(x)),
}
}

Expand Down Expand Up @@ -84,14 +84,10 @@ impl ServiceManager for OpenRcServiceManager {

fn uninstall(&self, ctx: ServiceUninstallCtx) -> io::Result<()> {
// If the script is configured to run at boot, remove it
let _ = rc_update(
"del",
&ctx.label.to_script_name(),
[OsStr::new("default")],
);
let _ = rc_update("del", &ctx.label.to_script_name(), [OsStr::new("default")]);

// Uninstall service by removing the script
std::fs::remove_file(service_dir_path().join(&ctx.label.to_script_name()))
std::fs::remove_file(service_dir_path().join(ctx.label.to_script_name()))
}

fn start(&self, ctx: ServiceStartCtx) -> io::Result<()> {
Expand Down Expand Up @@ -129,26 +125,20 @@ impl ServiceManager for OpenRcServiceManager {
if stdio.contains("does not exist") {
Ok(crate::ServiceStatus::NotInstalled)
} else {
Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Failed to get status of service {}: {}",
ctx.label.to_script_name(),
stdio
),
))
Err(io::Error::other(format!(
"Failed to get status of service {}: {}",
ctx.label.to_script_name(),
stdio
)))
}
}
Some(0) => Ok(crate::ServiceStatus::Running),
Some(3) => Ok(crate::ServiceStatus::Stopped(None)),
_ => Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Failed to get status of service {}: {}",
ctx.label.to_script_name(),
String::from_utf8_lossy(&output.stderr)
),
)),
_ => Err(io::Error::other(format!(
"Failed to get status of service {}: {}",
ctx.label.to_script_name(),
String::from_utf8_lossy(&output.stderr)
))),
}
}
}
Expand Down Expand Up @@ -203,7 +193,7 @@ fn rc_update<'a>(
})
.unwrap_or_else(|| format!("Failed to {cmd} {service}"));

Err(io::Error::new(io::ErrorKind::Other, msg))
Err(io::Error::other(msg))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/rcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl ServiceManager for RcdServiceManager {
_ => {
let code = status.code().unwrap_or(-1);
let msg = format!("Failed to get status of {service}, exit code: {code}");
Err(io::Error::new(io::ErrorKind::Other, msg))
Err(io::Error::other(msg))
}
}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ fn rc_d_script(cmd: &str, service: &str, wrap: bool) -> io::Result<ExitStatus> {
Ok(status)
} else {
let msg = format!("Failed to {cmd} {service}");
Err(io::Error::new(io::ErrorKind::Other, msg))
Err(io::Error::other(msg))
}
} else {
Ok(status)
Expand Down
17 changes: 7 additions & 10 deletions src/sc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl ServiceManager for ScServiceManager {
match which::which(SC_EXE) {
Ok(_) => Ok(true),
Err(which::Error::CannotFindBinaryPath) => Ok(false),
Err(x) => Err(io::Error::new(io::ErrorKind::Other, x)),
Err(x) => Err(io::Error::other(x)),
}
}

Expand Down Expand Up @@ -273,19 +273,16 @@ impl ServiceManager for ScServiceManager {
// 1060 = The specified service does not exist as an installed service.
return Ok(crate::ServiceStatus::NotInstalled);
}
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Command failed with exit code {}: {}",
output.status.code().unwrap_or(-1),
String::from_utf8_lossy(&output.stderr)
),
));
return Err(io::Error::other(format!(
"Command failed with exit code {}: {}",
output.status.code().unwrap_or(-1),
String::from_utf8_lossy(&output.stderr)
)));
}

let stdout = String::from_utf8_lossy(&output.stdout);
let line = stdout.split('\n').find(|line| {
line.trim_matches(&['\r', ' '])
line.trim_matches(['\r', ' '])
.to_lowercase()
.starts_with("state")
});
Expand Down
19 changes: 8 additions & 11 deletions src/systemd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl ServiceManager for SystemdServiceManager {
match which::which(SYSTEMCTL) {
Ok(_) => Ok(true),
Err(which::Error::CannotFindBinaryPath) => Ok(false),
Err(x) => Err(io::Error::new(io::ErrorKind::Other, x)),
Err(x) => Err(io::Error::other(x)),
}
}

Expand All @@ -144,7 +144,7 @@ impl ServiceManager for SystemdServiceManager {
&ctx,
self.user,
ctx.autostart,
ctx.disable_restart_on_failure
ctx.disable_restart_on_failure,
),
};

Expand Down Expand Up @@ -216,14 +216,11 @@ impl ServiceManager for SystemdServiceManager {
Some(4) => Ok(crate::ServiceStatus::NotInstalled),
Some(3) => Ok(crate::ServiceStatus::Stopped(None)),
Some(0) => Ok(crate::ServiceStatus::Running),
_ => Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Command failed with exit code {}: {}",
output.status.code().unwrap_or(-1),
String::from_utf8_lossy(&output.stderr)
),
)),
_ => Err(io::Error::other(format!(
"Command failed with exit code {}: {}",
output.status.code().unwrap_or(-1),
String::from_utf8_lossy(&output.stderr)
))),
}
}
}
Expand Down Expand Up @@ -261,7 +258,7 @@ fn make_service(
ctx: &ServiceInstallCtx,
user: bool,
autostart: bool,
disable_restart_on_failure: bool
disable_restart_on_failure: bool,
) -> String {
use std::fmt::Write as _;
let SystemdInstallConfig {
Expand Down
13 changes: 5 additions & 8 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,11 @@ pub fn wrap_output(output: Output) -> std::io::Result<Output> {
if msg.is_empty() {
msg = Cow::Borrowed("Failed to execute command with no output");
}
Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Command failed with exit code {}: {}",
output.status.code().unwrap_or(-1),
msg
),
))
Err(io::Error::other(format!(
"Command failed with exit code {}: {}",
output.status.code().unwrap_or(-1),
msg
)))
}
}

Expand Down
Loading
Loading