use super::{Cli, CliCommand, Invocation, chart_path, flags};
use crate::backend::Execute;
use crate::chart::Metadata;
use crate::command::{
ChartInfo, ChartPathOptions, ChartRef, ShowAll, ShowChart, ShowCrds, ShowReadme, ShowValues,
};
use crate::error::{Error, Result};
use crate::manifest::Manifest;
use crate::options::GlobalOptions;
#[cfg(test)]
mod tests;
fn show_args(
inv: &mut Invocation,
what: &str,
chart: &ChartRef,
options: &ChartPathOptions,
devel: bool,
) -> Result<()> {
inv.arg("show");
inv.arg(what);
inv.arg(chart.as_os_str());
chart_path(inv, chart, options)?;
inv.switch("--devel", devel);
Ok(())
}
fn file_text(stdout: &str) -> Option<String> {
(!stdout.is_empty()).then(|| stdout.strip_suffix('\n').unwrap_or(stdout).to_owned())
}
fn parse_metadata(command: &'static str, text: &str) -> Result<Metadata> {
serde_norway::from_str(text).map_err(|e| Error::Parse {
command,
message: e.to_string(),
})
}
impl CliCommand for ShowChart {
fn build(&self, inv: &mut Invocation) -> Result<()> {
show_args(inv, "chart", &self.chart, &self.chart_path, self.devel)
}
fn parse(&self, stdout: String) -> Result<Metadata> {
parse_metadata("helm show chart", &stdout)
}
}
impl CliCommand for ShowReadme {
fn build(&self, inv: &mut Invocation) -> Result<()> {
show_args(inv, "readme", &self.chart, &self.chart_path, self.devel)
}
fn parse(&self, stdout: String) -> Result<Option<String>> {
Ok(file_text(&stdout))
}
}
impl CliCommand for ShowCrds {
fn build(&self, inv: &mut Invocation) -> Result<()> {
show_args(inv, "crds", &self.chart, &self.chart_path, self.devel)
}
fn parse(&self, stdout: String) -> Result<Manifest> {
Ok(Manifest::parse(&stdout)?)
}
}
impl CliCommand for ShowValues {
fn build(&self, inv: &mut Invocation) -> Result<()> {
show_args(inv, "values", &self.chart, &self.chart_path, self.devel)?;
flags! { inv, self;
option("--jsonpath", jsonpath);
}
Ok(())
}
fn parse(&self, stdout: String) -> Result<Option<String>> {
if self.jsonpath.is_some() {
return Ok((!stdout.is_empty()).then_some(stdout));
}
Ok(file_text(&stdout))
}
}
macro_rules! part {
($all:ident, $command:ident $(, $extra:ident: $value:expr)*) => {
$command {
chart: $all.chart.clone(),
chart_path: $all.chart_path.clone(),
devel: $all.devel,
$($extra: $value,)*
}
};
}
impl Execute<ShowAll> for Cli {
async fn execute(&self, options: &GlobalOptions, command: &ShowAll) -> Result<ChartInfo> {
let chart = part!(command, ShowChart);
let values = part!(command, ShowValues, jsonpath: None);
let readme = part!(command, ShowReadme);
let crds = part!(command, ShowCrds);
let (metadata, values, readme, crds) = tokio::try_join!(
self.execute(options, &chart),
self.execute(options, &values),
self.execute(options, &readme),
self.execute(options, &crds),
)?;
Ok(ChartInfo {
metadata,
values,
readme,
crds,
})
}
}