use std::path::PathBuf;
use anyhow::{Context, Result};
pub(crate) fn run_show(explicit: Option<PathBuf>) -> Result<String> {
let cfg = super::load(explicit)?;
Ok(render(&cfg))
}
pub(crate) fn run_set_device(address: &str) -> Result<String> {
::config::set_device(address).context("writing device address to user config")?;
Ok(format!("device.address = {address}"))
}
pub(crate) async fn run_setup() -> Result<String> {
let devices =
transport::discover::list_paired_devices().await.context("listing paired devices")?;
if devices.is_empty() {
anyhow::bail!("no paired devices found — pair one first, e.g. `bluetoothctl pair <MAC>`");
}
let labels: Vec<String> = devices
.iter()
.map(|d| {
d.name
.as_ref()
.map_or_else(|| d.address.to_string(), |name| format!("{name} ({})", d.address))
})
.collect();
let choice = dialoguer::Select::new()
.with_prompt("Select a device")
.items(&labels)
.interact()
.context("reading device selection")?;
let address = devices.get(choice).context("selection index out of range")?.address;
let channels = transport::discover::resolve_channels(&address.to_string())
.await
.context("resolving MAP/PBAP channels over SDP")?;
let (Some(map), Some(pbap)) = (channels.map, channels.pbap) else {
anyhow::bail!(
"device {address} is missing a service record (map={:?}, pbap={:?}) — set the \
address manually with `config set-device` and edit the missing channel by hand",
channels.map,
channels.pbap,
);
};
::config::set_device_and_channels(&address.to_string(), map, pbap)
.context("writing device address and channels to user config")?;
Ok(format!(
"device.address = {address}\n\
device.map_channel = {map}\n\
device.pbap_channel = {pbap}"
))
}
fn render(cfg: &::config::Config) -> String {
format!(
"device.address = {}\n\
device.map_channel = {}\n\
device.pbap_channel = {}\n\
hub.node_key = {}",
cfg.device.address(),
cfg.device.map_channel,
cfg.device.pbap_channel,
cfg.hub.node_key.as_deref().unwrap_or("<not set>"),
)
}