component_debug/cli/
collection.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
// 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, get_resolved_declaration, Durability};
use anyhow::{bail, Result};
use cm_rust::{OfferDeclCommon, OfferTarget};
use fidl_fuchsia_sys2 as fsys;
use moniker::Moniker;
use prettytable::format::consts::FORMAT_CLEAN;
use prettytable::{cell, row, Table};

struct Collection {
    name: String,
    moniker: Moniker,
    durability: Durability,
    environment: Option<String>,
    offered_capabilities: Vec<String>,
}

pub async fn collection_list_cmd<W: std::io::Write>(
    realm_query: fsys::RealmQueryProxy,
    mut writer: W,
) -> Result<()> {
    let collections = get_all_collections(&realm_query).await?;

    let table = create_table(collections);
    table.print(&mut writer)?;

    Ok(())
}

pub async fn collection_show_cmd<W: std::io::Write>(
    query: String,
    realm_query: fsys::RealmQueryProxy,
    mut writer: W,
) -> Result<()> {
    let collections = get_all_collections(&realm_query).await?;

    let filtered_collections: Vec<Collection> =
        collections.into_iter().filter(|c| c.name.contains(&query)).collect();

    if filtered_collections.is_empty() {
        bail!("No collections found for query \"{}\"", query);
    }

    for collection in filtered_collections {
        let table = create_verbose_table(&collection);
        table.print(&mut writer)?;
        writeln!(writer, "")?;
    }

    Ok(())
}

async fn get_all_collections(realm_query: &fsys::RealmQueryProxy) -> Result<Vec<Collection>> {
    let instances = get_all_instances(realm_query).await?;
    let mut collections = vec![];

    for instance in instances {
        if instance.resolved_info.is_some() {
            let mut instance_collections =
                get_all_collections_of_instance(&instance.moniker, realm_query).await?;
            collections.append(&mut instance_collections);
        }
    }

    Ok(collections)
}

async fn get_all_collections_of_instance(
    moniker: &Moniker,
    realm_query: &fsys::RealmQueryProxy,
) -> Result<Vec<Collection>> {
    let manifest = get_resolved_declaration(moniker, realm_query).await?;
    let mut collections = vec![];

    for collection in manifest.collections {
        let mut offered_capabilities = vec![];

        for offer in &manifest.offers {
            match offer.target() {
                OfferTarget::Collection(name) => {
                    if name == &collection.name {
                        offered_capabilities.push(offer.target_name().to_string());
                    }
                }
                _ => {}
            }
        }

        collections.push(Collection {
            name: collection.name.to_string(),
            moniker: moniker.clone(),
            durability: collection.durability.into(),
            environment: collection.environment.map(|e| e.to_string()),
            offered_capabilities,
        });
    }

    Ok(collections)
}

fn create_table(collections: Vec<Collection>) -> Table {
    let mut table = Table::new();
    table.set_format(*FORMAT_CLEAN);
    table.set_titles(row!("Moniker", "Name", "Durability", "Environment"));

    for collection in collections {
        let environment = collection.environment.unwrap_or_else(|| "N/A".to_string());
        table.add_row(row!(
            collection.moniker.to_string(),
            collection.name,
            collection.durability.to_string(),
            environment
        ));
    }

    table
}

fn create_verbose_table(collection: &Collection) -> Table {
    let mut table = Table::new();
    table.set_format(*FORMAT_CLEAN);
    table.add_row(row!(r->"Moniker:", collection.moniker.to_string()));
    table.add_row(row!(r->"Collection Name:", collection.name));
    table.add_row(row!(r->"Durability:", collection.durability.to_string()));

    let environment = collection.environment.clone().unwrap_or_else(|| "N/A".to_string());
    table.add_row(row!(r->"Environment:", environment));

    let offered_capabilities = collection.offered_capabilities.join("\n");
    table.add_row(row!(r->"Offered Capabilities:", offered_capabilities));

    table
}

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

    fn create_query() -> fsys::RealmQueryProxy {
        // Serve RealmQuery for CML components.
        let query = serve_realm_query(
            vec![fsys::Instance {
                moniker: Some("./my_foo".to_string()),
                url: Some("fuchsia-pkg://fuchsia.com/foo#meta/foo.cm".to_string()),
                instance_id: Some("1234567890".to_string()),
                resolved_info: Some(fsys::ResolvedInfo {
                    resolved_url: Some("fuchsia-pkg://fuchsia.com/foo#meta/foo.cm".to_string()),
                    execution_info: None,
                    ..Default::default()
                }),
                ..Default::default()
            }],
            HashMap::from([(
                "./my_foo".to_string(),
                fdecl::Component {
                    offers: Some(vec![fdecl::Offer::Protocol(fdecl::OfferProtocol {
                        source: Some(fdecl::Ref::Parent(fdecl::ParentRef)),
                        source_name: Some("fuchsia.foo.bar".to_string()),
                        target: Some(fdecl::Ref::Collection(fdecl::CollectionRef {
                            name: "coll1".to_string(),
                        })),
                        target_name: Some("fuchsia.foo.bar".to_string()),
                        dependency_type: Some(fdecl::DependencyType::Strong),
                        availability: Some(fdecl::Availability::Required),
                        ..Default::default()
                    })]),
                    collections: Some(vec![fdecl::Collection {
                        name: Some("coll1".to_string()),
                        durability: Some(fdecl::Durability::Transient),
                        ..Default::default()
                    }]),
                    ..Default::default()
                },
            )]),
            HashMap::from([]),
            HashMap::from([]),
        );
        query
    }

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

        let mut collections = get_all_collections(&query).await.unwrap();

        assert_eq!(collections.len(), 1);

        let collection = collections.remove(0);

        assert_eq!(collection.name, "coll1");
        assert_eq!(collection.moniker, Moniker::parse_str("/my_foo").unwrap());
        assert_eq!(collection.durability, Durability::Transient);
        assert!(collection.environment.is_none());
        assert_eq!(collection.offered_capabilities, vec!["fuchsia.foo.bar"]);
    }
}