Skip to main content

helm_rs/
helm.rs

1//! The [`Helm`] handle.
2
3use std::sync::Arc;
4
5use crate::backend::{Backend, Command, Execute};
6use crate::error::Result;
7use crate::options::GlobalOptions;
8
9/// A Helm handle: a backend plus the global options applied to every command. Cloning is cheap
10/// (the backend is shared); the `with_*` methods return a modified clone.
11///
12/// Commands are built with their derived builders (see the crate documentation) and run with
13/// [`Helm::run`]:
14///
15/// ```no_run
16/// # async fn demo() -> helm_rs::Result<()> {
17/// use helm_rs::Helm;
18/// use helm_rs::command::Uninstall;
19///
20/// let helm = Helm::detect().await?.with_namespace("prod");
21///
22/// helm.run(Uninstall::builder("web").release("web-db").keep_history(true).build()).await?;
23/// # Ok(()) }
24/// ```
25#[derive(Debug)]
26pub struct Helm<B: Backend> {
27    backend: Arc<B>,
28    options: GlobalOptions,
29}
30
31impl<B: Backend> Clone for Helm<B> {
32    fn clone(&self) -> Self {
33        Self {
34            backend: Arc::clone(&self.backend),
35            options: self.options.clone(),
36        }
37    }
38}
39
40impl<B: Backend> Helm<B> {
41    /// Wraps a backend with default global options.
42    pub fn new(backend: B) -> Self {
43        Self {
44            backend: Arc::new(backend),
45            options: GlobalOptions::default(),
46        }
47    }
48
49    /// Returns a handle that uses `options` for every command.
50    #[must_use]
51    pub fn with_options(&self, options: GlobalOptions) -> Self {
52        Self {
53            backend: Arc::clone(&self.backend),
54            options,
55        }
56    }
57
58    /// Returns a handle scoped to `namespace` (`--namespace`).
59    #[must_use]
60    pub fn with_namespace(&self, namespace: impl Into<String>) -> Self {
61        let mut options = self.options.clone();
62
63        options.namespace = Some(namespace.into());
64
65        self.with_options(options)
66    }
67
68    /// The global options applied to every command.
69    #[must_use]
70    pub fn options(&self) -> &GlobalOptions {
71        &self.options
72    }
73
74    /// The backend.
75    #[must_use]
76    pub fn backend(&self) -> &B {
77        &self.backend
78    }
79
80    /// Runs a command on the backend with this handle's global options.
81    pub async fn run<C: Command>(&self, command: C) -> Result<C::Output>
82    where
83        B: Execute<C>,
84    {
85        self.backend.execute(&self.options, &command).await
86    }
87}
88
89#[cfg(feature = "cli")]
90impl Helm<crate::cli::Cli> {
91    /// Finds `helm` on `PATH` and checks that it is Helm 4.
92    pub async fn detect() -> Result<Self> {
93        Ok(Self::new(crate::cli::Cli::detect().await?))
94    }
95
96    /// Uses the given `helm` binary, checking that it is Helm 4.
97    pub async fn cli(binary: impl Into<std::path::PathBuf>) -> Result<Self> {
98        Ok(Self::new(crate::cli::Cli::new(binary).await?))
99    }
100}