framework_startup/
startup.rs1use anyhow::{Result, anyhow};
4
5use crate::config::StartupConfig;
6use crate::platform::{Platform, current_platform};
7use crate::scope::Scope;
8
9pub struct Startup {
11 config: StartupConfig,
12 scope: Scope,
13 platform: Box<dyn Platform>,
14}
15
16impl Startup {
17 pub fn root(config: StartupConfig) -> Result<Self> {
19 Self::new(config, Scope::Root)
20 }
21
22 pub fn user(config: StartupConfig) -> Result<Self> {
27 Self::new(config, Scope::User)
28 }
29
30 pub fn exists(&self) -> Result<bool> {
32 self.platform.exists(&self.config, self.scope)
33 }
34
35 pub fn is_run(&self) -> Result<bool> {
39 if self.not_installed()? {
40 return Ok(false);
41 }
42
43 self.running()
44 }
45
46 pub fn install(&self) -> Result<()> {
48 if self.exists()? {
49 return Ok(());
50 }
51
52 self.platform.install(&self.config, self.scope)
53 }
54
55 pub fn install_force(&self) -> Result<()> {
57 self.platform.install(&self.config, self.scope)
58 }
59
60 pub fn uninstall(&self) -> Result<()> {
64 if self.not_installed()? {
65 return Ok(());
66 }
67
68 self.platform.uninstall(&self.config, self.scope)
69 }
70
71 pub fn start(&self) -> Result<()> {
73 if self.not_installed()? || self.running()? {
74 return Ok(());
75 }
76
77 self.platform.start(&self.config, self.scope)
78 }
79
80 pub fn stop(&self) -> Result<()> {
82 if self.not_installed()? || !self.running()? {
83 return Ok(());
84 }
85
86 self.platform.stop(&self.config, self.scope)
87 }
88
89 pub fn restart(&self) -> Result<()> {
94 if self.not_installed()? {
95 return Ok(());
96 }
97
98 if self.running()? {
99 let _ = self.platform.stop(&self.config, self.scope);
100 }
101
102 self.platform.start(&self.config, self.scope)
103 }
104
105 fn running(&self) -> Result<bool> {
107 self.platform.is_run(&self.config, self.scope)
108 }
109
110 fn not_installed(&self) -> Result<bool> {
112 if self.exists()? {
113 return Ok(false);
114 }
115 if self.config.ignore_not_installed {
116 return Ok(true);
117 }
118
119 Err(anyhow!("自启 {} 未注册", self.config.name))
120 }
121
122 fn new(config: StartupConfig, scope: Scope) -> Result<Self> {
123 Ok(Self {
124 config: config.resolve()?,
125 scope,
126 platform: current_platform()?,
127 })
128 }
129}