helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
use std::path::PathBuf;

use crate::backend::Command;
use crate::options::Secret;

/// The flags `helm dependency build` and `helm dependency update` share
/// (`addDependencySubcommandFlags`, `pkg/cmd/dependency.go`, Helm v4.3.0).
///
/// Helm uses `username` and `password` only for the registry client, that is for `oci://`
/// dependencies; repositories of other kinds authenticate through the repository config. The
/// `password` never reaches argv: the CLI backend writes it, with the username, to a `0600`
/// temporary registry config entry for the registry host of each `oci://` dependency of the chart,
/// and refuses the command when the chart has none.
#[derive(Debug, Clone, Default, PartialEq, bon::Builder)]
#[builder(
    on(String, into),
    on(PathBuf, into),
    on(Secret, into),
    on(bool, default)
)]
#[allow(clippy::struct_excessive_bools, reason = "one field per Helm flag")]
pub struct DependencyOptions {
    /// Flag `--verify`: verify the packages against signatures. `build` verifies when it can
    /// (`VerifyIfPossible`: a missing provenance file only warns); `update` always verifies.
    pub verify: bool,
    /// Flag `--keyring`: the keyring that holds the public keys.
    pub keyring: Option<PathBuf>,
    /// Flag `--skip-refresh`: do not refresh the local repository cache.
    pub skip_refresh: bool,
    /// Flag `--username`.
    pub username: Option<String>,
    /// Never a flag: see the type documentation.
    pub password: Option<Secret>,
    /// Flag `--cert-file`: the HTTPS client certificate.
    pub cert_file: Option<PathBuf>,
    /// Flag `--key-file`: the HTTPS client key.
    pub key_file: Option<PathBuf>,
    /// Flag `--ca-file`: the CA bundle that verifies HTTPS servers.
    pub ca_file: Option<PathBuf>,
    /// Flag `--insecure-skip-tls-verify`.
    pub insecure_skip_tls_verify: bool,
    /// Flag `--plain-http`: use insecure HTTP connections.
    pub plain_http: bool,
}

/// Declares a command that takes a chart directory and the [`DependencyOptions`].
macro_rules! dependency_command {
    ($(#[$meta:meta])* $name:ident) => {
        $(#[$meta])*
        #[derive(Debug, Clone, PartialEq, bon::Builder)]
        #[builder(on(PathBuf, into))]
        pub struct $name {
            /// The chart directory (`CHART`).
            #[builder(start_fn)]
            pub chart: PathBuf,
            /// The download and verification flags.
            #[builder(default)]
            pub options: DependencyOptions,
        }

        impl $name {
            /// Runs on the chart in `chart` with Helm's defaults.
            #[must_use]
            pub fn new(chart: impl Into<PathBuf>) -> Self {
                Self::builder(chart).build()
            }
        }

        impl Command for $name {
            type Output = ();
        }
    };
}

dependency_command! {
    /// `helm dependency build`: rebuilds `charts/` from `Chart.lock` without renegotiating
    /// versions; without a lock file it behaves like [`DependencyUpdate`]. Helm prints progress
    /// lines only, which are dropped.
    DependencyBuild
}

dependency_command! {
    /// `helm dependency update`: resolves the dependencies of `Chart.yaml`, downloads them into
    /// `charts/` and writes `Chart.lock`. Helm prints progress lines only, which are dropped.
    DependencyUpdate
}

/// `helm dependency list`: the dependencies a chart declares and whether `charts/` satisfies
/// them. Helm has no machine output, so the CLI backend parses the table; it always passes
/// `--max-col-width` with the largest value so that no cell is cut short.
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[builder(on(PathBuf, into))]
pub struct DependencyList {
    /// The chart directory or archive (`CHART`).
    #[builder(start_fn)]
    pub chart: PathBuf,
}

impl DependencyList {
    /// Lists the dependencies of the chart at `chart`.
    #[must_use]
    pub fn new(chart: impl Into<PathBuf>) -> Self {
        Self::builder(chart).build()
    }
}

impl Command for DependencyList {
    type Output = DependencyListing;
}

/// What `helm dependency list` printed (`pkg/action/dependency.go`, Helm v4.3.0).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DependencyListing {
    /// The table rows, in the order of `Chart.yaml`; empty when the chart declares none.
    pub dependencies: Vec<DependencyRow>,
    /// The warning lines: `WARNING: no dependencies at ...` when there are none, and one line
    /// for each chart in `charts/` that `Chart.yaml` does not declare or that is not a chart.
    pub warnings: Vec<String>,
}

/// One row of the `helm dependency list` table.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DependencyRow {
    /// The dependency name.
    pub name: String,
    /// The version or constraint `Chart.yaml` asks for.
    pub version: String,
    /// The repository URL; empty for a chart that lives in `charts/`.
    pub repository: String,
    /// How `charts/` relates to the dependency.
    pub status: DependencyStatus,
}

/// The status column of `helm dependency list` (`dependencyStatus` and `statArchiveForStatus`,
/// `pkg/action/dependency.go`): every string Helm v4.3.0 can print.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DependencyStatus {
    /// `ok`: a packaged `charts/NAME-*.tgz` satisfies the version.
    Ok,
    /// `unpacked`: an unpacked chart in `charts/` satisfies the version.
    Unpacked,
    /// `missing`: there is no such chart in `charts/`.
    Missing,
    /// `wrong version`: the chart in `charts/` does not satisfy the version constraint.
    WrongVersion,
    /// `invalid version`: the dependency's version or the chart's own is not valid semver.
    InvalidVersion,
    /// `corrupt`: a `charts/NAME-*.tgz` archive cannot be loaded.
    Corrupt,
    /// `misnamed`: a `charts/NAME-*.tgz` archive holds a chart of another name.
    Misnamed,
    /// `too many matches`: several `charts/NAME-VERSION.tgz` archives match.
    TooManyMatches,
    /// `bad pattern`: the dependency name is not a valid file glob.
    BadPattern,
}

impl DependencyStatus {
    /// The status for the text Helm prints, `None` for a string Helm v4.3.0 never prints.
    #[must_use]
    pub fn from_helm(text: &str) -> Option<Self> {
        Some(match text {
            "ok" => Self::Ok,
            "unpacked" => Self::Unpacked,
            "missing" => Self::Missing,
            "wrong version" => Self::WrongVersion,
            "invalid version" => Self::InvalidVersion,
            "corrupt" => Self::Corrupt,
            "misnamed" => Self::Misnamed,
            "too many matches" => Self::TooManyMatches,
            "bad pattern" => Self::BadPattern,
            _ => return None,
        })
    }
}