Skip to main content

helm_rs/command/
dependency.rs

1use std::path::PathBuf;
2
3use crate::backend::Command;
4use crate::options::Secret;
5
6/// The flags `helm dependency build` and `helm dependency update` share
7/// (`addDependencySubcommandFlags`, `pkg/cmd/dependency.go`, Helm v4.3.0).
8///
9/// Helm uses `username` and `password` only for the registry client, that is for `oci://`
10/// dependencies; repositories of other kinds authenticate through the repository config. The
11/// `password` never reaches argv: the CLI backend writes it, with the username, to a `0600`
12/// temporary registry config entry for the registry host of each `oci://` dependency of the chart,
13/// and refuses the command when the chart has none.
14#[derive(Debug, Clone, Default, PartialEq, bon::Builder)]
15#[builder(
16    on(String, into),
17    on(PathBuf, into),
18    on(Secret, into),
19    on(bool, default)
20)]
21#[allow(clippy::struct_excessive_bools, reason = "one field per Helm flag")]
22pub struct DependencyOptions {
23    /// Flag `--verify`: verify the packages against signatures. `build` verifies when it can
24    /// (`VerifyIfPossible`: a missing provenance file only warns); `update` always verifies.
25    pub verify: bool,
26    /// Flag `--keyring`: the keyring that holds the public keys.
27    pub keyring: Option<PathBuf>,
28    /// Flag `--skip-refresh`: do not refresh the local repository cache.
29    pub skip_refresh: bool,
30    /// Flag `--username`.
31    pub username: Option<String>,
32    /// Never a flag: see the type documentation.
33    pub password: Option<Secret>,
34    /// Flag `--cert-file`: the HTTPS client certificate.
35    pub cert_file: Option<PathBuf>,
36    /// Flag `--key-file`: the HTTPS client key.
37    pub key_file: Option<PathBuf>,
38    /// Flag `--ca-file`: the CA bundle that verifies HTTPS servers.
39    pub ca_file: Option<PathBuf>,
40    /// Flag `--insecure-skip-tls-verify`.
41    pub insecure_skip_tls_verify: bool,
42    /// Flag `--plain-http`: use insecure HTTP connections.
43    pub plain_http: bool,
44}
45
46/// Declares a command that takes a chart directory and the [`DependencyOptions`].
47macro_rules! dependency_command {
48    ($(#[$meta:meta])* $name:ident) => {
49        $(#[$meta])*
50        #[derive(Debug, Clone, PartialEq, bon::Builder)]
51        #[builder(on(PathBuf, into))]
52        pub struct $name {
53            /// The chart directory (`CHART`).
54            #[builder(start_fn)]
55            pub chart: PathBuf,
56            /// The download and verification flags.
57            #[builder(default)]
58            pub options: DependencyOptions,
59        }
60
61        impl $name {
62            /// Runs on the chart in `chart` with Helm's defaults.
63            #[must_use]
64            pub fn new(chart: impl Into<PathBuf>) -> Self {
65                Self::builder(chart).build()
66            }
67        }
68
69        impl Command for $name {
70            type Output = ();
71        }
72    };
73}
74
75dependency_command! {
76    /// `helm dependency build`: rebuilds `charts/` from `Chart.lock` without renegotiating
77    /// versions; without a lock file it behaves like [`DependencyUpdate`]. Helm prints progress
78    /// lines only, which are dropped.
79    DependencyBuild
80}
81
82dependency_command! {
83    /// `helm dependency update`: resolves the dependencies of `Chart.yaml`, downloads them into
84    /// `charts/` and writes `Chart.lock`. Helm prints progress lines only, which are dropped.
85    DependencyUpdate
86}
87
88/// `helm dependency list`: the dependencies a chart declares and whether `charts/` satisfies
89/// them. Helm has no machine output, so the CLI backend parses the table; it always passes
90/// `--max-col-width` with the largest value so that no cell is cut short.
91#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
92#[builder(on(PathBuf, into))]
93pub struct DependencyList {
94    /// The chart directory or archive (`CHART`).
95    #[builder(start_fn)]
96    pub chart: PathBuf,
97}
98
99impl DependencyList {
100    /// Lists the dependencies of the chart at `chart`.
101    #[must_use]
102    pub fn new(chart: impl Into<PathBuf>) -> Self {
103        Self::builder(chart).build()
104    }
105}
106
107impl Command for DependencyList {
108    type Output = DependencyListing;
109}
110
111/// What `helm dependency list` printed (`pkg/action/dependency.go`, Helm v4.3.0).
112#[derive(Debug, Clone, Default, PartialEq, Eq)]
113pub struct DependencyListing {
114    /// The table rows, in the order of `Chart.yaml`; empty when the chart declares none.
115    pub dependencies: Vec<DependencyRow>,
116    /// The warning lines: `WARNING: no dependencies at ...` when there are none, and one line
117    /// for each chart in `charts/` that `Chart.yaml` does not declare or that is not a chart.
118    pub warnings: Vec<String>,
119}
120
121/// One row of the `helm dependency list` table.
122#[derive(Debug, Clone, PartialEq, Eq)]
123pub struct DependencyRow {
124    /// The dependency name.
125    pub name: String,
126    /// The version or constraint `Chart.yaml` asks for.
127    pub version: String,
128    /// The repository URL; empty for a chart that lives in `charts/`.
129    pub repository: String,
130    /// How `charts/` relates to the dependency.
131    pub status: DependencyStatus,
132}
133
134/// The status column of `helm dependency list` (`dependencyStatus` and `statArchiveForStatus`,
135/// `pkg/action/dependency.go`): every string Helm v4.3.0 can print.
136#[derive(Debug, Clone, Copy, PartialEq, Eq)]
137pub enum DependencyStatus {
138    /// `ok`: a packaged `charts/NAME-*.tgz` satisfies the version.
139    Ok,
140    /// `unpacked`: an unpacked chart in `charts/` satisfies the version.
141    Unpacked,
142    /// `missing`: there is no such chart in `charts/`.
143    Missing,
144    /// `wrong version`: the chart in `charts/` does not satisfy the version constraint.
145    WrongVersion,
146    /// `invalid version`: the dependency's version or the chart's own is not valid semver.
147    InvalidVersion,
148    /// `corrupt`: a `charts/NAME-*.tgz` archive cannot be loaded.
149    Corrupt,
150    /// `misnamed`: a `charts/NAME-*.tgz` archive holds a chart of another name.
151    Misnamed,
152    /// `too many matches`: several `charts/NAME-VERSION.tgz` archives match.
153    TooManyMatches,
154    /// `bad pattern`: the dependency name is not a valid file glob.
155    BadPattern,
156}
157
158impl DependencyStatus {
159    /// The status for the text Helm prints, `None` for a string Helm v4.3.0 never prints.
160    #[must_use]
161    pub fn from_helm(text: &str) -> Option<Self> {
162        Some(match text {
163            "ok" => Self::Ok,
164            "unpacked" => Self::Unpacked,
165            "missing" => Self::Missing,
166            "wrong version" => Self::WrongVersion,
167            "invalid version" => Self::InvalidVersion,
168            "corrupt" => Self::Corrupt,
169            "misnamed" => Self::Misnamed,
170            "too many matches" => Self::TooManyMatches,
171            "bad pattern" => Self::BadPattern,
172            _ => return None,
173        })
174    }
175}