Skip to main content

helm_rs/options/
mod.rs

1//! Helm's global flags and environment settings.
2//!
3//! Mirrors `pkg/cli/environment.go` (Helm v4.3.0) and the `helm env` output. Each field says
4//! whether the CLI backend passes it as a flag or as an environment variable.
5
6use std::fmt;
7use std::path::PathBuf;
8
9use crate::command::builder_collections;
10
11/// A secret string: redacted in `Debug`, no `Display`, read with [`Secret::expose`].
12#[derive(Clone, PartialEq, Eq)]
13pub struct Secret(String);
14
15impl Secret {
16    /// The secret value. Callers take care not to log it.
17    #[must_use]
18    pub fn expose(&self) -> &str {
19        &self.0
20    }
21}
22
23impl From<String> for Secret {
24    fn from(value: String) -> Self {
25        Self(value)
26    }
27}
28
29impl From<&str> for Secret {
30    fn from(value: &str) -> Self {
31        Self(value.to_owned())
32    }
33}
34
35impl fmt::Debug for Secret {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        f.write_str("Secret(<redacted>)")
38    }
39}
40
41/// Colored output mode (`--color`; `--colour` is the same flag).
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum Color {
44    /// Never color.
45    Never,
46    /// Color when attached to a terminal.
47    Auto,
48    /// Always color.
49    Always,
50}
51
52impl Color {
53    /// The flag value Helm expects.
54    #[must_use]
55    pub fn as_str(self) -> &'static str {
56        match self {
57            Self::Never => "never",
58            Self::Auto => "auto",
59            Self::Always => "always",
60        }
61    }
62}
63
64/// The release storage driver (`HELM_DRIVER`).
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub enum Driver {
67    /// Releases in Secrets (Helm's default).
68    Secret,
69    /// Releases in `ConfigMap`s.
70    ConfigMap,
71    /// Releases in memory (see `memory_driver_data`).
72    Memory,
73    /// Releases in a SQL database (see `driver_sql_connection_string`).
74    Sql,
75}
76
77impl Driver {
78    /// The `HELM_DRIVER` value.
79    #[must_use]
80    pub fn as_str(self) -> &'static str {
81        match self {
82            Self::Secret => "secret",
83            Self::ConfigMap => "configmap",
84            Self::Memory => "memory",
85            Self::Sql => "sql",
86        }
87    }
88}
89
90/// Settings applied to every command. `None`/`false`/empty means "leave to Helm's default".
91///
92/// `GlobalOptions::builder()` has one setter per field, like the command builders; the repeated
93/// ones also have a setter for a single item.
94#[derive(Debug, Clone, Default, PartialEq, bon::Builder)]
95#[builder(
96    on(String, into),
97    on(PathBuf, into),
98    on(Secret, into),
99    on(bool, default)
100)]
101pub struct GlobalOptions {
102    /// Flag `--kube-as-group`, repeated.
103    #[builder(field)]
104    pub kube_as_group: Vec<String>,
105    /// Env `HELM_MEMORY_DRIVER_DATA`, a colon-separated list of files for the memory driver.
106    #[builder(field)]
107    pub memory_driver_data: Vec<PathBuf>,
108    /// Flag `--kubeconfig`. When unset, helm reads `KUBECONFIG` from the environment, then
109    /// `~/.kube/config`, the same resolution as `kubectl` and `kube::Client::try_default`.
110    pub kubeconfig: Option<PathBuf>,
111    /// Flag `--kube-context`.
112    pub kube_context: Option<String>,
113    /// Flag `--namespace`.
114    pub namespace: Option<String>,
115    /// Flag `--kube-apiserver`.
116    pub kube_apiserver: Option<String>,
117    /// Flag `--kube-as-user`.
118    pub kube_as_user: Option<String>,
119    /// Flag `--kube-ca-file`.
120    pub kube_ca_file: Option<PathBuf>,
121    /// Flag `--kube-insecure-skip-tls-verify`.
122    pub kube_insecure_skip_tls_verify: bool,
123    /// Flag `--kube-tls-server-name`.
124    pub kube_tls_server_name: Option<String>,
125    /// Env `HELM_KUBETOKEN` (never argv, so it stays out of the process list).
126    pub kube_token: Option<Secret>,
127    /// Flag `--qps`.
128    pub qps: Option<f32>,
129    /// Flag `--burst-limit`; Helm accepts `-1` to disable throttling.
130    pub burst_limit: Option<i32>,
131    /// Flag `--debug`.
132    pub debug: bool,
133    /// Flag `--color` (`--colour` is the same flag).
134    pub color: Option<Color>,
135    /// Flag `--registry-config`.
136    pub registry_config: Option<PathBuf>,
137    /// Flag `--repository-config`.
138    pub repository_config: Option<PathBuf>,
139    /// Flag `--repository-cache`.
140    pub repository_cache: Option<PathBuf>,
141    /// Flag `--content-cache`.
142    pub content_cache: Option<PathBuf>,
143    /// Env `HELM_CACHE_HOME`.
144    pub cache_home: Option<PathBuf>,
145    /// Env `HELM_CONFIG_HOME`.
146    pub config_home: Option<PathBuf>,
147    /// Env `HELM_DATA_HOME`.
148    pub data_home: Option<PathBuf>,
149    /// Env `HELM_DRIVER`.
150    pub driver: Option<Driver>,
151    /// Env `HELM_DRIVER_SQL_CONNECTION_STRING`.
152    pub driver_sql_connection_string: Option<Secret>,
153    /// Env `HELM_MAX_HISTORY`.
154    pub max_history: Option<u32>,
155    /// Env `HELM_PLUGINS`.
156    pub plugins: Option<PathBuf>,
157    /// Env `HELM_NO_PLUGINS` (`1` when true).
158    pub no_plugins: bool,
159}
160
161builder_collections! {
162    GlobalOptionsBuilder, global_options_builder;
163    vec kube_as_group / add_kube_as_group: String, "repeated flag `--kube-as-group`", "the groups to impersonate";
164    vec memory_driver_data / memory_driver_file: PathBuf, "`HELM_MEMORY_DRIVER_DATA` files", "the files the memory driver loads";
165}
166
167#[cfg(test)]
168mod tests;