1use std::sync::Arc;
4
5use crate::backend::{Backend, Command, Execute};
6use crate::error::Result;
7use crate::options::GlobalOptions;
8
9#[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 pub fn new(backend: B) -> Self {
43 Self {
44 backend: Arc::new(backend),
45 options: GlobalOptions::default(),
46 }
47 }
48
49 #[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 #[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 #[must_use]
70 pub fn options(&self) -> &GlobalOptions {
71 &self.options
72 }
73
74 #[must_use]
76 pub fn backend(&self) -> &B {
77 &self.backend
78 }
79
80 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 pub async fn detect() -> Result<Self> {
93 Ok(Self::new(crate::cli::Cli::detect().await?))
94 }
95
96 pub async fn cli(binary: impl Into<std::path::PathBuf>) -> Result<Self> {
98 Ok(Self::new(crate::cli::Cli::new(binary).await?))
99 }
100}