Skip to main content

helm_rs/chart/
fetch.rs

1//! Loading a chart from wherever it lives: [`Chart::fetch`].
2
3use std::path::{Path, PathBuf};
4
5use tempfile::TempDir;
6
7use super::{Chart, Error as ChartError};
8use crate::backend::Execute;
9use crate::command::{ChartPathOptions, ChartRef, Pull};
10use crate::error::Result;
11use crate::helm::Helm;
12
13impl Chart {
14    /// Loads the chart `chart` names, wherever it lives.
15    ///
16    /// A [`ChartRef::Path`] is read natively with [`Chart::load`]; no Helm process runs and
17    /// `options` is not used. A repository, `oci://` or URL chart is downloaded with `helm pull`
18    /// into a private temporary directory, which is removed again on every path (an error
19    /// included), and the archive Helm wrote is loaded from there. Helm verifies the chart before
20    /// the archive is read when `options.verify` is set, so a chart that fails verification is
21    /// an error.
22    ///
23    /// Development versions are only a version constraint: Helm's `--devel` sets the version to
24    /// `>0.0.0-0` when none is given (`pkg/cmd/pull.go`, Helm v4.3.0) and does nothing else, so
25    /// set `options.version` to `>0.0.0-0` for them.
26    ///
27    /// ```no_run
28    /// # async fn demo() -> helm_rs::Result<()> {
29    /// use helm_rs::command::{ChartPathOptions, ChartRef};
30    /// use helm_rs::{Chart, Helm};
31    ///
32    /// let helm = Helm::detect().await?;
33    /// let options = ChartPathOptions::builder().version("^2.0.0").verify(true).build();
34    /// let chart = Chart::fetch(&helm, &ChartRef::Oci("oci://registry.example/charts/web".into()), &options).await?;
35    ///
36    /// println!("{} {}", chart.name(), chart.metadata.version);
37    /// # Ok(()) }
38    /// ```
39    ///
40    /// # Errors
41    /// Whatever the backend's `pull` fails with, and any error of [`Chart::load`].
42    pub async fn fetch<B: Execute<Pull>>(
43        helm: &Helm<B>,
44        chart: &ChartRef,
45        options: &ChartPathOptions,
46    ) -> Result<Self> {
47        if let ChartRef::Path(path) = chart {
48            return Ok(Self::load(path)?);
49        }
50
51        let dir = TempDir::new()?;
52        let pull = Pull::builder(chart.clone())
53            .chart_path(options.clone())
54            .destination(dir.path())
55            .build();
56
57        helm.run(pull).await?;
58
59        Ok(Self::load(find_archive(dir.path())?)?)
60    }
61}
62
63/// The archive `helm pull` wrote into `dir`. Helm prints no path and names the file after the
64/// last element of the chart URL (`pkg/downloader/chart_downloader.go`), so the archive is the
65/// one file that is not the `.prov` file written next to it with `--verify` or `--prov`
66/// (`pkg/action/pull.go`).
67fn find_archive(dir: &Path) -> Result<PathBuf> {
68    let mut archives = Vec::new();
69
70    for entry in std::fs::read_dir(dir)? {
71        let path = entry?.path();
72
73        if path.extension().is_none_or(|extension| extension != "prov") {
74            archives.push(path);
75        }
76    }
77
78    match archives.len() {
79        1 => Ok(archives.remove(0)),
80        count => Err(ChartError::Archive(format!(
81            "helm pull wrote {count} chart files into {}, expected one",
82            dir.display()
83        ))
84        .into()),
85    }
86}
87
88#[cfg(all(test, feature = "mock"))]
89mod tests;