Skip to main content

orch/cli/
service.rs

1use crate::cmd::SyncCommandErrorContext;
2
3/// Run a service subcommand for orch: `brew services` on macOS, the `orch` systemd user unit elsewhere.
4fn service(action: &str) -> anyhow::Result<()> {
5    let (program, args) = if cfg!(target_os = "macos") {
6        ("brew", vec!["services", action, "orch"])
7    } else {
8        // brew's `info` is systemd's `status`
9        let action = if action == "info" { "status" } else { action };
10        ("systemctl", vec!["--user", action, "orch"])
11    };
12
13    let status = std::process::Command::new(program)
14        .args(&args)
15        .status_with_context()?;
16
17    if !status.success() {
18        anyhow::bail!("{program} {} failed", args.join(" "));
19    }
20
21    Ok(())
22}
23
24/// Start the orch service.
25pub fn start() -> anyhow::Result<()> {
26    service("start")
27}
28
29/// Stop the orch service.
30pub fn stop() -> anyhow::Result<()> {
31    service("stop")
32}
33
34/// Restart the orch service.
35pub fn restart() -> anyhow::Result<()> {
36    service("restart")
37}
38
39/// Show service status.
40pub fn status() -> anyhow::Result<()> {
41    service("info")
42}