From 36268937de44285050053e85fe10c6c89f154692 Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Wed, 23 Sep 2020 08:45:03 +0200 Subject: [PATCH] Connect clap with dbus execution --- sfsmcctl/src/main.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/sfsmcctl/src/main.rs b/sfsmcctl/src/main.rs index ec00137..313c7b2 100644 --- a/sfsmcctl/src/main.rs +++ b/sfsmcctl/src/main.rs @@ -158,12 +158,32 @@ async fn main() -> Result<(), Box> { panic!("Lost connection to D-Bus: {}", err); }); + // Get subcommands (e.g. ("start", None); ("minecraft", Some("stop"))) + let subcommands = clapped.subcommand(); + let subcommands = (subcommands.0, subcommands.1.map(|m| m.subcommand_name()).flatten()); + // Get the action (e.g. subcommand: ("start", None) -> "start"; subcommand: ("minecraft", Some("stop")) -> "stop") + let action = match subcommands { + (a, None) => a, + (_, Some(a)) => a + }; + // The object path + let path = match subcommands.0 { + "minecraft" => "/minecraft", + _ => "/" + }; + // Get a proxy connection to the daemon - let daemon = NativeObject::new(c, "/"); + let object = NativeObject::new(c, path); // TODO: // Perform operations - info!("The daemon is{} running", if !daemon.is_running().await {"n't"} else {""}); + match action { + "start" => object.start().await?, + "stop" => object.stop().await?, + "restart" => object.restart().await?, + "is-running" => info!("Service at path {} is{} running", path, if !object.is_running().await {"n't"} else {""}), + _ => unreachable!("unknown action") + } Ok(()) } \ No newline at end of file