Skip to main content

helm_rs/command/release/
read.rs

1use std::collections::BTreeMap;
2
3use jiff::Timestamp;
4use serde::Deserialize;
5use serde_json::Value;
6
7use crate::backend::Command;
8use crate::chart::Dependency;
9use crate::go_json::omit_zero;
10use crate::manifest::Manifest;
11use crate::release::{ApplyMethod, Hook, Release, Revision, Status as ReleaseState};
12
13/// Declares a read command: a release name, an optional revision and an output type.
14macro_rules! read_command {
15    ($(#[$meta:meta])* $name:ident => $output:ty) => {
16        $(#[$meta])*
17        #[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
18        #[builder(on(String, into))]
19        pub struct $name {
20            /// The `RELEASE_NAME` argument.
21            #[builder(start_fn)]
22            pub release: String,
23            /// Flag `--revision`: read this revision instead of the latest.
24            pub revision: Option<Revision>,
25        }
26
27        impl $name {
28            /// Reads the latest revision of `release`.
29            #[must_use]
30            pub fn new(release: impl Into<String>) -> Self {
31                Self::builder(release).build()
32            }
33        }
34
35        impl Command for $name {
36            type Output = $output;
37        }
38    };
39}
40
41read_command! {
42    /// `helm status -o json` (`pkg/cmd/status.go`): the release without its chart (Helm strips it).
43    /// The resources of `info.resources` are only filled in for this machine output.
44    Status => Release
45}
46
47read_command! {
48    /// `helm get manifest` (`pkg/cmd/get_manifest.go`): the rendered, non-hook manifests of a
49    /// release, parsed.
50    GetManifest => Manifest
51}
52
53read_command! {
54    /// `helm get hooks` (`pkg/cmd/get_hooks.go`): the hooks of a release, parsed from the hook
55    /// manifests Helm prints. Helm prints no execution state, so `last_run` is empty; read it from
56    /// [`Status`] instead.
57    GetHooks => Vec<Hook>
58}
59
60read_command! {
61    /// `helm get notes` (`pkg/cmd/get_notes.go`): the rendered `NOTES.txt`, empty when the chart has
62    /// none.
63    GetNotes => String
64}
65
66read_command! {
67    /// `helm get metadata -o json` (`pkg/cmd/get_metadata.go`).
68    GetMetadata => ReleaseMetadata
69}
70
71/// `helm get all` (`pkg/cmd/get_all.go`): the text `helm status --debug` prints, with the chart
72/// metadata, values, hooks, manifest and notes. Helm has no JSON for it and its `--template` only
73/// offers plain Go template access to `.Release` (no functions, so no JSON encoder), which makes
74/// a typed result impossible; the output is the text. For structured data use [`Status`] plus
75/// [`GetManifest`], [`GetHooks`], [`GetValues`] and [`GetNotes`].
76#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
77#[builder(on(String, into))]
78pub struct GetAll {
79    /// The `RELEASE_NAME` argument.
80    #[builder(start_fn)]
81    pub release: String,
82    /// Flag `--revision`: read this revision instead of the latest.
83    pub revision: Option<Revision>,
84    /// Flag `--template`: a Go template evaluated with the release as `.Release`, replacing the
85    /// default text.
86    pub template: Option<String>,
87}
88
89impl GetAll {
90    /// Reads the latest revision of `release` as Helm's default text.
91    #[must_use]
92    pub fn new(release: impl Into<String>) -> Self {
93        Self::builder(release).build()
94    }
95}
96
97impl Command for GetAll {
98    type Output = String;
99}
100
101/// `helm get values -o json` (`pkg/cmd/get_values.go`): the user-supplied values, or with `all`
102/// the computed ones. Helm prints `null` when no values were supplied.
103#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
104#[builder(on(String, into), on(bool, default))]
105pub struct GetValues {
106    /// The `RELEASE_NAME` argument.
107    #[builder(start_fn)]
108    pub release: String,
109    /// Flag `--all`: all computed values, not only the user-supplied ones.
110    pub all: bool,
111    /// Flag `--revision`: read this revision instead of the latest.
112    pub revision: Option<Revision>,
113}
114
115impl GetValues {
116    /// Reads the user-supplied values of the latest revision of `release`.
117    #[must_use]
118    pub fn new(release: impl Into<String>) -> Self {
119        Self::builder(release).build()
120    }
121}
122
123impl Command for GetValues {
124    type Output = Value;
125}
126
127/// The result of `helm get metadata -o json` (`action.Metadata`, `pkg/action/get_metadata.go`).
128#[derive(Debug, Clone, PartialEq, Deserialize)]
129pub struct ReleaseMetadata {
130    /// The release name.
131    pub name: String,
132    /// The chart name.
133    pub chart: String,
134    /// The chart version.
135    pub version: String,
136    /// The application version of the chart.
137    #[serde(rename = "appVersion", default)]
138    pub app_version: String,
139    /// The annotations of `Chart.yaml`.
140    #[serde(default)]
141    pub annotations: BTreeMap<String, String>,
142    /// The labels of the release record.
143    #[serde(default)]
144    pub labels: BTreeMap<String, String>,
145    /// The chart's dependencies.
146    #[serde(default)]
147    pub dependencies: Vec<Dependency>,
148    /// The release namespace.
149    pub namespace: String,
150    /// The revision.
151    pub revision: Revision,
152    /// The release status.
153    pub status: ReleaseState,
154    /// When the release was last deployed (RFC 3339, second precision).
155    #[serde(rename = "deployedAt", default, with = "omit_zero")]
156    pub deployed_at: Option<Timestamp>,
157    /// How the release was applied; `None` when the record has no field (client-side apply).
158    #[serde(rename = "applyMethod", default)]
159    pub apply_method: Option<ApplyMethod>,
160}