helm-rs 0.1.1

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
use std::time::Duration;

use super::{DryRun, Force, ServerSide, WaitStrategy};
use crate::backend::Command;
use crate::command::builder_collections;
use crate::release::Revision;

/// `helm rollback` (`pkg/cmd/rollback.go`): rolls a release back to an earlier revision. Helm
/// prints only a success line, so there is no output; read the result with
/// [`Status`](crate::command::Status).
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[builder(on(String, into), on(bool, default))]
pub struct Rollback {
    /// The `RELEASE` argument.
    #[builder(start_fn)]
    pub release: String,
    /// The `REVISION` argument; without it (or with revision 0) Helm rolls back to the previous
    /// revision.
    pub revision: Option<Revision>,
    /// Flag `--cleanup-on-fail`: delete the resources this rollback created if it fails.
    pub cleanup_on_fail: bool,
    /// Flag `--description`: a custom description of at most 256 characters (Helm checks).
    pub description: Option<String>,
    /// Flag `--dry-run`.
    pub dry_run: Option<DryRun>,
    /// Flags `--force-replace` and `--force-conflicts`.
    pub force: Option<Force>,
    /// Flag `--history-max`: how many revisions to keep; 0 keeps all (Helm's default is 10).
    pub history_max: Option<u32>,
    /// Flag `--no-hooks`: skip hooks.
    pub no_hooks: bool,
    /// Flag `--server-side` (Helm's default is `auto`).
    pub server_side: Option<ServerSide>,
    /// Flag `--timeout` for any single Kubernetes operation (Helm's default is five minutes).
    pub timeout: Option<Duration>,
    /// Flag `--wait` (Helm's default is `hookOnly`).
    pub wait: Option<WaitStrategy>,
    /// Flag `--wait-for-jobs`: with a wait, also wait for Jobs to complete.
    pub wait_for_jobs: bool,
}

impl Rollback {
    /// A rollback of `release` to the previous revision with every other setting left to Helm.
    #[must_use]
    pub fn new(release: impl Into<String>) -> Self {
        Self::builder(release).build()
    }
}

impl Command for Rollback {
    type Output = ();
}

/// How Helm deletes the objects that depend on a release's resources (`--cascade`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Cascade {
    /// `background`: delete the owner, then the dependents (Helm's default).
    Background,
    /// `orphan`: leave the dependents.
    Orphan,
    /// `foreground`: delete the dependents first; combine with a wait to block until they are
    /// gone.
    Foreground,
}

impl Cascade {
    /// The flag value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Background => "background",
            Self::Orphan => "orphan",
            Self::Foreground => "foreground",
        }
    }
}

/// `helm uninstall` (`pkg/cmd/uninstall.go`): removes one or more releases. Helm prints one
/// `release "name" uninstalled` line per release (also for a missing one under
/// `ignore_not_found`, and for a dry run), which carries no more than success, so there is no
/// output. A release that does not exist is [`Error::ReleaseNotFound`](crate::Error) unless
/// `ignore_not_found` is set. Helm stops at the first failure; earlier releases stay uninstalled.
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[builder(start_fn(name = start, vis = ""), on(String, into), on(bool, default))]
#[allow(clippy::struct_excessive_bools, reason = "one field per Helm flag")]
pub struct Uninstall {
    /// The `RELEASE_NAME` arguments (Helm needs at least one).
    #[builder(field)]
    pub releases: Vec<String>,
    /// Flag `--cascade` (Helm's default is `background`).
    pub cascade: Option<Cascade>,
    /// Flag `--description`: a custom description.
    pub description: Option<String>,
    /// Flag `--dry-run`: only simulate.
    pub dry_run: bool,
    /// Flag `--ignore-not-found`: a missing release is a success.
    pub ignore_not_found: bool,
    /// Flag `--keep-history`: mark the release uninstalled but keep its history.
    pub keep_history: bool,
    /// Flag `--no-hooks`: skip hooks.
    pub no_hooks: bool,
    /// Flag `--timeout` for any single Kubernetes operation (Helm's default is five minutes).
    pub timeout: Option<Duration>,
    /// Flag `--wait` (Helm's default is `hookOnly`).
    pub wait: Option<WaitStrategy>,
}

builder_collections! {
    UninstallBuilder, uninstall_builder;
    vec releases / release: String, "`RELEASE_NAME` arguments", "the releases to uninstall";
}

impl Uninstall {
    /// Starts building an uninstall of `release`, the first of the required `RELEASE_NAME...`.
    pub fn builder(release: impl Into<String>) -> UninstallBuilder {
        Self::start().release(release)
    }

    /// An uninstall of `release` with every other setting left to Helm.
    #[must_use]
    pub fn new(release: impl Into<String>) -> Self {
        Self::builder(release).build()
    }
}

impl Command for Uninstall {
    type Output = ();
}

/// `helm test` (`pkg/cmd/release_testing.go`): runs the test hooks of a release. Helm has no
/// machine-readable output for it (there is no `-o`), so the output is the status text it prints
/// (including a `TEST SUITE` block per test); the structured result is the hooks' `last_run` in
/// the release read afterwards with [`Status`](crate::command::Status). A failing test makes
/// Helm exit with an error, which is reported as [`Error::Backend`](crate::Error).
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[builder(on(String, into), on(bool, default))]
pub struct Test {
    /// The `RELEASE` argument.
    #[builder(start_fn)]
    pub release: String,
    /// Flag `--filter`, repeated: `name=value` to include or `!name=value` to exclude a test.
    #[builder(field)]
    pub filter: Vec<String>,
    /// Flag `--logs`: print the logs of the test pods.
    pub logs: bool,
    /// Flag `--timeout` for any single Kubernetes operation (Helm's default is five minutes).
    pub timeout: Option<Duration>,
}

builder_collections! {
    TestBuilder, test_builder;
    vec filter / add_filter: String, "repeated flag `--filter`",
        "`name=value` to include or `!name=value` to exclude a test";
}

impl Test {
    /// A test run of `release` with every other setting left to Helm.
    #[must_use]
    pub fn new(release: impl Into<String>) -> Self {
        Self::builder(release).build()
    }
}

impl Command for Test {
    type Output = String;
}