1use crate::commands::types::*;
6use crate::commands::utils;
7use crate::text_formatter;
8use crate::types::Error;
9use argh::{ArgsInfo, FromArgs};
10use derivative::Derivative;
11use diagnostics_data::InspectData;
12use serde::Serialize;
13use std::cmp::Ordering;
14use std::fmt;
15use std::ops::Deref;
16
17#[derive(Derivative, Serialize, PartialEq)]
18#[derivative(Eq)]
19pub struct ShowResultItem(InspectData);
20
21impl Deref for ShowResultItem {
22 type Target = InspectData;
23
24 fn deref(&self) -> &Self::Target {
25 &self.0
26 }
27}
28
29impl PartialOrd for ShowResultItem {
30 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
31 Some(self.cmp(other))
32 }
33}
34
35impl Ord for ShowResultItem {
36 fn cmp(&self, other: &Self) -> Ordering {
37 self.moniker.cmp(&other.moniker).then_with(|| {
38 let this_name = self.metadata.name.as_ref();
39 let other_name = other.metadata.name.as_ref();
40 this_name.cmp(other_name)
41 })
42 }
43}
44
45#[derive(Serialize)]
46pub struct ShowResult(Vec<ShowResultItem>);
47
48impl fmt::Display for ShowResult {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 for item in self.0.iter() {
51 text_formatter::output_schema(f, &item.0)?;
52 }
53 Ok(())
54 }
55}
56
57#[derive(ArgsInfo, FromArgs, PartialEq, Debug)]
60#[argh(subcommand, name = "show")]
61pub struct ShowCommand {
62 #[argh(positional)]
63 pub selectors: Vec<String>,
68
69 #[argh(option)]
70 pub data: Vec<String>,
74
75 #[argh(option)]
76 pub accessor: Option<String>,
82
83 #[argh(option)]
84 pub name: Option<String>,
88}
89
90impl Command for ShowCommand {
91 type Result = ShowResult;
92
93 async fn execute<P: DiagnosticsProvider>(self, provider: &P) -> Result<Self::Result, Error> {
94 let mut selectors = if self.data.is_empty() {
95 utils::process_fuzzy_inputs(self.selectors, provider).await?
96 } else {
97 if self.selectors.len() != 1 {
98 return Err(Error::WrongNumberOfSearchQueriesForDataFlag);
99 }
100 utils::process_component_query_with_partial_selectors(
101 &self.selectors[0],
102 self.data.into_iter(),
103 provider,
104 )
105 .await?
106 };
107
108 utils::ensure_tree_field_is_set(&mut selectors, self.name)?;
109 let inspect_data_iter =
110 provider.snapshot(self.accessor.as_deref(), selectors).await?.into_iter();
111
112 let mut results = inspect_data_iter
113 .map(|mut d: InspectData| {
114 if let Some(hierarchy) = &mut d.payload {
115 hierarchy.sort();
116 }
117 ShowResultItem(d)
118 })
119 .collect::<Vec<_>>();
120
121 results.sort();
122 Ok(ShowResult(results))
123 }
124}