1use anyhow::{anyhow, Context as _};
6use fidl_fuchsia_dash as fdash;
7use fuchsia_component::client::connect_to_protocol;
8use futures::stream::StreamExt as _;
9
10mod args;
11
12pub async fn exec() -> anyhow::Result<()> {
13 let args: args::PackageArgs = argh::from_env();
14
15 match args.subcommand {
16 crate::args::PackageSubcommand::Explore(args) => {
17 let dash_launcher = connect_to_protocol::<fdash::LauncherMarker>()?;
20 let stdout = socket_to_stdio::Stdout::buffered();
22
23 #[allow(clippy::large_futures)]
24 explore_cmd(args, dash_launcher, stdout).await
25 }
26 }
27}
28
29async fn explore_cmd(
30 args: crate::args::ExploreArgs,
31 dash_launcher: fdash::LauncherProxy,
32 stdout: socket_to_stdio::Stdout<'_>,
33) -> anyhow::Result<()> {
34 let crate::args::ExploreArgs { url, subpackages, tools, command, fuchsia_pkg_resolver } = args;
35 let (client, server) = fidl::Socket::create_stream();
36 let () = dash_launcher
37 .explore_package_over_socket2(
38 fuchsia_pkg_resolver,
39 &url,
40 &subpackages,
41 server,
42 &tools,
43 command.as_deref(),
44 )
45 .await
46 .context("fuchsia.dash/Launcher.ExplorePackageOverSocket2 fidl error")?
47 .map_err(|e| match e {
48 fdash::LauncherError::ResolveTargetPackage => {
49 anyhow!("No package found matching '{url}' {}.", subpackages.join(" "))
50 }
51 e => anyhow!("Error exploring package: {e:?}"),
52 })?;
53
54 #[allow(clippy::large_futures)]
55 let () = socket_to_stdio::connect_socket_to_stdio(client, stdout).await?;
56
57 let exit_code = wait_for_shell_exit(&dash_launcher).await?;
58 std::process::exit(exit_code);
59}
60
61async fn wait_for_shell_exit(launcher_proxy: &fdash::LauncherProxy) -> anyhow::Result<i32> {
62 match launcher_proxy.take_event_stream().next().await {
63 Some(Ok(fdash::LauncherEvent::OnTerminated { return_code })) => Ok(return_code),
64 Some(Err(e)) => Err(anyhow!("OnTerminated event error: {e:?}")),
65 None => Err(anyhow!("didn't receive an expected OnTerminated event")),
66 }
67}