Skip to main content

helm_rs/command/release/
list.rs

1use jiff::Timestamp;
2use serde::{Deserialize, Deserializer, de::Error};
3
4use crate::backend::Command;
5use crate::release::{Revision, Status};
6
7/// `helm list -o json` (`pkg/cmd/list.go`). Helm's `--short`, `--no-headers` and `--time-format`
8/// only change the table or the time text, which the typed output replaces, so they are not
9/// fields (`--short` is the names of the result).
10#[derive(Debug, Clone, Default, PartialEq, Eq, bon::Builder)]
11#[builder(on(String, into), on(bool, default))]
12#[allow(clippy::struct_excessive_bools, reason = "one field per Helm flag")]
13pub struct List {
14    /// Flag `--all-namespaces`: list across all namespaces.
15    pub all_namespaces: bool,
16    /// Flag `--date`: sort by release date instead of name.
17    pub date: bool,
18    /// Flag `--deployed`: show deployed releases.
19    pub deployed: bool,
20    /// Flag `--failed`: show failed releases.
21    pub failed: bool,
22    /// Flag `--filter`: a regular expression the release name must match.
23    pub filter: Option<String>,
24    /// Flag `--max`: how many releases to fetch (Helm's default is 256; 0 means the server's
25    /// default).
26    pub max: Option<u32>,
27    /// Flag `--offset`: skip this many releases.
28    pub offset: Option<u32>,
29    /// Flag `--pending`: show pending releases.
30    pub pending: bool,
31    /// Flag `--reverse`: reverse the sort order.
32    pub reverse: bool,
33    /// Flag `--selector`: a label query (`k=v`, `k==v`, `k!=v`); only for the secret and configmap
34    /// drivers.
35    pub selector: Option<String>,
36    /// Flag `--superseded`: show superseded releases.
37    pub superseded: bool,
38    /// Flag `--uninstalled`: show uninstalled releases (kept by `--keep-history`).
39    pub uninstalled: bool,
40    /// Flag `--uninstalling`: show releases being uninstalled.
41    pub uninstalling: bool,
42}
43
44impl List {
45    /// Lists the releases of the current namespace with every setting left to Helm.
46    #[must_use]
47    pub fn new() -> Self {
48        Self::builder().build()
49    }
50}
51
52impl Command for List {
53    type Output = Vec<ListItem>;
54}
55
56/// One release of `helm list -o json` (`releaseElement`, `pkg/cmd/list.go`). Helm writes the
57/// revision and the time as strings; both are parsed here.
58#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
59pub struct ListItem {
60    /// The release name.
61    pub name: String,
62    /// The release namespace.
63    pub namespace: String,
64    /// The revision.
65    #[serde(deserialize_with = "revision")]
66    pub revision: Revision,
67    /// When the release was last deployed (Go's `time.Time.String()`, written as `-` when never).
68    #[serde(deserialize_with = "go_time")]
69    pub updated: Option<Timestamp>,
70    /// The release status.
71    pub status: Status,
72    /// `name-version` of the chart, or `MISSING` when the record has no chart.
73    pub chart: String,
74    /// The chart's application version, or `MISSING`.
75    pub app_version: String,
76}
77
78/// Reads a revision written as a decimal string.
79fn revision<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Revision, D::Error> {
80    let text = String::deserialize(deserializer)?;
81
82    text.parse().map(Revision).map_err(D::Error::custom)
83}
84
85/// Reads `2006-01-02 15:04:05.999999999 -0700 MST` (Go's `time.Time.String()`); the zone name is
86/// dropped because the numeric offset already fixes the instant. `-` is `None`.
87fn go_time<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<Timestamp>, D::Error> {
88    let text = String::deserialize(deserializer)?;
89    let mut parts = text.split_whitespace();
90
91    if text == "-" {
92        return Ok(None);
93    }
94
95    let (Some(date), Some(time), Some(offset)) = (parts.next(), parts.next(), parts.next()) else {
96        return Err(D::Error::custom(format!("not a Go time: {text:?}")));
97    };
98    let (sign_hours, minutes) = offset.split_at(offset.len().saturating_sub(2));
99
100    format!("{date}T{time}{sign_hours}:{minutes}")
101        .parse()
102        .map(Some)
103        .map_err(D::Error::custom)
104}