helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! The [`Helm`] handle.

use std::sync::Arc;

use crate::backend::{Backend, Command, Execute};
use crate::error::Result;
use crate::options::GlobalOptions;

/// A Helm handle: a backend plus the global options applied to every command. Cloning is cheap
/// (the backend is shared); the `with_*` methods return a modified clone.
///
/// Commands are built with their derived builders (see the crate documentation) and run with
/// [`Helm::run`]:
///
/// ```no_run
/// # async fn demo() -> helm_rs::Result<()> {
/// use helm_rs::Helm;
/// use helm_rs::command::Uninstall;
///
/// let helm = Helm::detect().await?.with_namespace("prod");
///
/// helm.run(Uninstall::builder("web").release("web-db").keep_history(true).build()).await?;
/// # Ok(()) }
/// ```
#[derive(Debug)]
pub struct Helm<B: Backend> {
    backend: Arc<B>,
    options: GlobalOptions,
}

impl<B: Backend> Clone for Helm<B> {
    fn clone(&self) -> Self {
        Self {
            backend: Arc::clone(&self.backend),
            options: self.options.clone(),
        }
    }
}

impl<B: Backend> Helm<B> {
    /// Wraps a backend with default global options.
    pub fn new(backend: B) -> Self {
        Self {
            backend: Arc::new(backend),
            options: GlobalOptions::default(),
        }
    }

    /// Returns a handle that uses `options` for every command.
    #[must_use]
    pub fn with_options(&self, options: GlobalOptions) -> Self {
        Self {
            backend: Arc::clone(&self.backend),
            options,
        }
    }

    /// Returns a handle scoped to `namespace` (`--namespace`).
    #[must_use]
    pub fn with_namespace(&self, namespace: impl Into<String>) -> Self {
        let mut options = self.options.clone();

        options.namespace = Some(namespace.into());

        self.with_options(options)
    }

    /// The global options applied to every command.
    #[must_use]
    pub fn options(&self) -> &GlobalOptions {
        &self.options
    }

    /// The backend.
    #[must_use]
    pub fn backend(&self) -> &B {
        &self.backend
    }

    /// Runs a command on the backend with this handle's global options.
    pub async fn run<C: Command>(&self, command: C) -> Result<C::Output>
    where
        B: Execute<C>,
    {
        self.backend.execute(&self.options, &command).await
    }
}

#[cfg(feature = "cli")]
impl Helm<crate::cli::Cli> {
    /// Finds `helm` on `PATH` and checks that it is Helm 4.
    pub async fn detect() -> Result<Self> {
        Ok(Self::new(crate::cli::Cli::detect().await?))
    }

    /// Uses the given `helm` binary, checking that it is Helm 4.
    pub async fn cli(binary: impl Into<std::path::PathBuf>) -> Result<Self> {
        Ok(Self::new(crate::cli::Cli::new(binary).await?))
    }
}