helm-rs 0.1.1

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

use crate::cli::{CliCommand, Invocation};
use crate::command::Env;
use crate::error::{Error, Result};

impl CliCommand for Env {
    fn build(&self, inv: &mut Invocation) -> Result<()> {
        inv.arg("env");

        Ok(())
    }

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

/// Parses `KEY="value"` lines; Helm adds the quotes without escaping (`pkg/cmd/env.go`).
pub(super) fn parse_env(stdout: &str) -> Result<BTreeMap<String, String>> {
    let mut map = BTreeMap::new();

    for line in stdout.lines().filter(|line| !line.is_empty()) {
        let value = line
            .split_once('=')
            .and_then(|(key, value)| Some((key, value.strip_prefix('"')?.strip_suffix('"')?)));
        let Some((key, value)) = value else {
            return Err(Error::Parse {
                command: "helm env",
                message: format!("not a KEY=\"value\" line: {line:?}"),
            });
        };

        map.insert(key.to_owned(), value.to_owned());
    }

    Ok(map)
}