Skip to main content

helm_rs/cli/
show.rs

1use super::{Cli, CliCommand, Invocation, chart_path, flags};
2use crate::backend::Execute;
3use crate::chart::Metadata;
4use crate::command::{
5    ChartInfo, ChartPathOptions, ChartRef, ShowAll, ShowChart, ShowCrds, ShowReadme, ShowValues,
6};
7use crate::error::{Error, Result};
8use crate::manifest::Manifest;
9use crate::options::GlobalOptions;
10
11#[cfg(test)]
12mod tests;
13
14/// Writes `show <what> CHART`, the chart download flags and `--devel`.
15fn show_args(
16    inv: &mut Invocation,
17    what: &str,
18    chart: &ChartRef,
19    options: &ChartPathOptions,
20    devel: bool,
21) -> Result<()> {
22    inv.arg("show");
23    inv.arg(what);
24    inv.arg(chart.as_os_str());
25
26    chart_path(inv, chart, options)?;
27
28    inv.switch("--devel", devel);
29
30    Ok(())
31}
32
33/// What Helm printed for an optional file: nothing when the chart has none, else the file and one
34/// added newline.
35fn file_text(stdout: &str) -> Option<String> {
36    (!stdout.is_empty()).then(|| stdout.strip_suffix('\n').unwrap_or(stdout).to_owned())
37}
38
39/// Parses the `Chart.yaml` metadata Helm prints for `command`.
40fn parse_metadata(command: &'static str, text: &str) -> Result<Metadata> {
41    serde_norway::from_str(text).map_err(|e| Error::Parse {
42        command,
43        message: e.to_string(),
44    })
45}
46
47impl CliCommand for ShowChart {
48    fn build(&self, inv: &mut Invocation) -> Result<()> {
49        show_args(inv, "chart", &self.chart, &self.chart_path, self.devel)
50    }
51
52    fn parse(&self, stdout: String) -> Result<Metadata> {
53        parse_metadata("helm show chart", &stdout)
54    }
55}
56
57impl CliCommand for ShowReadme {
58    fn build(&self, inv: &mut Invocation) -> Result<()> {
59        show_args(inv, "readme", &self.chart, &self.chart_path, self.devel)
60    }
61
62    fn parse(&self, stdout: String) -> Result<Option<String>> {
63        Ok(file_text(&stdout))
64    }
65}
66
67impl CliCommand for ShowCrds {
68    fn build(&self, inv: &mut Invocation) -> Result<()> {
69        show_args(inv, "crds", &self.chart, &self.chart_path, self.devel)
70    }
71
72    fn parse(&self, stdout: String) -> Result<Manifest> {
73        Ok(Manifest::parse(&stdout)?)
74    }
75}
76
77impl CliCommand for ShowValues {
78    fn build(&self, inv: &mut Invocation) -> Result<()> {
79        show_args(inv, "values", &self.chart, &self.chart_path, self.devel)?;
80
81        flags! { inv, self;
82            option("--jsonpath", jsonpath);
83        }
84
85        Ok(())
86    }
87
88    fn parse(&self, stdout: String) -> Result<Option<String>> {
89        if self.jsonpath.is_some() {
90            return Ok((!stdout.is_empty()).then_some(stdout));
91        }
92
93        Ok(file_text(&stdout))
94    }
95}
96
97/// The same chart, download flags and `--devel` as a `ShowAll`, for the command `$command`.
98macro_rules! part {
99    ($all:ident, $command:ident $(, $extra:ident: $value:expr)*) => {
100        $command {
101            chart: $all.chart.clone(),
102            chart_path: $all.chart_path.clone(),
103            devel: $all.devel,
104            $($extra: $value,)*
105        }
106    };
107}
108
109impl Execute<ShowAll> for Cli {
110    async fn execute(&self, options: &GlobalOptions, command: &ShowAll) -> Result<ChartInfo> {
111        let chart = part!(command, ShowChart);
112        let values = part!(command, ShowValues, jsonpath: None);
113        let readme = part!(command, ShowReadme);
114        let crds = part!(command, ShowCrds);
115        let (metadata, values, readme, crds) = tokio::try_join!(
116            self.execute(options, &chart),
117            self.execute(options, &values),
118            self.execute(options, &readme),
119            self.execute(options, &crds),
120        )?;
121
122        Ok(ChartInfo {
123            metadata,
124            values,
125            readme,
126            crds,
127        })
128    }
129}