Skip to main content

component/
lib.rs

1// Copyright 2023 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
5mod args;
6
7use crate::args::*;
8use anyhow::Result;
9use component_debug::cli::*;
10use component_debug::config::resolve_raw_config_overrides;
11use component_debug::copy::copy_cmd;
12use fidl_fuchsia_dash as fdash;
13use fidl_fuchsia_sys2 as fsys;
14use fuchsia_component::client::{connect_to_protocol, connect_to_protocol_at_path};
15use futures::FutureExt;
16use socket_to_stdio::Stdout;
17
18pub async fn exec() -> Result<()> {
19    let args: ComponentArgs = argh::from_env();
20
21    let mut writer = std::io::stdout();
22    let realm_query =
23        connect_to_protocol_at_path::<fsys::RealmQueryMarker>("/svc/fuchsia.sys2.RealmQuery.root")?;
24    let route_validator = connect_to_protocol_at_path::<fsys::RouteValidatorMarker>(
25        "/svc/fuchsia.sys2.RouteValidator.root",
26    )?;
27    let lifecycle_controller = connect_to_protocol_at_path::<fsys::LifecycleControllerMarker>(
28        "/svc/fuchsia.sys2.LifecycleController.root",
29    )?;
30    let config_override = connect_to_protocol_at_path::<fsys::ConfigOverrideMarker>(
31        "/svc/fuchsia.sys2.ConfigOverride.root",
32    )?;
33
34    match args.subcommand {
35        ComponentSubcommand::Show(args) => {
36            show_cmd_print(args.query, realm_query, writer, true).await
37        }
38        ComponentSubcommand::Create(args) => {
39            let config_overrides = resolve_raw_config_overrides(
40                &realm_query,
41                &args.moniker,
42                &args.url.to_string(),
43                &args.config,
44            )
45            .await?;
46            create_cmd(args.url, args.moniker, config_overrides, lifecycle_controller, writer).await
47        }
48        ComponentSubcommand::Destroy(args) => {
49            destroy_cmd(args.query, lifecycle_controller, realm_query, writer).await
50        }
51        ComponentSubcommand::Resolve(args) => {
52            resolve_cmd(args.query, lifecycle_controller, realm_query, writer).await
53        }
54        ComponentSubcommand::Explore(args) => {
55            // TODO(https://fxbug.dev/296283299): Verify that the optional Launcher protocol is
56            // available before connecting.
57            let dash_launcher = connect_to_protocol::<fdash::LauncherMarker>()?;
58            // TODO(https://fxbug.dev/42077838): Use Stdout::raw instead, when a command is
59            // not provided.
60            let stdout = Stdout::buffered();
61
62            #[allow(clippy::large_futures)]
63            explore_cmd(
64                args.query,
65                args.ns_layout,
66                args.command,
67                args.tools,
68                dash_launcher,
69                realm_query,
70                stdout,
71            )
72            .await
73        }
74        ComponentSubcommand::Reload(args) => {
75            reload_cmd(args.query, lifecycle_controller, realm_query, writer).await
76        }
77        ComponentSubcommand::Start(args) => {
78            start_cmd(args.query, lifecycle_controller, realm_query, writer).await
79        }
80        ComponentSubcommand::Stop(args) => {
81            stop_cmd(args.query, lifecycle_controller, realm_query, writer).await
82        }
83        ComponentSubcommand::Doctor(args) => {
84            doctor_cmd_print(args.query, route_validator, realm_query, writer).await
85        }
86        ComponentSubcommand::Capability(args) => {
87            capability_cmd(args.capability_name, realm_query, writer).await
88        }
89        ComponentSubcommand::List(args) => {
90            list_cmd_print(args.filter, args.verbose, realm_query, writer).await
91        }
92        ComponentSubcommand::Graph(args) => {
93            graph_cmd(args.filter, args.orientation, realm_query, writer).await
94        }
95        ComponentSubcommand::Run(args) => {
96            let config_overrides = resolve_raw_config_overrides(
97                &realm_query,
98                &args.moniker,
99                &args.url.to_string(),
100                &args.config,
101            )
102            .await?;
103            run_cmd(
104                args.moniker,
105                args.url,
106                args.recreate,
107                args.connect_stdio,
108                config_overrides,
109                || {
110                    async {
111                        connect_to_protocol_at_path::<fsys::LifecycleControllerMarker>(
112                            "/svc/fuchsia.sys2.LifecycleController.root",
113                        )
114                    }
115                    .boxed()
116                },
117                writer,
118            )
119            .await
120        }
121        ComponentSubcommand::Copy(args) => {
122            copy_cmd(&realm_query, args.paths, args.verbose, writer).await
123        }
124        ComponentSubcommand::Route(args) => {
125            route_cmd_print(args.target, args.filter, route_validator, realm_query, writer).await
126        }
127        ComponentSubcommand::Storage(args) => match args.subcommand {
128            StorageSubcommand::Copy(copy_args) => {
129                storage_copy_cmd(
130                    args.provider,
131                    args.capability,
132                    copy_args.source_path,
133                    copy_args.destination_path,
134                    realm_query,
135                )
136                .await
137            }
138            StorageSubcommand::Delete(delete_args) => {
139                storage_delete_cmd(args.provider, args.capability, delete_args.path, realm_query)
140                    .await
141            }
142            StorageSubcommand::List(list_args) => {
143                let entries =
144                    storage_list_cmd(args.provider, args.capability, list_args.path, realm_query)
145                        .await?;
146                storage_list_cmd_write(entries, &mut writer)
147            }
148            StorageSubcommand::MakeDirectory(make_dir_args) => {
149                storage_make_directory_cmd(
150                    args.provider,
151                    args.capability,
152                    make_dir_args.path,
153                    realm_query,
154                )
155                .await
156            }
157        },
158        ComponentSubcommand::Collection(args) => match args.subcommand {
159            CollectionSubcommand::List(_) => collection_list_cmd(realm_query, writer).await,
160            CollectionSubcommand::Show(show_args) => {
161                collection_show_cmd(show_args.query, realm_query, writer).await
162            }
163        },
164        ComponentSubcommand::Config(args) => match args.subcommand {
165            ConfigSubcommand::Set(SetArgs { query, key_values, reload }) => {
166                config_set_cmd(
167                    query,
168                    key_values,
169                    reload,
170                    lifecycle_controller,
171                    realm_query,
172                    config_override,
173                    writer,
174                )
175                .await
176            }
177            ConfigSubcommand::Unset(UnsetArgs { query, reload }) => {
178                config_unset_cmd(
179                    query,
180                    reload,
181                    lifecycle_controller,
182                    realm_query,
183                    config_override,
184                    writer,
185                )
186                .await
187            }
188            ConfigSubcommand::List(ConfigListArgs { query }) => {
189                config_list_cmd(query, realm_query, writer).await
190            }
191        },
192    }
193}