use {
crate::{
commands::{types::*, utils},
text_formatter,
types::Error,
},
argh::{ArgsInfo, FromArgs},
async_trait::async_trait,
derivative::Derivative,
diagnostics_data::{Inspect, InspectData, InspectHandleName},
serde::Serialize,
std::{cmp::Ordering, fmt, 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().map(InspectHandleName::as_ref);
let other_name = other.metadata.name.as_ref().map(InspectHandleName::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(option)]
pub manifest: Option<String>,
#[argh(positional)]
pub selectors: Vec<String>,
#[argh(option)]
pub accessor: Option<String>,
}
#[async_trait]
impl Command for ShowCommand {
type Result = ShowResult;
async fn execute<P: DiagnosticsProvider>(&self, provider: &P) -> Result<Self::Result, Error> {
let selectors = utils::get_selectors_for_manifest(
&self.manifest,
&self.selectors,
&self.accessor,
provider,
)
.await?;
let selectors = utils::expand_selectors(selectors)?;
let inspect_data_iter =
provider.snapshot::<Inspect>(&self.accessor, &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))
}
}