helm-rs 0.1.0

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
use serde_json::Value;

use crate::cli::invocation::flags;
use crate::cli::parse_json;
use crate::cli::{CliCommand, Invocation};
use crate::command::{
    GetAll, GetHooks, GetManifest, GetMetadata, GetNotes, GetValues, History, HistoryEntry, List,
    ListItem, ReleaseMetadata, Status,
};
use crate::error::{Error, Result};
use crate::manifest::{Manifest, Rendered};
use crate::release::{Hook, Release};

/// A read command whose arguments are `<words> RELEASE [--revision N]` and whose output is JSON.
macro_rules! revision_command {
    ($command:ty, [$($word:literal),+], $json:literal, $output:ty, $name:literal) => {
        impl CliCommand for $command {
            fn build(&self, inv: &mut Invocation) -> Result<()> {
                $(inv.arg($word);)+
                inv.arg(&self.release);
                flags! { inv, self;
                    number("--revision", revision);
                }
                if $json {
                    inv.json_output();
                }

                Ok(())
            }

            fn parse(&self, stdout: String) -> Result<$output> {
                parse_json($name, &stdout)
            }
        }
    };
}

revision_command!(Status, ["status"], true, Release, "helm status");
revision_command!(
    GetMetadata,
    ["get", "metadata"],
    true,
    ReleaseMetadata,
    "helm get metadata"
);

impl CliCommand for GetValues {
    fn build(&self, inv: &mut Invocation) -> Result<()> {
        inv.arg("get");
        inv.arg("values");
        inv.arg(&self.release);
        flags! { inv, self;
            switch("--all", all);
            number("--revision", revision);
        }
        inv.json_output();

        Ok(())
    }

    fn parse(&self, stdout: String) -> Result<Value> {
        parse_json("helm get values", &stdout)
    }
}

impl CliCommand for GetAll {
    fn build(&self, inv: &mut Invocation) -> Result<()> {
        inv.arg("get");
        inv.arg("all");
        inv.arg(&self.release);
        flags! { inv, self;
            number("--revision", revision);
            option("--template", template);
        }

        Ok(())
    }

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

impl CliCommand for GetNotes {
    fn build(&self, inv: &mut Invocation) -> Result<()> {
        inv.arg("get");
        inv.arg("notes");
        inv.arg(&self.release);
        flags! { inv, self;
            number("--revision", revision);
        }

        Ok(())
    }

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

impl CliCommand for GetManifest {
    fn build(&self, inv: &mut Invocation) -> Result<()> {
        inv.arg("get");
        inv.arg("manifest");
        inv.arg(&self.release);
        flags! { inv, self;
            number("--revision", revision);
        }

        Ok(())
    }

    fn parse(&self, stdout: String) -> Result<Manifest> {
        Manifest::parse(&stdout).map_err(|e| Error::Parse {
            command: "helm get manifest",
            message: e.to_string(),
        })
    }
}

impl CliCommand for GetHooks {
    fn build(&self, inv: &mut Invocation) -> Result<()> {
        inv.arg("get");
        inv.arg("hooks");
        inv.arg(&self.release);
        flags! { inv, self;
            number("--revision", revision);
        }

        Ok(())
    }

    fn parse(&self, stdout: String) -> Result<Vec<Hook>> {
        Rendered::parse(&stdout)
            .map(|rendered| rendered.hooks)
            .map_err(|e| Error::Parse {
                command: "helm get hooks",
                message: e.to_string(),
            })
    }
}

impl CliCommand for History {
    fn build(&self, inv: &mut Invocation) -> Result<()> {
        inv.arg("history");
        inv.arg(&self.release);
        flags! { inv, self;
            number("--max", max);
            switch("--show-rollback-revision", show_rollback_revision);
        }
        inv.json_output();

        Ok(())
    }

    fn parse(&self, stdout: String) -> Result<Vec<HistoryEntry>> {
        parse_json("helm history", &stdout)
    }
}

impl CliCommand for List {
    fn build(&self, inv: &mut Invocation) -> Result<()> {
        inv.arg("list");
        flags! { inv, self;
            switch("--all-namespaces", all_namespaces);
            switch("--date", date);
            switch("--deployed", deployed);
            switch("--failed", failed);
            option("--filter", filter);
            number("--max", max);
            number("--offset", offset);
            switch("--pending", pending);
            switch("--reverse", reverse);
            option("--selector", selector);
            switch("--superseded", superseded);
            switch("--uninstalled", uninstalled);
            switch("--uninstalling", uninstalling);
        }
        inv.json_output();

        Ok(())
    }

    fn parse(&self, stdout: String) -> Result<Vec<ListItem>> {
        parse_json("helm list", &stdout)
    }
}

/// Strips the `NOTES:` heading and the final newline `helm get notes` adds; a release without
/// notes prints nothing.
pub(super) fn parse_notes(stdout: &str) -> Result<String> {
    if stdout.is_empty() {
        return Ok(String::new());
    }

    let notes = stdout
        .strip_prefix("NOTES:\n")
        .ok_or_else(|| Error::Parse {
            command: "helm get notes",
            message: "output does not start with `NOTES:`".to_owned(),
        })?;

    Ok(notes.strip_suffix('\n').unwrap_or(notes).to_owned())
}