helm-rs 0.1.1

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

use crate::backend::Command;
use crate::command::builder_collections;
use crate::values::Values;

/// `helm lint` (`pkg/cmd/lint.go`, `pkg/action/lint.go`): checks charts and returns the findings.
///
/// A chart with errors (or, with `strict`, warnings) makes Helm exit non-zero; that is a result,
/// not an error: read [`LintReport::passed`]. An error is returned only when Helm could not lint
/// at all (for example an unreadable values file).
#[derive(Debug, Clone, PartialEq, bon::Builder)]
#[builder(start_fn(name = start, vis = ""), on(String, into), on(bool, default))]
#[allow(clippy::struct_excessive_bools, reason = "one field per Helm flag")]
pub struct Lint {
    /// The `PATH` arguments: chart directories or `.tgz` archives; at least one.
    #[builder(field)]
    pub paths: Vec<PathBuf>,
    /// Flags `-f`, `--set`, `--set-string`, `--set-file`, `--set-json` and `--set-literal`.
    #[builder(default)]
    pub values: Values,
    /// Flag `--kube-version`: the Kubernetes version for capabilities and deprecation checks.
    pub kube_version: Option<String>,
    /// Flag `--quiet`: print only warnings and errors (charts without them are left out of the
    /// report).
    pub quiet: bool,
    /// Flag `--skip-schema-validation`: do not validate the values against the chart's schema.
    pub skip_schema_validation: bool,
    /// Flag `--strict`: a warning fails the chart too.
    pub strict: bool,
    /// Flag `--with-subcharts`: lint the charts below `charts/` as well.
    pub with_subcharts: bool,
}

builder_collections! {
    LintBuilder, lint_builder;
    vec paths / path: PathBuf, "`PATH` arguments", "the chart directories or `.tgz` archives";
}

impl Lint {
    /// Starts building a lint of the chart at `path`, the first of the required `PATH...`.
    pub fn builder(path: impl Into<PathBuf>) -> LintBuilder {
        Self::start().path(path)
    }

    /// Lints the chart at `path` with Helm's defaults.
    #[must_use]
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self::builder(path).build()
    }
}

/// How serious a lint finding is (`pkg/chart/v2/lint/support/message.go`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Severity {
    /// `UNKNOWN`: never fails a chart.
    Unknown,
    /// `INFO`: for example a missing `values.yaml`.
    Info,
    /// `WARNING`: against convention but likely to work; fails the chart with `--strict`.
    Warning,
    /// `ERROR`: the chart is not likely to work; fails the chart.
    Error,
}

/// One finding: Helm prints it as `[SEVERITY] path: message`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LintMessage {
    /// The severity.
    pub severity: Severity,
    /// The file or directory the finding is about, relative to the chart (`Chart.yaml`,
    /// `templates/`); empty when Helm names none.
    pub path: String,
    /// What is wrong; may span several lines.
    pub message: String,
}

/// The findings for one linted chart.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LintedChart {
    /// The chart path, as Helm printed it (`==> Linting <path>`).
    pub path: String,
    /// The findings, in the order Helm printed them.
    pub messages: Vec<LintMessage>,
    /// Failures that were not lint rules (`Error unable to check Chart.yaml ...`); Helm prints
    /// them only when there are no findings.
    pub errors: Vec<String>,
    /// Whether Helm counts the chart as failed: an error finding or failure, or a warning with
    /// `strict`.
    pub failed: bool,
}

/// The totals Helm reports (`N chart(s) linted, M chart(s) failed`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LintSummary {
    /// How many charts were linted, subcharts included.
    pub linted: usize,
    /// How many of them failed.
    pub failed: usize,
}

/// What `helm lint` printed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LintReport {
    /// The charts Helm printed a section for; with `quiet`, only those with warnings or errors.
    pub charts: Vec<LintedChart>,
    /// The totals, absent when Helm prints none (`quiet` and nothing to report).
    pub summary: Option<LintSummary>,
}

impl LintReport {
    /// True when no chart failed.
    #[must_use]
    pub fn passed(&self) -> bool {
        self.charts.iter().all(|chart| !chart.failed)
    }
}

impl Command for Lint {
    type Output = LintReport;
}