helm-rs 0.1.0

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
use std::path::PathBuf;

use crate::cli::{CliCommand, Invocation, flags, oci_hosts, registry_auth};
use crate::command::{Package, Passphrase};
use crate::error::{Error, Result};

const SAVED: &str = "Successfully packaged chart and saved it to: ";

impl CliCommand for Package {
    fn build(&self, inv: &mut Invocation) -> Result<()> {
        if self.charts.is_empty() {
            return Err(Error::InvalidCommand(
                "helm package needs at least one chart path".to_owned(),
            ));
        }

        if self.sign && self.key.is_none() {
            return Err(Error::InvalidCommand(
                "--sign needs a signing key name (--key)".to_owned(),
            ));
        }

        let mut username = self.username.as_deref();

        inv.arg("package");

        for chart in &self.charts {
            inv.arg(chart);
        }

        if let Some(password) = &self.password {
            let user = username.take().ok_or_else(|| {
                Error::InvalidCommand("a registry password needs a username".to_owned())
            })?;
            let hosts = oci_hosts(self.charts.iter().map(PathBuf::as_path))?;

            if hosts.is_empty() {
                return Err(Error::InvalidCommand(
                    "Helm uses a package password only for oci:// dependencies, and these \
                     charts have none"
                        .to_owned(),
                ));
            }

            let hosts: Vec<&str> = hosts.iter().map(String::as_str).collect();

            registry_auth(inv, &hosts, user, password)?;
        }

        flags! { inv, self;
            option("--app-version", app_version);
            option("--ca-file", ca_file);
            option("--cert-file", cert_file);
            switch("--dependency-update", dependency_update);
            option("--destination", destination);
            switch("--insecure-skip-tls-verify", insecure_skip_tls_verify);
            option("--key", key);
            option("--key-file", key_file);
            option("--keyring", keyring);
        }

        match &self.passphrase {
            Some(Passphrase::File(path)) => inv.option("--passphrase-file", Some(path)),
            Some(Passphrase::Stdin(secret)) => {
                if secret.expose().contains(['\n', '\r']) {
                    return Err(Error::InvalidCommand(
                        "Helm reads the passphrase as one line: it cannot contain a line break"
                            .to_owned(),
                    ));
                }

                inv.option("--passphrase-file", Some("-"));
                inv.stdin = Some(secret.expose().as_bytes().to_vec());
            }
            None => {}
        }

        inv.switch("--plain-http", self.plain_http);
        inv.switch("--sign", self.sign);
        inv.option("--username", username);
        inv.option("--version", self.version.as_ref());

        Ok(())
    }

    fn parse(&self, stdout: String) -> Result<Vec<PathBuf>> {
        let saved: Vec<PathBuf> = stdout
            .lines()
            .filter_map(|line| line.strip_prefix(SAVED))
            .map(PathBuf::from)
            .collect();

        if saved.len() != self.charts.len() {
            return Err(Error::Parse {
                command: "helm package",
                message: format!(
                    "{} archives reported for {} charts: {stdout:?}",
                    saved.len(),
                    self.charts.len()
                ),
            });
        }

        Ok(saved)
    }
}