#[cfg(test)]
#[macro_use]
mod chart_rows;
mod chart_path;
mod classify;
mod dependency;
mod env;
mod fetch;
mod invocation;
mod plugin;
mod registry;
mod release;
mod render;
mod repo;
mod search;
mod show;
mod verification;
mod version;
#[cfg(test)]
mod testing;
#[cfg(test)]
mod tests;
use std::ffi::OsStr;
use std::io;
use std::path::PathBuf;
use std::process::Stdio;
use tokio::io::AsyncWriteExt;
use tokio::process::{Child, Command as Process};
use tracing::debug;
use crate::backend::{Backend, Command, Execute};
use crate::command::Version;
use crate::error::{Error, Result};
use crate::options::GlobalOptions;
pub(crate) use chart_path::{chart_path, oci_host, oci_hosts, registry_auth};
pub(crate) use invocation::{Invocation, flags, parse_json};
pub(crate) trait CliCommand: Command {
const MUTATES: bool = false;
fn build(&self, inv: &mut Invocation) -> Result<()>;
fn parse(&self, stdout: String) -> Result<Self::Output>;
fn parse_failure(&self, _stdout: &str, _stderr: &str) -> Option<Result<Self::Output>> {
None
}
}
#[derive(Debug, Clone)]
pub struct Cli {
pub binary: PathBuf,
pub version: semver::Version,
}
impl Cli {
pub async fn detect() -> Result<Self> {
Self::new("helm").await
}
pub async fn new(binary: impl Into<PathBuf>) -> Result<Self> {
let binary = binary.into();
let found = run(&binary, &GlobalOptions::default(), &Version)
.await?
.version;
if found.major < 4 {
return Err(Error::UnsupportedVersion { found });
}
Ok(Self {
binary,
version: found,
})
}
}
impl Backend for Cli {}
impl<C: CliCommand> Execute<C> for Cli {
async fn execute(&self, options: &GlobalOptions, command: &C) -> Result<C::Output> {
run(&self.binary, options, command).await
}
}
fn is_helm_var(key: &OsStr) -> bool {
key.to_string_lossy().starts_with("HELM_")
}
async fn run<C: CliCommand>(
binary: &PathBuf,
options: &GlobalOptions,
command: &C,
) -> Result<C::Output> {
let mut inv = Invocation::default();
let mut process = Process::new(binary);
inv.globals(options)?;
command.build(&mut inv)?;
for (key, _) in std::env::vars_os().filter(|(key, _)| is_helm_var(key)) {
process.env_remove(key);
}
process
.args(&inv.args)
.envs(inv.env.iter().map(|(k, v)| (k, v)))
.stdin(if inv.stdin.is_some() {
Stdio::piped()
} else {
Stdio::null()
})
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(!C::MUTATES);
debug!(binary = %binary.display(), args = ?inv.args, "running helm");
let child = process.spawn().map_err(|e| match e.kind() {
io::ErrorKind::NotFound => Error::BinaryNotFound(binary.clone()),
_ => Error::Io(e),
})?;
let finished = if C::MUTATES {
tokio::spawn(finish(child, inv))
.await
.map_err(|e| Error::Io(io::Error::other(e)))??
} else {
finish(child, inv).await?
};
if !finished.success {
if let Some(parsed) = command.parse_failure(&finished.stdout, &finished.stderr) {
return parsed;
}
return Err(classify::classify(finished.exit_code, finished.stderr));
}
command.parse(finished.stdout)
}
struct Finished {
success: bool,
exit_code: Option<i32>,
stdout: String,
stderr: String,
}
async fn finish(mut child: Child, inv: Invocation) -> Result<Finished> {
let mut stdin = child.stdin.take();
let input = inv.stdin.as_deref().unwrap_or_default();
let feed = async {
if let Some(stdin) = stdin.as_mut() {
let _ = stdin.write_all(input).await;
}
drop(stdin.take());
};
let ((), output) = tokio::join!(feed, child.wait_with_output());
let output = output?;
debug!(status = %output.status, "helm finished");
if !output.status.success() {
return Ok(Finished {
success: false,
exit_code: output.status.code(),
stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
});
}
let stdout = String::from_utf8(output.stdout)
.map_err(|e| Error::Io(io::Error::new(io::ErrorKind::InvalidData, e)))?;
Ok(Finished {
success: true,
exit_code: output.status.code(),
stdout,
stderr: String::new(),
})
}