helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! Helm's global flags and environment settings.
//!
//! Mirrors `pkg/cli/environment.go` (Helm v4.3.0) and the `helm env` output. Each field says
//! whether the CLI backend passes it as a flag or as an environment variable.

use std::fmt;
use std::path::PathBuf;

use crate::command::builder_collections;

/// A secret string: redacted in `Debug`, no `Display`, read with [`Secret::expose`].
#[derive(Clone, PartialEq, Eq)]
pub struct Secret(String);

impl Secret {
    /// The secret value. Callers take care not to log it.
    #[must_use]
    pub fn expose(&self) -> &str {
        &self.0
    }
}

impl From<String> for Secret {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl From<&str> for Secret {
    fn from(value: &str) -> Self {
        Self(value.to_owned())
    }
}

impl fmt::Debug for Secret {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Secret(<redacted>)")
    }
}

/// Colored output mode (`--color`; `--colour` is the same flag).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Color {
    /// Never color.
    Never,
    /// Color when attached to a terminal.
    Auto,
    /// Always color.
    Always,
}

impl Color {
    /// The flag value Helm expects.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Never => "never",
            Self::Auto => "auto",
            Self::Always => "always",
        }
    }
}

/// The release storage driver (`HELM_DRIVER`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Driver {
    /// Releases in Secrets (Helm's default).
    Secret,
    /// Releases in `ConfigMap`s.
    ConfigMap,
    /// Releases in memory (see `memory_driver_data`).
    Memory,
    /// Releases in a SQL database (see `driver_sql_connection_string`).
    Sql,
}

impl Driver {
    /// The `HELM_DRIVER` value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Secret => "secret",
            Self::ConfigMap => "configmap",
            Self::Memory => "memory",
            Self::Sql => "sql",
        }
    }
}

/// Settings applied to every command. `None`/`false`/empty means "leave to Helm's default".
///
/// `GlobalOptions::builder()` has one setter per field, like the command builders; the repeated
/// ones also have a setter for a single item.
#[derive(Debug, Clone, Default, PartialEq, bon::Builder)]
#[builder(
    on(String, into),
    on(PathBuf, into),
    on(Secret, into),
    on(bool, default)
)]
pub struct GlobalOptions {
    /// Flag `--kube-as-group`, repeated.
    #[builder(field)]
    pub kube_as_group: Vec<String>,
    /// Env `HELM_MEMORY_DRIVER_DATA`, a colon-separated list of files for the memory driver.
    #[builder(field)]
    pub memory_driver_data: Vec<PathBuf>,
    /// Flag `--kubeconfig`. When unset, helm reads `KUBECONFIG` from the environment, then
    /// `~/.kube/config`, the same resolution as `kubectl` and `kube::Client::try_default`.
    pub kubeconfig: Option<PathBuf>,
    /// Flag `--kube-context`.
    pub kube_context: Option<String>,
    /// Flag `--namespace`.
    pub namespace: Option<String>,
    /// Flag `--kube-apiserver`.
    pub kube_apiserver: Option<String>,
    /// Flag `--kube-as-user`.
    pub kube_as_user: Option<String>,
    /// Flag `--kube-ca-file`.
    pub kube_ca_file: Option<PathBuf>,
    /// Flag `--kube-insecure-skip-tls-verify`.
    pub kube_insecure_skip_tls_verify: bool,
    /// Flag `--kube-tls-server-name`.
    pub kube_tls_server_name: Option<String>,
    /// Env `HELM_KUBETOKEN` (never argv, so it stays out of the process list).
    pub kube_token: Option<Secret>,
    /// Flag `--qps`.
    pub qps: Option<f32>,
    /// Flag `--burst-limit`; Helm accepts `-1` to disable throttling.
    pub burst_limit: Option<i32>,
    /// Flag `--debug`.
    pub debug: bool,
    /// Flag `--color` (`--colour` is the same flag).
    pub color: Option<Color>,
    /// Flag `--registry-config`.
    pub registry_config: Option<PathBuf>,
    /// Flag `--repository-config`.
    pub repository_config: Option<PathBuf>,
    /// Flag `--repository-cache`.
    pub repository_cache: Option<PathBuf>,
    /// Flag `--content-cache`.
    pub content_cache: Option<PathBuf>,
    /// Env `HELM_CACHE_HOME`.
    pub cache_home: Option<PathBuf>,
    /// Env `HELM_CONFIG_HOME`.
    pub config_home: Option<PathBuf>,
    /// Env `HELM_DATA_HOME`.
    pub data_home: Option<PathBuf>,
    /// Env `HELM_DRIVER`.
    pub driver: Option<Driver>,
    /// Env `HELM_DRIVER_SQL_CONNECTION_STRING`.
    pub driver_sql_connection_string: Option<Secret>,
    /// Env `HELM_MAX_HISTORY`.
    pub max_history: Option<u32>,
    /// Env `HELM_PLUGINS`.
    pub plugins: Option<PathBuf>,
    /// Env `HELM_NO_PLUGINS` (`1` when true).
    pub no_plugins: bool,
}

builder_collections! {
    GlobalOptionsBuilder, global_options_builder;
    vec kube_as_group / add_kube_as_group: String, "repeated flag `--kube-as-group`", "the groups to impersonate";
    vec memory_driver_data / memory_driver_file: PathBuf, "`HELM_MEMORY_DRIVER_DATA` files", "the files the memory driver loads";
}

#[cfg(test)]
mod tests;