component_debug/cli/
list.rs

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// 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.

use crate::realm::{get_all_instances, Instance};
use ansi_term::Colour;
use anyhow::Result;
use fidl_fuchsia_sys2 as fsys;
use prettytable::format::consts::FORMAT_CLEAN;
use prettytable::{cell, row, Table};
use std::collections::HashSet;
use std::str::FromStr;

/// Filters that can be applied when listing components
#[derive(Debug, PartialEq)]
pub enum ListFilter {
    Running,
    Stopped,
    /// Filters components that are an ancestor of the component with the given name.
    /// Includes the named component.
    Ancestor(String),
    /// Filters components that are a descendant of the component with the given name.
    /// Includes the named component.
    Descendant(String),
    /// Filters components that are a relative (either an ancestor or a descendant) of the
    /// component with the given name. Includes the named component.
    Relative(String),
}

impl FromStr for ListFilter {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "running" => Ok(ListFilter::Running),
            "stopped" => Ok(ListFilter::Stopped),
            filter => match filter.split_once(":") {
                Some((function, arg)) => match function {
                    "ancestor" | "ancestors" => Ok(ListFilter::Ancestor(arg.to_string())),
                    "descendant" | "descendants" => Ok(ListFilter::Descendant(arg.to_string())),
                    "relative" | "relatives" => Ok(ListFilter::Relative(arg.to_string())),
                    _ => Err("unknown function for list filter."),
                },
                None => Err("list filter should be 'running', 'stopped', 'ancestors:<component_name>', 'descendants:<component_name>', or 'relatives:<component_name>'."),
            },
        }
    }
}

pub async fn list_cmd_print<W: std::io::Write>(
    filter: Option<ListFilter>,
    verbose: bool,
    realm_query: fsys::RealmQueryProxy,
    mut writer: W,
) -> Result<()> {
    let instances = get_instances_matching_filter(filter, &realm_query).await?;

    if verbose {
        let table = create_table(instances);
        table.print(&mut writer)?;
    } else {
        for instance in instances {
            writeln!(writer, "{}", instance.moniker)?;
        }
    }

    Ok(())
}

pub async fn list_cmd_serialized(
    filter: Option<ListFilter>,
    realm_query: fsys::RealmQueryProxy,
) -> Result<Vec<Instance>> {
    let basic_infos = get_instances_matching_filter(filter, &realm_query).await?;
    Ok(basic_infos)
}

/// Creates a verbose table containing information about all instances.
fn create_table(instances: Vec<Instance>) -> Table {
    let mut table = Table::new();
    table.set_format(*FORMAT_CLEAN);
    table.set_titles(row!("State", "Moniker", "URL"));

    for instance in instances {
        let state = instance.resolved_info.map_or(Colour::Red.paint("Stopped"), |r| {
            r.execution_info
                .map_or(Colour::Yellow.paint("Resolved"), |_| Colour::Green.paint("Running"))
        });

        table.add_row(row!(state, instance.moniker.to_string(), instance.url));
    }
    table
}

pub async fn get_instances_matching_filter(
    filter: Option<ListFilter>,
    realm_query: &fsys::RealmQueryProxy,
) -> Result<Vec<Instance>> {
    let instances = get_all_instances(realm_query).await?;

    let mut instances = match filter {
        Some(ListFilter::Running) => instances
            .into_iter()
            .filter(|i| i.resolved_info.as_ref().map_or(false, |r| r.execution_info.is_some()))
            .collect(),
        Some(ListFilter::Stopped) => instances
            .into_iter()
            .filter(|i| i.resolved_info.as_ref().map_or(true, |r| r.execution_info.is_none()))
            .collect(),
        Some(ListFilter::Ancestor(m)) => filter_ancestors(instances, m),
        Some(ListFilter::Descendant(m)) => filter_descendants(instances, m),
        Some(ListFilter::Relative(m)) => filter_relatives(instances, m),
        _ => instances,
    };

    instances.sort_by_key(|c| c.moniker.to_string());

    Ok(instances)
}

fn filter_ancestors(instances: Vec<Instance>, child_str: String) -> Vec<Instance> {
    let mut ancestors = HashSet::new();

    // Find monikers with this child as the leaf.
    for instance in &instances {
        if let Some(child) = instance.moniker.leaf() {
            if child.to_string() == child_str {
                // Add this moniker to ancestor list.
                let mut cur_moniker = instance.moniker.clone();
                ancestors.insert(cur_moniker.clone());

                // Loop over parents of this moniker and add them to ancestor list.
                while let Some(parent) = cur_moniker.parent() {
                    ancestors.insert(parent.clone());
                    cur_moniker = parent;
                }
            }
        }
    }

    instances.into_iter().filter(|i| ancestors.contains(&i.moniker)).collect()
}

fn filter_descendants(instances: Vec<Instance>, child_str: String) -> Vec<Instance> {
    let mut descendants = HashSet::new();

    // Find monikers with this child as the leaf.
    for instance in &instances {
        if let Some(child) = instance.moniker.leaf() {
            if child.to_string() == child_str {
                // Get all descendants of this moniker.
                for possible_child_instance in &instances {
                    if possible_child_instance.moniker.has_prefix(&instance.moniker) {
                        descendants.insert(possible_child_instance.moniker.clone());
                    }
                }
            }
        }
    }

    instances.into_iter().filter(|i| descendants.contains(&i.moniker)).collect()
}

fn filter_relatives(instances: Vec<Instance>, child_str: String) -> Vec<Instance> {
    let mut relatives = HashSet::new();

    // Find monikers with this child as the leaf.
    for instance in &instances {
        if let Some(child) = instance.moniker.leaf() {
            if child.to_string() == child_str {
                // Loop over parents of this moniker and add them to relatives list.
                let mut cur_moniker = instance.moniker.clone();
                while let Some(parent) = cur_moniker.parent() {
                    relatives.insert(parent.clone());
                    cur_moniker = parent;
                }

                // Get all descendants of this moniker and add them to relatives list.
                for possible_child_instance in &instances {
                    if possible_child_instance.moniker.has_prefix(&instance.moniker) {
                        relatives.insert(possible_child_instance.moniker.clone());
                    }
                }
            }
        }
    }

    instances.into_iter().filter(|i| relatives.contains(&i.moniker)).collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::*;
    use moniker::Moniker;
    use std::collections::HashMap;

    fn create_query() -> fsys::RealmQueryProxy {
        // Serve RealmQuery for CML components.
        let query = serve_realm_query(
            vec![
                fsys::Instance {
                    moniker: Some("./".to_string()),
                    url: Some("fuchsia-pkg://fuchsia.com/root#meta/root.cm".to_string()),
                    instance_id: None,
                    resolved_info: Some(fsys::ResolvedInfo {
                        resolved_url: Some(
                            "fuchsia-pkg://fuchsia.com/root#meta/root.cm".to_string(),
                        ),
                        execution_info: None,
                        ..Default::default()
                    }),
                    ..Default::default()
                },
                fsys::Instance {
                    moniker: Some("./core".to_string()),
                    url: Some("fuchsia-pkg://fuchsia.com/core#meta/core.cm".to_string()),
                    instance_id: None,
                    resolved_info: Some(fsys::ResolvedInfo {
                        resolved_url: Some(
                            "fuchsia-pkg://fuchsia.com/core#meta/core.cm".to_string(),
                        ),
                        execution_info: Some(fsys::ExecutionInfo {
                            start_reason: Some("Debugging Workflow".to_string()),
                            ..Default::default()
                        }),
                        ..Default::default()
                    }),
                    ..Default::default()
                },
                fsys::Instance {
                    moniker: Some("./core/appmgr".to_string()),
                    url: Some("fuchsia-pkg://fuchsia.com/appmgr#meta/appmgr.cm".to_string()),
                    instance_id: None,
                    resolved_info: Some(fsys::ResolvedInfo {
                        resolved_url: Some(
                            "fuchsia-pkg://fuchsia.com/appmgr#meta/appmgr.cm".to_string(),
                        ),
                        execution_info: Some(fsys::ExecutionInfo {
                            start_reason: Some("Debugging Workflow".to_string()),
                            ..Default::default()
                        }),
                        ..Default::default()
                    }),
                    ..Default::default()
                },
            ],
            HashMap::new(),
            HashMap::new(),
            HashMap::new(),
        );
        query
    }

    #[fuchsia::test]
    async fn no_filter() {
        let query = create_query();

        let instances = get_instances_matching_filter(None, &query).await.unwrap();
        assert_eq!(
            instances.iter().map(|i| i.moniker.clone()).collect::<Vec<_>>(),
            vec![
                Moniker::root(),
                Moniker::parse_str("/core").unwrap(),
                Moniker::parse_str("/core/appmgr").unwrap(),
            ]
        );
    }

    #[fuchsia::test]
    async fn running_only() {
        let query = create_query();

        let instances =
            get_instances_matching_filter(Some(ListFilter::Running), &query).await.unwrap();
        assert_eq!(
            instances.iter().map(|i| i.moniker.clone()).collect::<Vec<_>>(),
            vec![Moniker::parse_str("/core").unwrap(), Moniker::parse_str("/core/appmgr").unwrap(),]
        );
    }

    #[fuchsia::test]
    async fn stopped_only() {
        let query = create_query();

        let instances =
            get_instances_matching_filter(Some(ListFilter::Stopped), &query).await.unwrap();
        assert_eq!(
            instances.iter().map(|i| i.moniker.clone()).collect::<Vec<_>>(),
            [Moniker::root()]
        );
    }

    #[fuchsia::test]
    async fn descendants_only() {
        let query = create_query();

        let instances =
            get_instances_matching_filter(Some(ListFilter::Descendant("core".to_string())), &query)
                .await
                .unwrap();
        assert_eq!(
            instances.iter().map(|i| i.moniker.clone()).collect::<Vec<_>>(),
            vec![Moniker::parse_str("/core").unwrap(), Moniker::parse_str("/core/appmgr").unwrap(),]
        );
    }

    #[fuchsia::test]
    async fn ancestors_only() {
        let query = create_query();

        let instances =
            get_instances_matching_filter(Some(ListFilter::Ancestor("core".to_string())), &query)
                .await
                .unwrap();
        assert_eq!(
            instances.iter().map(|i| i.moniker.clone()).collect::<Vec<_>>(),
            vec![Moniker::root(), Moniker::parse_str("/core").unwrap()]
        );
    }

    #[fuchsia::test]
    async fn relative_only() {
        let query = create_query();

        let instances =
            get_instances_matching_filter(Some(ListFilter::Relative("core".to_string())), &query)
                .await
                .unwrap();
        assert_eq!(
            instances.iter().map(|i| i.moniker.clone()).collect::<Vec<_>>(),
            vec![
                Moniker::root(),
                Moniker::parse_str("/core").unwrap(),
                Moniker::parse_str("/core/appmgr").unwrap(),
            ]
        );
    }
}