helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! Loading a chart from wherever it lives: [`Chart::fetch`].

use std::path::{Path, PathBuf};

use tempfile::TempDir;

use super::{Chart, Error as ChartError};
use crate::backend::Execute;
use crate::command::{ChartPathOptions, ChartRef, Pull};
use crate::error::Result;
use crate::helm::Helm;

impl Chart {
    /// Loads the chart `chart` names, wherever it lives.
    ///
    /// A [`ChartRef::Path`] is read natively with [`Chart::load`]; no Helm process runs and
    /// `options` is not used. A repository, `oci://` or URL chart is downloaded with `helm pull`
    /// into a private temporary directory, which is removed again on every path (an error
    /// included), and the archive Helm wrote is loaded from there. Helm verifies the chart before
    /// the archive is read when `options.verify` is set, so a chart that fails verification is
    /// an error.
    ///
    /// Development versions are only a version constraint: Helm's `--devel` sets the version to
    /// `>0.0.0-0` when none is given (`pkg/cmd/pull.go`, Helm v4.3.0) and does nothing else, so
    /// set `options.version` to `>0.0.0-0` for them.
    ///
    /// ```no_run
    /// # async fn demo() -> helm_rs::Result<()> {
    /// use helm_rs::command::{ChartPathOptions, ChartRef};
    /// use helm_rs::{Chart, Helm};
    ///
    /// let helm = Helm::detect().await?;
    /// let options = ChartPathOptions::builder().version("^2.0.0").verify(true).build();
    /// let chart = Chart::fetch(&helm, &ChartRef::Oci("oci://registry.example/charts/web".into()), &options).await?;
    ///
    /// println!("{} {}", chart.name(), chart.metadata.version);
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    /// Whatever the backend's `pull` fails with, and any error of [`Chart::load`].
    pub async fn fetch<B: Execute<Pull>>(
        helm: &Helm<B>,
        chart: &ChartRef,
        options: &ChartPathOptions,
    ) -> Result<Self> {
        if let ChartRef::Path(path) = chart {
            return Ok(Self::load(path)?);
        }

        let dir = TempDir::new()?;
        let pull = Pull::builder(chart.clone())
            .chart_path(options.clone())
            .destination(dir.path())
            .build();

        helm.run(pull).await?;

        Ok(Self::load(find_archive(dir.path())?)?)
    }
}

/// The archive `helm pull` wrote into `dir`. Helm prints no path and names the file after the
/// last element of the chart URL (`pkg/downloader/chart_downloader.go`), so the archive is the
/// one file that is not the `.prov` file written next to it with `--verify` or `--prov`
/// (`pkg/action/pull.go`).
fn find_archive(dir: &Path) -> Result<PathBuf> {
    let mut archives = Vec::new();

    for entry in std::fs::read_dir(dir)? {
        let path = entry?.path();

        if path.extension().is_none_or(|extension| extension != "prov") {
            archives.push(path);
        }
    }

    match archives.len() {
        1 => Ok(archives.remove(0)),
        count => Err(ChartError::Archive(format!(
            "helm pull wrote {count} chart files into {}, expected one",
            dir.display()
        ))
        .into()),
    }
}

#[cfg(all(test, feature = "mock"))]
mod tests;