1use std::borrow::Cow;2use std::io::Write as _;34use anyhow::Context as _;5use remowt_ui_prompt::dbus::{DbusPrompterInterface, DbusPrompterProxy, BUS_NAME, PROMPTER_PATH};6use remowt_ui_prompt::{Prompter, Source};7use tracing::debug;8use zbus::Connection;910pub async fn serve<P>(conn: &Connection, prompter: P) -> anyhow::Result<()>11where12 P: Prompter + 'static,13{14 conn.object_server()15 .at(PROMPTER_PATH, DbusPrompterInterface(prompter))16 .await?;17 match conn.request_name(BUS_NAME).await {18 Ok(()) => {}19 Err(zbus::Error::NameTaken) => {20 debug!("{BUS_NAME} already owned, chaining to upstream");21 }22 Err(e) => return Err(e.into()),23 }24 Ok(())25}2627pub async fn ask(prompt: &str, description: String) -> anyhow::Result<()> {28 let conn = Connection::session()29 .await30 .context("connecting to the session bus (DBUS_SESSION_BUS_ADDRESS)")?;31 let proxy = DbusPrompterProxy::builder(&conn)32 .destination(BUS_NAME)?33 .path(PROMPTER_PATH)?34 .build()35 .await?;3637 let password = proxy38 .prompt_text(39 false,40 prompt,41 &description,42 &[Source(Cow::Borrowed("remowt-askpass"))],43 )44 .await?;4546 let mut out = std::io::stdout().lock();47 out.write_all(password.as_bytes())?;48 out.write_all(b"\n")?;49 out.flush()?;50 Ok(())51}