update/
check.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::args;
6use crate::monitor_state::monitor_state;
7use anyhow::{Context, Error};
8use fidl_fuchsia_update::{CheckOptions, Initiator, ManagerMarker, ManagerProxy, MonitorMarker};
9use fuchsia_component::client::connect_to_protocol;
10
11pub async fn handle_check_now_cmd(cmd: args::CheckNow) -> Result<(), Error> {
12    let update_manager =
13        connect_to_protocol::<ManagerMarker>().context("Failed to connect to update manager")?;
14    handle_check_now_cmd_impl(cmd, &update_manager).await
15}
16
17async fn handle_check_now_cmd_impl(
18    cmd: args::CheckNow,
19    update_manager: &ManagerProxy,
20) -> Result<(), Error> {
21    let args::CheckNow { service_initiated, monitor } = cmd;
22    let options = CheckOptions {
23        initiator: Some(if service_initiated { Initiator::Service } else { Initiator::User }),
24        allow_attaching_to_existing_update_check: Some(true),
25        ..Default::default()
26    };
27    let (monitor_client, monitor_server) = if monitor {
28        let (client_end, request_stream) =
29            fidl::endpoints::create_request_stream::<MonitorMarker>();
30        (Some(client_end), Some(request_stream))
31    } else {
32        (None, None)
33    };
34    if let Err(e) = update_manager.check_now(&options, monitor_client).await? {
35        anyhow::bail!("Update check failed to start: {:?}", e);
36    }
37    println!("Checking for an update.");
38    if let Some(monitor_server) = monitor_server {
39        monitor_state(monitor_server).await?;
40    }
41    Ok(())
42}