kpcli: add daemon messaging support
This commit is contained in:
50
kpcli/src/daemon/mod.rs
Normal file
50
kpcli/src/daemon/mod.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use anyhow::Result;
|
||||
use clap::Subcommand;
|
||||
use dbus::blocking::{Connection, Proxy};
|
||||
|
||||
const DBUS_NAME: &str = "net.buzzert.kordophonecd";
|
||||
const DBUS_PATH: &str = "/net/buzzert/kordophone/Server";
|
||||
|
||||
mod dbus_interface {
|
||||
#![allow(unused)]
|
||||
include!(concat!(env!("OUT_DIR"), "/kordophone-client.rs"));
|
||||
}
|
||||
|
||||
use dbus_interface::NetBuzzertKordophoneServer as KordophoneServer;
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
/// Prints the server Kordophone version.
|
||||
Version,
|
||||
}
|
||||
|
||||
impl Commands {
|
||||
pub async fn run(cmd: Commands) -> Result<()> {
|
||||
let mut client = DaemonCli::new()?;
|
||||
match cmd {
|
||||
Commands::Version => client.print_version().await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DaemonCli {
|
||||
conn: Connection,
|
||||
}
|
||||
|
||||
impl DaemonCli {
|
||||
pub fn new() -> Result<Self> {
|
||||
Ok(Self {
|
||||
conn: Connection::new_session()?
|
||||
})
|
||||
}
|
||||
|
||||
fn proxy(&self) -> Proxy<&Connection> {
|
||||
self.conn.with_proxy(DBUS_NAME, DBUS_PATH, std::time::Duration::from_millis(5000))
|
||||
}
|
||||
|
||||
pub async fn print_version(&mut self) -> Result<()> {
|
||||
let version = KordophoneServer::get_version(&self.proxy())?;
|
||||
println!("Server version: {}", version);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
mod client;
|
||||
mod db;
|
||||
mod printers;
|
||||
mod daemon;
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::{Parser, Subcommand};
|
||||
@@ -25,6 +26,12 @@ enum Commands {
|
||||
Db {
|
||||
#[command(subcommand)]
|
||||
command: db::Commands,
|
||||
},
|
||||
|
||||
/// Commands for interacting with the daemon
|
||||
Daemon {
|
||||
#[command(subcommand)]
|
||||
command: daemon::Commands,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +39,7 @@ async fn run_command(command: Commands) -> Result<()> {
|
||||
match command {
|
||||
Commands::Client { command } => client::Commands::run(command).await,
|
||||
Commands::Db { command } => db::Commands::run(command).await,
|
||||
Commands::Daemon { command } => daemon::Commands::run(command).await,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user