use std::sync::Arc;
use crate::backend::{Backend, Command, Execute};
use crate::error::Result;
use crate::options::GlobalOptions;
#[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> {
pub fn new(backend: B) -> Self {
Self {
backend: Arc::new(backend),
options: GlobalOptions::default(),
}
}
#[must_use]
pub fn with_options(&self, options: GlobalOptions) -> Self {
Self {
backend: Arc::clone(&self.backend),
options,
}
}
#[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)
}
#[must_use]
pub fn options(&self) -> &GlobalOptions {
&self.options
}
#[must_use]
pub fn backend(&self) -> &B {
&self.backend
}
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> {
pub async fn detect() -> Result<Self> {
Ok(Self::new(crate::cli::Cli::detect().await?))
}
pub async fn cli(binary: impl Into<std::path::PathBuf>) -> Result<Self> {
Ok(Self::new(crate::cli::Cli::new(binary).await?))
}
}