Optimise daemon action behaviour

This commit is contained in:
Elias Schriefer 2020-11-05 19:08:55 +01:00
parent d18e44d3b9
commit baa1d67f29

View File

@ -48,7 +48,34 @@ impl<'proxy> NativeObject<'proxy> {
// _ => Self::no_path("start", self) // _ => Self::no_path("start", self)
}; };
proxy.method_call(iface_name("Service"), "Start", ()).await if proxy.path != dbus::Path::new("/").unwrap() && !Self::new(proxy.connection.clone(), "/").is_running().await {
return Err(Error::new_failed("Service is not running"))
}
match proxy.method_call(iface_name("Service"), "Start", ()).await {
Err(err)
if proxy.path == dbus::Path::new("/").unwrap()
&& err.name() == Some("org.freedesktop.DBus.Error.ServiceUnknown")
=> {
#[cfg(debug_assertions)]
let args = format!("start sfsmc-debug@{}", std::env::current_dir()
.map_err(|err| dbus::MethodErr::failed(&err))?
.parent()
.ok_or(Error::new_failed("called `Option::unwrap()` on a `None` value"))?
.display()
);
#[cfg(not(debug_assertions))]
let args = "start sfsmc".to_owned();
tokio::process::Command::new("systemctl")
.args(args.split_whitespace())
.spawn()
.map_err(|err| dbus::MethodErr::failed(&err))?
.await
.map(|_| ())
.map_err(|err| dbus::MethodErr::failed(&err).into())
},
res => res
}
} }
pub async fn stop(&self) -> Result<(), Error> { pub async fn stop(&self) -> Result<(), Error> {
@ -175,7 +202,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Get a proxy connection to the daemon // Get a proxy connection to the daemon
let object = NativeObject::new(c, path); let object = NativeObject::new(c, path);
// TODO:
// Perform operations // Perform operations
let res = match action { let res = match action {
"start" => object.start().await, "start" => object.start().await,