helm-rs 0.1.0

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! The options of a manifest diff, mirroring the flags of the `helm diff` plugin.

use super::{SecretMasker, mask::MaskedValue};
use crate::command::builder_collections;

/// What a diff shows of `Secret` values (`--show-secrets` and `--show-secrets-decoded` of the
/// helm-diff plugin, `diff/diff.go` `doDiff`, plus a masking policy of your own).
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum SecretMode {
    /// Neither flag: `data` and `stringData` are merged into `data` and every value becomes a
    /// marker that carries only the decoded length. Exactly [`SecretMasker::helm_diff`]: the
    /// default is one masker among others.
    #[default]
    Redact,
    /// `--show-secrets`: the manifest is shown as rendered, `data` still base64 encoded.
    Show,
    /// `--show-secrets-decoded`: the decoded values under `stringData`. Wins over [`Show`](Self::Show)
    /// in the plugin, so it is a variant of its own here.
    Decode,
    /// Your own policy, applied like [`Redact`](Self::Redact) (merged into `data`, one masked
    /// string per value). Structurally malformed data (`data` or `stringData` that is not a
    /// mapping) never reaches the masker, which needs a key and bytes: it is replaced by the
    /// fixed marker `[malformed secret data]`, because the API server rejects such a Secret.
    Mask(SecretMasker),
}

/// The options of [`Diff::between`](super::Diff::between), mirroring `diff.Options` and the
/// release flags of the helm-diff plugin (`diff/diff.go`, `cmd/options.go`, `cmd/upgrade.go`,
/// `manifest/parse.go`, all at v3.15.15).
///
/// Not modelled, because they print, run a tool, or need a cluster: `--output`, `--diff-tool`,
/// `--suppress-secrets` (equals suppressing the `Secret` kind), `--find-renames` (a rename is a
/// removed plus an added object here), `--suppress-output-line-regex`, `--three-way-merge`,
/// `--take-ownership`, `--revision`, `--detailed-exitcode` (see [`Diff::is_empty`](super::Diff::is_empty))
/// and every flag that only steers rendering or the Helm connection.
#[derive(Debug, Clone, PartialEq, Eq, Default, bon::Builder)]
#[builder(on(String, into), on(bool, default))]
#[allow(
    clippy::struct_excessive_bools,
    reason = "one field per helm-diff flag"
)]
pub struct DiffOptions {
    /// Flag `--suppress` (repeatable): kinds whose changes are reported without content. The
    /// object still shows as added, removed or changed, but its text is one line saying so and it
    /// has no field changes. Compared with the kind exactly.
    #[builder(field)]
    pub suppress: Vec<String>,
    /// Flags `--show-secrets` and `--show-secrets-decoded`; redacted by default.
    #[builder(default)]
    pub secrets: SecretMode,
    /// Flag `--context`: lines of context around each change in the text diff. `None` (the
    /// plugin's `-1`) keeps every line.
    pub context: Option<usize>,
    /// Flag `--strip-trailing-cr`: ignore a carriage return at the end of each line.
    pub strip_trailing_cr: bool,
    /// Flag `--normalize-manifests`: re-serialise every object (keys sorted, one canonical
    /// style, comments including `# Source:` dropped) so that style differences do not show.
    pub normalize_manifests: bool,
    /// Flag `--no-hooks`: leave out every object that has a `helm.sh/hook` annotation. The
    /// plugin diffs hooks by default.
    pub no_hooks: bool,
    /// Flag `--include-tests`: also diff hooks whose `helm.sh/hook` annotation is exactly `test`
    /// or `test-success`, which the plugin leaves out by default.
    pub include_tests: bool,
}

impl<S: diff_options_builder::State> DiffOptionsBuilder<S>
where
    S::Secrets: diff_options_builder::IsUnset,
{
    /// Masks Secret values with `mask`: sets [`DiffOptions::secrets`] to
    /// [`SecretMode::Mask`]. The closure receives plaintext, see [`SecretMasker`].
    pub fn mask_secrets(
        self,
        mask: impl Fn(&MaskedValue<'_>) -> String + Send + Sync + 'static,
    ) -> DiffOptionsBuilder<diff_options_builder::SetSecrets<S>> {
        self.secrets(SecretMode::Mask(SecretMasker::new(mask)))
    }
}

builder_collections! {
    DiffOptionsBuilder, diff_options_builder;
    vec suppress / suppress_kind: String, "repeated flag `--suppress`", "the kinds whose content is hidden";
}