use std::path::{Path, PathBuf};
use tempfile::TempDir;
use super::{Chart, Error as ChartError};
use crate::backend::Execute;
use crate::command::{ChartPathOptions, ChartRef, Pull};
use crate::error::Result;
use crate::helm::Helm;
impl Chart {
pub async fn fetch<B: Execute<Pull>>(
helm: &Helm<B>,
chart: &ChartRef,
options: &ChartPathOptions,
) -> Result<Self> {
if let ChartRef::Path(path) = chart {
return Ok(Self::load(path)?);
}
let dir = TempDir::new()?;
let pull = Pull::builder(chart.clone())
.chart_path(options.clone())
.destination(dir.path())
.build();
helm.run(pull).await?;
Ok(Self::load(find_archive(dir.path())?)?)
}
}
fn find_archive(dir: &Path) -> Result<PathBuf> {
let mut archives = Vec::new();
for entry in std::fs::read_dir(dir)? {
let path = entry?.path();
if path.extension().is_none_or(|extension| extension != "prov") {
archives.push(path);
}
}
match archives.len() {
1 => Ok(archives.remove(0)),
count => Err(ChartError::Archive(format!(
"helm pull wrote {count} chart files into {}, expected one",
dir.display()
))
.into()),
}
}
#[cfg(all(test, feature = "mock"))]
mod tests;