mod client;
mod spawn;
use std::path::Path;
use anyhow::Result;
use broker_client::send_request;
use config::Config;
use ipc::{BrokerRequest, BrokerResponse};
pub(in crate::commands) use broker_client::{connect_retry, query_persistent, query_state};
pub(in crate::commands) use client::{run_status, run_stop};
pub(in crate::commands) fn resolve_addr<'a>(cfg: &'a Config, device: Option<&'a str>) -> &'a str {
device.unwrap_or_else(|| cfg.device.address())
}
pub(crate) async fn call(
cfg: &Config,
device: Option<&str>,
config_path: Option<&Path>,
req: BrokerRequest,
) -> Result<BrokerResponse> {
let addr = resolve_addr(cfg, device);
spawn::ensure_running(cfg, device, config_path).await?;
send_request(addr, req).await
}
pub(crate) async fn send(
cfg: &Config,
device: Option<&str>,
config_path: Option<&Path>,
number: String,
message: String,
) -> Result<String> {
let addr = resolve_addr(cfg, device);
spawn::ensure_running(cfg, device, config_path).await?;
Ok(broker_client::send(addr, number, message).await?)
}
pub(crate) async fn delete(
cfg: &Config,
device: Option<&str>,
config_path: Option<&Path>,
handle: String,
folder: String,
) -> Result<String> {
let addr = resolve_addr(cfg, device);
spawn::ensure_running(cfg, device, config_path).await?;
Ok(broker_client::delete(addr, handle, folder).await?)
}
pub(crate) async fn folders(
cfg: &Config,
device: Option<&str>,
config_path: Option<&Path>,
) -> Result<Vec<ipc::FolderDto>> {
let addr = resolve_addr(cfg, device);
spawn::ensure_running(cfg, device, config_path).await?;
Ok(broker_client::folders(addr).await?)
}
pub(crate) async fn sync(
cfg: &Config,
device: Option<&str>,
config_path: Option<&Path>,
folder: Option<String>,
) -> Result<String> {
let addr = resolve_addr(cfg, device);
spawn::ensure_running(cfg, device, config_path).await?;
Ok(broker_client::sync(addr, folder).await?)
}
pub(crate) async fn sync_contacts(
cfg: &Config,
device: Option<&str>,
config_path: Option<&Path>,
) -> Result<ipc::SyncReportDto> {
let addr = resolve_addr(cfg, device);
spawn::ensure_running(cfg, device, config_path).await?;
Ok(broker_client::sync_contacts(addr).await?)
}