helm-rs 0.1.2

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

use super::builder_collections;
use crate::backend::Command;
use crate::options::Secret;

/// `helm plugin install`: installs a plugin from a local directory, an archive URL, a VCS URL or
/// an `oci://` reference.
///
/// Helm 4.3.0 has `--username` and `--password` for OCI sources but its OCI plugin getter ignores
/// them (`pkg/getter/ocigetter.go` builds the registry client without basic auth), so only the
/// username is modelled. Authenticate with [`RegistryLogin`](super::RegistryLogin), whose
/// credentials the installer reads from `$HELM_CONFIG_HOME/registry/config.json`.
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[builder(on(String, into), on(PathBuf, into), on(bool, default))]
pub struct PluginInstall {
    /// The plugin source (`<path|url>`).
    #[builder(start_fn)]
    pub source: String,
    /// Flag `--version`: a version constraint (the latest when unset).
    pub version: Option<String>,
    /// Flag `--verify`: verify the signature before installing. Helm's default is true; `false`
    /// is passed as `--verify=false`. Local directories are never verified.
    #[builder(default = true)]
    pub verify: bool,
    /// Flag `--keyring`: the public keyring used for verification.
    pub keyring: Option<PathBuf>,
    /// Flag `--cert-file`: the SSL client certificate.
    pub cert_file: Option<PathBuf>,
    /// Flag `--key-file`: the SSL client key.
    pub key_file: Option<PathBuf>,
    /// Flag `--ca-file`: the CA bundle that verifies the server.
    pub ca_file: Option<PathBuf>,
    /// Flag `--insecure-skip-tls-verify`: skip the TLS certificate checks.
    pub insecure_skip_tls_verify: bool,
    /// Flag `--plain-http`: use insecure HTTP connections.
    pub plain_http: bool,
    /// Flag `--username`: the registry username.
    pub username: Option<String>,
}

impl PluginInstall {
    /// Installs `source` with Helm's defaults (signature verification on).
    #[must_use]
    pub fn new(source: impl Into<String>) -> Self {
        Self::builder(source).build()
    }
}

impl Command for PluginInstall {
    type Output = PluginInstalled;
}

/// What a successful `helm plugin install` printed (`pkg/cmd/plugin_install.go`, Helm v4.3.0).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PluginInstalled {
    /// The name of the installed plugin (`Installed plugin:` line).
    pub name: String,
    /// The signer and hash Helm verified, as `plugin verify` reports them; `None` when Helm did
    /// not verify (a local directory, or `verify` is false).
    pub verification: Option<PluginVerification>,
}

/// `helm plugin list`: the installed plugins. Helm has no machine output for it, so the CLI
/// backend parses the table.
#[derive(Debug, Clone, Default, PartialEq, Eq, bon::Builder)]
#[builder(on(String, into))]
pub struct PluginList {
    /// Flag `--type`: only plugins of this type (`cli/v1`, `getter/v1`, `postrenderer/v1`).
    pub kind: Option<String>,
}

/// One installed plugin: a row of the `helm plugin list` table (`pkg/cmd/plugin_list.go`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Plugin {
    /// The plugin name.
    pub name: String,
    /// The plugin version; empty when the plugin declares none.
    pub version: String,
    /// The plugin type, for example `cli/v1`.
    pub kind: String,
    /// The plugin API version: `v1` or `legacy`.
    pub api_version: String,
    /// The provenance status Helm reports: `signed`, `unsigned`, `local dev`, `invalid provenance`,
    /// `mismatched provenance` or `unknown`.
    pub provenance: String,
    /// The source URL; `None` when Helm prints `unknown`.
    pub source: Option<String>,
}

impl Command for PluginList {
    type Output = Vec<Plugin>;
}

/// Where `helm plugin package --sign` reads the passphrase of the signing key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Passphrase {
    /// Flag `--passphrase-file`: a file that contains the passphrase.
    File(PathBuf),
    /// Flag `--passphrase-file -`: the passphrase, written to Helm's stdin.
    Stdin(Secret),
}

/// `helm plugin package`: packages a plugin directory into `NAME-VERSION.tgz`, signed by default.
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[builder(on(String, into), on(PathBuf, into), on(bool, default))]
pub struct PluginPackage {
    /// The plugin directory (`PATH`).
    #[builder(start_fn)]
    pub path: PathBuf,
    /// Flag `--destination` (`-d`): where to write the tarball (Helm's default is `.`).
    pub destination: Option<PathBuf>,
    /// Flag `--sign`: sign the plugin with a PGP key and write a `.prov` file. Helm's default is
    /// true; `false` is passed as `--sign=false`.
    #[builder(default = true)]
    pub sign: bool,
    /// Flag `--key`: the name of the signing key.
    pub key: Option<String>,
    /// Flag `--keyring`: the keyring that holds the key.
    pub keyring: Option<PathBuf>,
    /// Flag `--passphrase-file`: the passphrase of the signing key.
    pub passphrase: Option<Passphrase>,
}

impl PluginPackage {
    /// Packages `path` with Helm's defaults (signing on).
    #[must_use]
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self::builder(path).build()
    }
}

impl Command for PluginPackage {
    type Output = ();
}

/// `helm plugin uninstall`: removes installed plugins.
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[builder(start_fn(name = start, vis = ""))]
pub struct PluginUninstall {
    /// The plugin names (`<plugin>...`); at least one.
    #[builder(field)]
    pub names: Vec<String>,
}

builder_collections! {
    PluginUninstallBuilder, plugin_uninstall_builder;
    vec names / name: String, "`<plugin>` arguments", "the names of the plugins";
}

impl PluginUninstall {
    /// Starts building for the plugins in `names`, the first of the required `<plugin>...`.
    pub fn builder(names: impl IntoIterator<Item = impl Into<String>>) -> PluginUninstallBuilder {
        Self::start().names(names)
    }

    /// Uninstalls the named plugins.
    #[must_use]
    pub fn new(names: impl IntoIterator<Item = impl Into<String>>) -> Self {
        Self::builder(names).build()
    }
}

impl Command for PluginUninstall {
    type Output = ();
}

/// `helm plugin update`: updates installed plugins from their source.
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[builder(start_fn(name = start, vis = ""))]
pub struct PluginUpdate {
    /// The plugin names (`<plugin>...`); at least one.
    #[builder(field)]
    pub names: Vec<String>,
}

builder_collections! {
    PluginUpdateBuilder, plugin_update_builder;
    vec names / name: String, "`<plugin>` arguments", "the names of the plugins";
}

impl PluginUpdate {
    /// Starts building for the plugins in `names`, the first of the required `<plugin>...`.
    pub fn builder(names: impl IntoIterator<Item = impl Into<String>>) -> PluginUpdateBuilder {
        Self::start().names(names)
    }

    /// Updates the named plugins.
    #[must_use]
    pub fn new(names: impl IntoIterator<Item = impl Into<String>>) -> Self {
        Self::builder(names).build()
    }
}

impl Command for PluginUpdate {
    type Output = ();
}

/// What a successful `helm plugin verify` reports (`pkg/cmd/plugin_verify.go`, Helm v4.3.0).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PluginVerification {
    /// The identities of the signing key (`Signed by:` lines), for example `Name <mail>`.
    pub signed_by: Vec<String>,
    /// The fingerprint of the signing key, upper-case hex.
    pub fingerprint: String,
    /// The verified tarball hash, for example `sha256:...`.
    pub hash: String,
}

/// `helm plugin verify`: verifies a plugin tarball against its `.prov` file. Failure is the error;
/// success returns the signer and the verified hash.
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[builder(on(PathBuf, into))]
pub struct PluginVerify {
    /// The plugin tarball (`PATH`); `PATH.prov` must sit next to it.
    #[builder(start_fn)]
    pub path: PathBuf,
    /// Flag `--keyring`: the keyring that holds the public keys.
    pub keyring: Option<PathBuf>,
}

impl PluginVerify {
    /// Verifies the tarball at `path`.
    #[must_use]
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self::builder(path).build()
    }
}

impl Command for PluginVerify {
    type Output = PluginVerification;
}