Skip to main content

helm_rs/values/
mod.rs

1//! Value sources for install, upgrade, template and lint.
2//!
3//! Helm merges `-f` files in the order given, then the `--set*` flags in a fixed order of kinds
4//! (`pkg/cli/values/options.go`, v4.3.0): `--set-json`, `--set`, `--set-string`, `--set-file`,
5//! `--set-literal`; within a kind, in the order given. [`Values`] keeps the same structure, so the
6//! precedence is the one Helm applies.
7//!
8//! The `--set*` forms appear in the process argument list. They must never carry secrets; put
9//! secret values in [`Values::json`] or [`Values::serialize`], which the CLI backend hands to Helm
10//! through `0600` temporary files.
11
12use std::fmt::{self, Display};
13use std::path::{Path, PathBuf};
14
15use serde::Serialize;
16
17use crate::error::Result;
18
19/// One `-f` source.
20#[derive(Clone, PartialEq)]
21pub enum ValuesFile {
22    /// A file path.
23    Path(PathBuf),
24    /// A URL (Helm's `-f` accepts one).
25    Url(String),
26    /// An in-memory document; its contents are redacted in `Debug`.
27    Json(serde_json::Value),
28}
29
30impl fmt::Debug for ValuesFile {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::Path(path) => f.debug_tuple("Path").field(path).finish(),
34            Self::Url(url) => f.debug_tuple("Url").field(url).finish(),
35            Self::Json(_) => f.write_str("Json(<redacted>)"),
36        }
37    }
38}
39
40/// Ordered value sources, built with the chained methods below.
41#[derive(Debug, Clone, Default, PartialEq)]
42pub struct Values {
43    /// `-f` sources, lowest precedence first.
44    pub files: Vec<ValuesFile>,
45    /// `--set-json key=json` arguments.
46    pub set_json: Vec<String>,
47    /// `--set key=value` arguments.
48    pub set: Vec<String>,
49    /// `--set-string key=value` arguments.
50    pub set_string: Vec<String>,
51    /// `--set-file key=path` arguments.
52    pub set_file: Vec<String>,
53    /// `--set-literal key=value` arguments.
54    pub set_literal: Vec<String>,
55}
56
57impl Values {
58    /// No sources.
59    #[must_use]
60    pub fn new() -> Self {
61        Self::default()
62    }
63
64    /// True when there are no sources at all.
65    #[must_use]
66    pub fn is_empty(&self) -> bool {
67        self == &Self::default()
68    }
69
70    /// Adds a values file (`-f path`).
71    #[must_use]
72    pub fn file(mut self, path: impl AsRef<Path>) -> Self {
73        self.files.push(ValuesFile::Path(path.as_ref().to_owned()));
74
75        self
76    }
77
78    /// Adds a values URL (`-f url`).
79    #[must_use]
80    pub fn url(mut self, url: impl Into<String>) -> Self {
81        self.files.push(ValuesFile::Url(url.into()));
82
83        self
84    }
85
86    /// Adds an in-memory document, delivered as a `0600` temporary file. Safe for secrets.
87    #[must_use]
88    pub fn json(mut self, value: serde_json::Value) -> Self {
89        self.files.push(ValuesFile::Json(value));
90
91        self
92    }
93
94    /// Adds any serializable value as an in-memory document. Safe for secrets.
95    pub fn serialize(self, value: &impl Serialize) -> Result<Self> {
96        Ok(self.json(serde_json::to_value(value)?))
97    }
98
99    /// Adds `--set key=value`. Visible in argv; no secrets.
100    #[must_use]
101    pub fn set(mut self, key: impl Display, value: impl Display) -> Self {
102        self.set.push(format!("{key}={value}"));
103
104        self
105    }
106
107    /// Adds `--set-string key=value`. Visible in argv; no secrets.
108    #[must_use]
109    pub fn set_string(mut self, key: impl Display, value: impl Display) -> Self {
110        self.set_string.push(format!("{key}={value}"));
111
112        self
113    }
114
115    /// Adds `--set-file key=path`, reading the value from the file.
116    #[must_use]
117    pub fn set_file(mut self, key: impl Display, path: impl AsRef<Path>) -> Self {
118        self.set_file
119            .push(format!("{key}={}", path.as_ref().display()));
120
121        self
122    }
123
124    /// Adds `--set-json key=<json>`. Visible in argv; no secrets.
125    #[must_use]
126    pub fn set_json(mut self, key: impl Display, value: &serde_json::Value) -> Self {
127        self.set_json.push(format!("{key}={value}"));
128
129        self
130    }
131
132    /// Adds `--set-literal key=value`, which Helm takes verbatim (no comma or dot parsing).
133    #[must_use]
134    pub fn set_literal(mut self, key: impl Display, value: impl Display) -> Self {
135        self.set_literal.push(format!("{key}={value}"));
136
137        self
138    }
139}
140
141#[cfg(test)]
142mod tests;