Skip to main content

helm_rs/manifest/diff/
options.rs

1//! The options of a manifest diff, mirroring the flags of the `helm diff` plugin.
2
3use super::{SecretMasker, mask::MaskedValue};
4use crate::command::builder_collections;
5
6/// What a diff shows of `Secret` values (`--show-secrets` and `--show-secrets-decoded` of the
7/// helm-diff plugin, `diff/diff.go` `doDiff`, plus a masking policy of your own).
8#[derive(Debug, Clone, PartialEq, Eq, Default)]
9pub enum SecretMode {
10    /// Neither flag: `data` and `stringData` are merged into `data` and every value becomes a
11    /// marker that carries only the decoded length. Exactly [`SecretMasker::helm_diff`]: the
12    /// default is one masker among others.
13    #[default]
14    Redact,
15    /// `--show-secrets`: the manifest is shown as rendered, `data` still base64 encoded.
16    Show,
17    /// `--show-secrets-decoded`: the decoded values under `stringData`. Wins over [`Show`](Self::Show)
18    /// in the plugin, so it is a variant of its own here.
19    Decode,
20    /// Your own policy, applied like [`Redact`](Self::Redact) (merged into `data`, one masked
21    /// string per value). Structurally malformed data (`data` or `stringData` that is not a
22    /// mapping) never reaches the masker, which needs a key and bytes: it is replaced by the
23    /// fixed marker `[malformed secret data]`, because the API server rejects such a Secret.
24    Mask(SecretMasker),
25}
26
27/// The options of [`Diff::between`](super::Diff::between), mirroring `diff.Options` and the
28/// release flags of the helm-diff plugin (`diff/diff.go`, `cmd/options.go`, `cmd/upgrade.go`,
29/// `manifest/parse.go`, all at v3.15.15).
30///
31/// Not modelled, because they print, run a tool, or need a cluster: `--output`, `--diff-tool`,
32/// `--suppress-secrets` (equals suppressing the `Secret` kind), `--find-renames` (a rename is a
33/// removed plus an added object here), `--suppress-output-line-regex`, `--three-way-merge`,
34/// `--take-ownership`, `--revision`, `--detailed-exitcode` (see [`Diff::is_empty`](super::Diff::is_empty))
35/// and every flag that only steers rendering or the Helm connection.
36#[derive(Debug, Clone, PartialEq, Eq, Default, bon::Builder)]
37#[builder(on(String, into), on(bool, default))]
38#[allow(
39    clippy::struct_excessive_bools,
40    reason = "one field per helm-diff flag"
41)]
42pub struct DiffOptions {
43    /// Flag `--suppress` (repeatable): kinds whose changes are reported without content. The
44    /// object still shows as added, removed or changed, but its text is one line saying so and it
45    /// has no field changes. Compared with the kind exactly.
46    #[builder(field)]
47    pub suppress: Vec<String>,
48    /// Flags `--show-secrets` and `--show-secrets-decoded`; redacted by default.
49    #[builder(default)]
50    pub secrets: SecretMode,
51    /// Flag `--context`: lines of context around each change in the text diff. `None` (the
52    /// plugin's `-1`) keeps every line.
53    pub context: Option<usize>,
54    /// Flag `--strip-trailing-cr`: ignore a carriage return at the end of each line.
55    pub strip_trailing_cr: bool,
56    /// Flag `--normalize-manifests`: re-serialise every object (keys sorted, one canonical
57    /// style, comments including `# Source:` dropped) so that style differences do not show.
58    pub normalize_manifests: bool,
59    /// Flag `--no-hooks`: leave out every object that has a `helm.sh/hook` annotation. The
60    /// plugin diffs hooks by default.
61    pub no_hooks: bool,
62    /// Flag `--include-tests`: also diff hooks whose `helm.sh/hook` annotation is exactly `test`
63    /// or `test-success`, which the plugin leaves out by default.
64    pub include_tests: bool,
65}
66
67impl<S: diff_options_builder::State> DiffOptionsBuilder<S>
68where
69    S::Secrets: diff_options_builder::IsUnset,
70{
71    /// Masks Secret values with `mask`: sets [`DiffOptions::secrets`] to
72    /// [`SecretMode::Mask`]. The closure receives plaintext, see [`SecretMasker`].
73    pub fn mask_secrets(
74        self,
75        mask: impl Fn(&MaskedValue<'_>) -> String + Send + Sync + 'static,
76    ) -> DiffOptionsBuilder<diff_options_builder::SetSecrets<S>> {
77        self.secrets(SecretMode::Mask(SecretMasker::new(mask)))
78    }
79}
80
81builder_collections! {
82    DiffOptionsBuilder, diff_options_builder;
83    vec suppress / suppress_kind: String, "repeated flag `--suppress`", "the kinds whose content is hidden";
84}