helm-rs 0.1.1

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! Value sources for install, upgrade, template and lint.
//!
//! Helm merges `-f` files in the order given, then the `--set*` flags in a fixed order of kinds
//! (`pkg/cli/values/options.go`, v4.3.0): `--set-json`, `--set`, `--set-string`, `--set-file`,
//! `--set-literal`; within a kind, in the order given. [`Values`] keeps the same structure, so the
//! precedence is the one Helm applies.
//!
//! The `--set*` forms appear in the process argument list. They must never carry secrets; put
//! secret values in [`Values::json`] or [`Values::serialize`], which the CLI backend hands to Helm
//! through `0600` temporary files.

use std::fmt::{self, Display};
use std::path::{Path, PathBuf};

use serde::Serialize;

use crate::error::Result;

/// One `-f` source.
#[derive(Clone, PartialEq)]
pub enum ValuesFile {
    /// A file path.
    Path(PathBuf),
    /// A URL (Helm's `-f` accepts one).
    Url(String),
    /// An in-memory document; its contents are redacted in `Debug`.
    Json(serde_json::Value),
}

impl fmt::Debug for ValuesFile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Path(path) => f.debug_tuple("Path").field(path).finish(),
            Self::Url(url) => f.debug_tuple("Url").field(url).finish(),
            Self::Json(_) => f.write_str("Json(<redacted>)"),
        }
    }
}

/// Ordered value sources, built with the chained methods below.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Values {
    /// `-f` sources, lowest precedence first.
    pub files: Vec<ValuesFile>,
    /// `--set-json key=json` arguments.
    pub set_json: Vec<String>,
    /// `--set key=value` arguments.
    pub set: Vec<String>,
    /// `--set-string key=value` arguments.
    pub set_string: Vec<String>,
    /// `--set-file key=path` arguments.
    pub set_file: Vec<String>,
    /// `--set-literal key=value` arguments.
    pub set_literal: Vec<String>,
}

impl Values {
    /// No sources.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// True when there are no sources at all.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self == &Self::default()
    }

    /// Adds a values file (`-f path`).
    #[must_use]
    pub fn file(mut self, path: impl AsRef<Path>) -> Self {
        self.files.push(ValuesFile::Path(path.as_ref().to_owned()));

        self
    }

    /// Adds a values URL (`-f url`).
    #[must_use]
    pub fn url(mut self, url: impl Into<String>) -> Self {
        self.files.push(ValuesFile::Url(url.into()));

        self
    }

    /// Adds an in-memory document, delivered as a `0600` temporary file. Safe for secrets.
    #[must_use]
    pub fn json(mut self, value: serde_json::Value) -> Self {
        self.files.push(ValuesFile::Json(value));

        self
    }

    /// Adds any serializable value as an in-memory document. Safe for secrets.
    pub fn serialize(self, value: &impl Serialize) -> Result<Self> {
        Ok(self.json(serde_json::to_value(value)?))
    }

    /// Adds `--set key=value`. Visible in argv; no secrets.
    #[must_use]
    pub fn set(mut self, key: impl Display, value: impl Display) -> Self {
        self.set.push(format!("{key}={value}"));

        self
    }

    /// Adds `--set-string key=value`. Visible in argv; no secrets.
    #[must_use]
    pub fn set_string(mut self, key: impl Display, value: impl Display) -> Self {
        self.set_string.push(format!("{key}={value}"));

        self
    }

    /// Adds `--set-file key=path`, reading the value from the file.
    #[must_use]
    pub fn set_file(mut self, key: impl Display, path: impl AsRef<Path>) -> Self {
        self.set_file
            .push(format!("{key}={}", path.as_ref().display()));

        self
    }

    /// Adds `--set-json key=<json>`. Visible in argv; no secrets.
    #[must_use]
    pub fn set_json(mut self, key: impl Display, value: &serde_json::Value) -> Self {
        self.set_json.push(format!("{key}={value}"));

        self
    }

    /// Adds `--set-literal key=value`, which Helm takes verbatim (no comma or dot parsing).
    #[must_use]
    pub fn set_literal(mut self, key: impl Display, value: impl Display) -> Self {
        self.set_literal.push(format!("{key}={value}"));

        self
    }
}

#[cfg(test)]
mod tests;