demo: add an option to --wait for VM

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2021-12-16 23:43:15 +04:00
parent e4df821fc2
commit 5bee9ea7f3
2 changed files with 43 additions and 9 deletions

View File

@ -19,6 +19,32 @@ pub struct Display {
}
impl Display {
pub async fn lookup(
conn: &Connection,
wait: bool,
name: Option<&str>,
) -> Result<Option<OwnedUniqueName>> {
let mut changed = fdo::DBusProxy::new(conn)
.await?
.receive_name_owner_changed()
.await?;
loop {
let list = Display::by_name(&conn).await?;
if let Some(name) = name {
let res = list.get(name);
if res.is_some() {
return Ok(res.cloned());
}
} else if !list.is_empty() {
return Ok(None);
}
if !wait {
return Err(Error::Failed("Can't find VM".into()));
};
let _ = changed.next().await;
}
}
pub async fn by_name(conn: &Connection) -> Result<HashMap<String, OwnedUniqueName>> {
let mut hm = HashMap::new();
let list = match fdo::DBusProxy::new(conn)

View File

@ -27,6 +27,7 @@ struct AppOptions {
vm_name: Option<String>,
address: Option<String>,
list: bool,
wait: bool,
}
impl App {
@ -56,6 +57,14 @@ impl App {
"List available VM names",
None,
);
app.add_main_option(
"wait",
glib::Char(0),
glib::OptionFlags::NONE,
glib::OptionArg::None,
"Wait for display to be available",
None,
);
app.add_main_option(
"version",
glib::Char(0),
@ -79,6 +88,9 @@ impl App {
if opt.lookup_value("list", None).is_some() {
app_opt.list = true;
}
if opt.lookup_value("wait", None).is_some() {
app_opt.wait = true;
}
app_opt.vm_name = opt
.lookup_value(&glib::OPTION_REMAINING, None)
.and_then(|args| args.child_value(0).get::<String>());
@ -135,15 +147,11 @@ impl App {
app_clone.inner.app.quit();
return;
}
let dest = if let Some(name) = opt_clone.borrow().vm_name.as_ref() {
let list = Display::by_name(&conn).await.unwrap();
Some(
list.get(name)
.unwrap_or_else(|| panic!("Can't find VM name: {}", name))
.clone(),
)
} else {
None
let dest = {
let name = opt_clone.borrow().vm_name.clone();
let wait = opt_clone.borrow().wait;
Display::lookup(&conn, wait, name.as_deref()).await.unwrap()
};
let display = Display::new(&conn, dest.as_ref()).await.unwrap();