Private
Public Access
1
0
Files
Kordophone/kpcli/src/daemon/mod.rs

70 lines
1.9 KiB
Rust
Raw Normal View History

2025-02-12 00:10:33 -08:00
use anyhow::Result;
use clap::Subcommand;
use dbus::blocking::{Connection, Proxy};
const DBUS_NAME: &str = "net.buzzert.kordophonecd";
const DBUS_PATH: &str = "/net/buzzert/kordophonecd/daemon";
2025-02-12 00:10:33 -08:00
mod dbus_interface {
#![allow(unused)]
include!(concat!(env!("OUT_DIR"), "/kordophone-client.rs"));
}
use dbus_interface::NetBuzzertKordophoneRepository as KordophoneRepository;
2025-02-12 00:10:33 -08:00
#[derive(Subcommand)]
pub enum Commands {
/// Gets all known conversations.
Conversations,
/// Runs a sync operation.
Sync,
2025-02-12 00:10:33 -08:00
/// 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,
Commands::Conversations => client.print_conversations().await,
Commands::Sync => client.sync_conversations().await,
2025-02-12 00:10:33 -08:00
}
}
}
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 = KordophoneRepository::get_version(&self.proxy())?;
2025-02-12 00:10:33 -08:00
println!("Server version: {}", version);
Ok(())
}
pub async fn print_conversations(&mut self) -> Result<()> {
let conversations = KordophoneRepository::get_conversations(&self.proxy())?;
println!("Conversations: {:?}", conversations);
Ok(())
}
pub async fn sync_conversations(&mut self) -> Result<()> {
let success = KordophoneRepository::sync_all_conversations(&self.proxy())?;
println!("Initiated sync");
Ok(())
}
2025-02-12 00:10:33 -08:00
}