iquery/commands/
selectors.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
// Copyright 2020 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::commands::types::*;
use crate::commands::utils;
use crate::types::Error;
use argh::{ArgsInfo, FromArgs};
use diagnostics_data::{Inspect, InspectData, InspectHandleName};
use diagnostics_hierarchy::DiagnosticsHierarchy;
use fidl_fuchsia_diagnostics as fdiagnostics;
use serde::ser::{Error as _, SerializeSeq};
use serde::{Serialize, Serializer};
use std::fmt;

/// Lists all available selectors for the given input of component queries or partial selectors.
#[derive(ArgsInfo, FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "selectors")]
pub struct SelectorsCommand {
    #[argh(positional)]
    /// component query, component selector, or component and tree selector. Minimum: 1 unless
    /// `--component` is set. When `--component` is provided then the selectors should be tree
    /// selectors, otherwise they can be component selectors or component and tree selectors.
    /// Full selectors (including a property segment) are allowed but not informative.
    pub selectors: Vec<String>,

    #[argh(option)]
    /// DEPRECATED: use `--component` instead.
    pub manifest: Option<String>,

    #[argh(option)]
    /// a fuzzy-search query. May include URL, moniker, or manifest fragments. No selector-escaping
    /// for moniker is needed in this query. Selectors following --component should omit the
    /// component selector, as they will be spliced together by the tool with the correct escaping.
    pub component: Option<String>,

    #[argh(option)]
    /// A string specifying what `fuchsia.diagnostics.ArchiveAccessor` to connect to.
    /// This can be copied from the output of `ffx inspect list-accessors`.
    /// The selector will be in the form of:
    /// <moniker>:fuchsia.diagnostics.ArchiveAccessor.pipeline_name
    pub accessor: Option<String>,
}

impl Command for SelectorsCommand {
    type Result = SelectorsResult;

    async fn execute<P: DiagnosticsProvider>(self, provider: &P) -> Result<Self::Result, Error> {
        if self.manifest.is_some() {
            panic!("ERROR: option `--manifest` is deprecated, please use `--component` instead");
        }

        if self.selectors.is_empty() && self.component.is_none() && self.manifest.is_none() {
            return Err(Error::invalid_arguments(
                "Expected 1 or more component queries or tree selectors. Got zero.",
            ));
        }

        let mut selectors = if let Some(component) = self.component {
            utils::process_component_query_with_partial_selectors(
                component,
                self.selectors.into_iter(),
                provider,
            )
            .await?
        } else if let Some(manifest) = self.manifest {
            utils::get_selectors_for_manifest(
                manifest,
                self.selectors,
                self.accessor.clone(),
                provider,
            )
            .await?
        } else {
            utils::process_fuzzy_inputs(self.selectors, provider).await?
        };

        utils::ensure_tree_field_is_set(&mut selectors, None)?;
        let mut results =
            provider.snapshot::<Inspect>(self.accessor.as_deref(), selectors.into_iter()).await?;
        for result in results.iter_mut() {
            if let Some(hierarchy) = &mut result.payload {
                hierarchy.sort();
            }
        }
        Ok(SelectorsResult(inspect_to_selectors(results)))
    }
}

pub struct SelectorsResult(Vec<fdiagnostics::Selector>);

impl Serialize for SelectorsResult {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
        let mut stringified = self
            .0
            .iter()
            .map(|item| {
                selectors::selector_to_string(
                    item,
                    selectors::SelectorDisplayOptions::never_wrap_in_quotes(),
                )
                .map_err(|e| S::Error::custom(format!("failed to serialize: {:#?}", e)))
            })
            .collect::<Result<Vec<_>, _>>()?;
        stringified.sort();
        for item in stringified {
            seq.serialize_element(&item)?;
        }

        seq.end()
    }
}

impl fmt::Display for SelectorsResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut stringified = self
            .0
            .iter()
            .map(|item| {
                selectors::selector_to_string(item, selectors::SelectorDisplayOptions::default())
                    .map_err(|_| fmt::Error)
            })
            .collect::<Result<Vec<_>, _>>()?;
        stringified.sort();
        for item in stringified {
            writeln!(f, "{item}")?;
        }
        Ok(())
    }
}

fn get_selectors(
    moniker: String,
    hierarchy: DiagnosticsHierarchy,
    name: InspectHandleName,
) -> Vec<fdiagnostics::Selector> {
    hierarchy
        .property_iter()
        .flat_map(|(node_path, maybe_property)| maybe_property.map(|prop| (node_path, prop)))
        .map(|(node_path, property)| {
            let node_path = node_path
                .iter()
                .map(|s| fdiagnostics::StringSelector::ExactMatch(s.to_string()))
                .collect::<Vec<_>>();

            let target_properties =
                fdiagnostics::StringSelector::ExactMatch(property.name().to_string());

            let tree_selector = Some(fdiagnostics::TreeSelector::PropertySelector(
                fdiagnostics::PropertySelector { node_path, target_properties },
            ));

            let tree_names = Some(fdiagnostics::TreeNames::Some(vec![name.to_string()]));

            let component_selector = Some(fdiagnostics::ComponentSelector {
                moniker_segments: Some(
                    moniker
                        .split("/")
                        .map(|segment| {
                            fdiagnostics::StringSelector::ExactMatch(segment.to_string())
                        })
                        .collect(),
                ),
                ..Default::default()
            });

            fdiagnostics::Selector {
                component_selector,
                tree_selector,
                tree_names,
                ..Default::default()
            }
        })
        .collect()
}

fn inspect_to_selectors(inspect_data: Vec<InspectData>) -> Vec<fdiagnostics::Selector> {
    inspect_data
        .into_iter()
        .filter_map(|schema| {
            let moniker = schema.moniker;
            let name = schema.metadata.name;
            schema.payload.map(|hierarchy| get_selectors(moniker.to_string(), hierarchy, name))
        })
        .flatten()
        .collect()
}