helm-rs 0.1.1

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
use crate::cli::{CliCommand, Invocation};
use crate::command::{HelmVersion, Version};
use crate::error::{Error, Result};

const TEMPLATE: &str = "{{.Version}}\n{{.GitCommit}}\n{{.GitTreeState}}\n{{.GoVersion}}";

impl CliCommand for Version {
    fn build(&self, inv: &mut Invocation) -> Result<()> {
        inv.arg("version");
        inv.arg("--template");
        inv.arg(TEMPLATE);

        Ok(())
    }

    fn parse(&self, stdout: String) -> Result<HelmVersion> {
        parse_version(&stdout)
    }
}

/// Parses the four template lines of [`TEMPLATE`].
pub(super) fn parse_version(stdout: &str) -> Result<HelmVersion> {
    let parse_error = |message: String| Error::Parse {
        command: "helm version",
        message,
    };
    let lines: Vec<&str> = stdout.trim_end_matches('\n').split('\n').collect();
    let [version, git_commit, git_tree_state, go_version] = lines[..] else {
        return Err(parse_error(format!(
            "expected 4 lines, got {}",
            lines.len()
        )));
    };
    let version = semver::Version::parse(version.trim().trim_start_matches('v'))
        .map_err(|e| parse_error(format!("{version:?}: {e}")))?;

    Ok(HelmVersion {
        version,
        git_commit: git_commit.to_owned(),
        git_tree_state: git_tree_state.to_owned(),
        go_version: go_version.to_owned(),
    })
}