1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

mod args;

use crate::args::*;
use anyhow::Result;
use component_debug::cli::*;
use component_debug::config::resolve_raw_config_overrides;
use component_debug::copy::copy_cmd;
use fidl_fuchsia_dash as fdash;
use fidl_fuchsia_sys2 as fsys;
use fuchsia_component::client::{connect_to_protocol, connect_to_protocol_at_path};
use socket_to_stdio::Stdout;

pub async fn exec() -> Result<()> {
    let args: ComponentArgs = argh::from_env();

    let writer = std::io::stdout();
    let realm_query =
        connect_to_protocol_at_path::<fsys::RealmQueryMarker>("/svc/fuchsia.sys2.RealmQuery.root")?;
    let route_validator = connect_to_protocol_at_path::<fsys::RouteValidatorMarker>(
        "/svc/fuchsia.sys2.RouteValidator.root",
    )?;
    let lifecycle_controller = connect_to_protocol_at_path::<fsys::LifecycleControllerMarker>(
        "/svc/fuchsia.sys2.LifecycleController.root",
    )?;

    match args.subcommand {
        ComponentSubcommand::Show(args) => {
            show_cmd_print(args.query, realm_query, writer, true).await
        }
        ComponentSubcommand::Create(args) => {
            let config_overrides = resolve_raw_config_overrides(
                &realm_query,
                &args.moniker,
                &args.url.to_string(),
                &args.config,
            )
            .await?;
            create_cmd(args.url, args.moniker, config_overrides, lifecycle_controller, writer).await
        }
        ComponentSubcommand::Destroy(args) => {
            destroy_cmd(args.query, lifecycle_controller, realm_query, writer).await
        }
        ComponentSubcommand::Resolve(args) => {
            resolve_cmd(args.query, lifecycle_controller, realm_query, writer).await
        }
        ComponentSubcommand::Explore(args) => {
            // TODO(https://fxbug.dev/42177573): Verify that the optional Launcher protocol is available
            // before connecting.
            let dash_launcher = connect_to_protocol::<fdash::LauncherMarker>()?;
            // TODO(https://fxbug.dev/42077838): Use Stdout::raw instead, when a command is not provided
            let stdout = Stdout::buffered();

            #[allow(clippy::large_futures)]
            explore_cmd(
                args.query,
                args.ns_layout,
                args.command,
                args.tools,
                dash_launcher,
                realm_query,
                stdout,
            )
            .await
        }
        ComponentSubcommand::Reload(args) => {
            reload_cmd(args.query, lifecycle_controller, realm_query, writer).await
        }
        ComponentSubcommand::Start(args) => {
            start_cmd(args.query, lifecycle_controller, realm_query, writer).await
        }
        ComponentSubcommand::Stop(args) => {
            stop_cmd(args.query, lifecycle_controller, realm_query, writer).await
        }
        ComponentSubcommand::Doctor(args) => {
            doctor_cmd_print(args.query, route_validator, realm_query, writer).await
        }
        ComponentSubcommand::Capability(args) => {
            capability_cmd(args.capability_name, realm_query, writer).await
        }
        ComponentSubcommand::List(args) => {
            list_cmd_print(args.filter, args.verbose, realm_query, writer).await
        }
        ComponentSubcommand::Graph(args) => {
            graph_cmd(args.filter, args.orientation, realm_query, writer).await
        }
        ComponentSubcommand::Run(args) => {
            let config_overrides = resolve_raw_config_overrides(
                &realm_query,
                &args.moniker,
                &args.url.to_string(),
                &args.config,
            )
            .await?;
            run_cmd(
                args.moniker,
                args.url,
                args.recreate,
                args.connect_stdio,
                config_overrides,
                lifecycle_controller,
                writer,
            )
            .await
        }
        ComponentSubcommand::Copy(args) => {
            copy_cmd(&realm_query, args.paths, args.verbose, writer).await
        }
        ComponentSubcommand::Storage(args) => match args.subcommand {
            StorageSubcommand::Copy(copy_args) => {
                storage_copy_cmd(
                    args.provider,
                    args.capability,
                    copy_args.source_path,
                    copy_args.destination_path,
                    realm_query,
                )
                .await
            }
            StorageSubcommand::Delete(delete_args) => {
                storage_delete_cmd(args.provider, args.capability, delete_args.path, realm_query)
                    .await
            }
            StorageSubcommand::List(list_args) => {
                storage_list_cmd(
                    args.provider,
                    args.capability,
                    list_args.path,
                    realm_query,
                    writer,
                )
                .await
            }
            StorageSubcommand::MakeDirectory(make_dir_args) => {
                storage_make_directory_cmd(
                    args.provider,
                    args.capability,
                    make_dir_args.path,
                    realm_query,
                )
                .await
            }
        },
        ComponentSubcommand::Collection(args) => match args.subcommand {
            CollectionSubcommand::List(_) => collection_list_cmd(realm_query, writer).await,
            CollectionSubcommand::Show(show_args) => {
                collection_show_cmd(show_args.query, realm_query, writer).await
            }
        },
    }
}