cloudpub_client/service/
mod.rs

1use anyhow::Result;
2use std::path::PathBuf;
3
4#[cfg(target_os = "windows")]
5mod windows;
6#[cfg(target_os = "windows")]
7pub use windows::*;
8
9#[cfg(target_os = "linux")]
10mod linux;
11#[cfg(target_os = "linux")]
12pub use linux::*;
13
14#[cfg(target_os = "macos")]
15mod macos;
16#[cfg(target_os = "macos")]
17pub use macos::*;
18
19// Common service trait
20pub trait ServiceManager {
21    fn install(&self) -> Result<()>;
22    fn uninstall(&self) -> Result<()>;
23    fn start(&self) -> Result<()>;
24    fn stop(&self) -> Result<()>;
25    fn status(&self) -> Result<ServiceStatus>;
26}
27
28#[allow(dead_code)]
29pub enum ServiceStatus {
30    Running,
31    Stopped,
32    NotInstalled,
33    Unknown,
34}
35
36#[allow(dead_code)]
37pub struct ServiceConfig {
38    pub name: String,
39    pub display_name: String,
40    pub description: String,
41    pub executable_path: PathBuf,
42    pub args: Vec<String>,
43    pub config_path: Option<PathBuf>,
44}
45
46pub fn create_service_manager(config: ServiceConfig) -> Box<dyn ServiceManager> {
47    #[cfg(target_os = "windows")]
48    {
49        Box::new(WindowsServiceManager::new(config))
50    }
51    #[cfg(target_os = "linux")]
52    {
53        Box::new(LinuxServiceManager::new(config))
54    }
55    #[cfg(target_os = "macos")]
56    {
57        Box::new(MacOSServiceManager::new(config))
58    }
59    #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
60    {
61        panic!("Unsupported platform for service management");
62    }
63}