use crate::commands::types::*;
use crate::commands::utils;
use crate::text_formatter;
use crate::types::Error;
use argh::{ArgsInfo, FromArgs};
use derivative::Derivative;
use diagnostics_data::{Inspect, InspectData};
use serde::Serialize;
use std::cmp::Ordering;
use std::fmt;
use std::ops::Deref;
#[derive(Derivative, Serialize, PartialEq)]
#[derivative(Eq)]
pub struct ShowResultItem(InspectData);
impl Deref for ShowResultItem {
type Target = InspectData;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl PartialOrd for ShowResultItem {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ShowResultItem {
fn cmp(&self, other: &Self) -> Ordering {
self.moniker.cmp(&other.moniker).then_with(|| {
let this_name = self.metadata.name.as_ref();
let other_name = other.metadata.name.as_ref();
this_name.cmp(other_name)
})
}
}
#[derive(Serialize)]
pub struct ShowResult(Vec<ShowResultItem>);
impl fmt::Display for ShowResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for item in self.0.iter() {
text_formatter::output_schema(f, &item.0)?;
}
Ok(())
}
}
#[derive(ArgsInfo, FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "show")]
pub struct ShowCommand {
#[argh(positional)]
pub selectors: Vec<String>,
#[argh(option)]
pub manifest: Option<String>,
#[argh(option)]
pub component: Option<String>,
#[argh(option)]
pub accessor: Option<String>,
#[argh(option)]
pub name: Option<String>,
}
impl Command for ShowCommand {
type Result = ShowResult;
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");
}
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, self.name)?;
let inspect_data_iter =
provider.snapshot::<Inspect>(self.accessor.as_deref(), selectors).await?.into_iter();
let mut results = inspect_data_iter
.map(|mut d: InspectData| {
if let Some(hierarchy) = &mut d.payload {
hierarchy.sort();
}
ShowResultItem(d)
})
.collect::<Vec<_>>();
results.sort();
Ok(ShowResult(results))
}
}