|
1 | 1 | pub mod provider; |
2 | 2 | mod template; |
3 | 3 |
|
4 | | -use std::{borrow::Cow, collections::HashMap, fmt::Debug}; |
| 4 | +use std::{borrow::Cow, collections::HashMap, fmt::Debug, vec}; |
5 | 5 |
|
6 | 6 | use spin_locked_app::Variable; |
7 | 7 |
|
@@ -92,28 +92,45 @@ impl ProviderResolver { |
92 | 92 | Ok(PreparedResolver { variables }) |
93 | 93 | } |
94 | 94 |
|
95 | | - /// Prepares the resolver by attempting to resolve all variables, printing warnings for any |
96 | | - /// that cannot be resolved. |
97 | | - pub async fn pre_runtime_prepare(&self) -> Result<()> { |
98 | | - for name in self.internal.variables.keys() { |
99 | | - self.check_variable_existence(name).await?; |
| 95 | + /// Validates `Provider` that are `ProviderVariableKind::Static` provides its variable during startup. |
| 96 | + pub async fn validate_variable_existence(&self) -> Result<()> { |
| 97 | + // If a dynamic provider is available, skip validation. |
| 98 | + if self |
| 99 | + .providers |
| 100 | + .iter() |
| 101 | + .any(|p| p.kind() == ProviderVariableKind::Dynamic) |
| 102 | + { |
| 103 | + return Ok(()); |
100 | 104 | } |
101 | | - Ok(()) |
102 | | - } |
103 | 105 |
|
104 | | - async fn check_variable_existence(&self, key: &str) -> Result<()> { |
105 | | - for provider in &self.providers { |
106 | | - if provider.kind() == &ProviderVariableKind::Dynamic { |
107 | | - continue; |
| 106 | + let mut unvalidated_keys = vec![]; |
| 107 | + for key in self.internal.variables.keys() { |
| 108 | + let mut resolved = false; |
| 109 | + |
| 110 | + for provider in &self.providers { |
| 111 | + if provider |
| 112 | + .get(&Key(key)) |
| 113 | + .await |
| 114 | + .map_err(Error::Provider)? |
| 115 | + .is_some() |
| 116 | + { |
| 117 | + resolved = true; |
| 118 | + break; |
| 119 | + } |
108 | 120 | } |
109 | 121 |
|
110 | | - match provider.get(&Key(key)).await { |
111 | | - Ok(Some(_)) => return Ok(()), |
112 | | - Err(_) | Ok(None) => return self.internal.resolve_variable(key).map(|_| ()), |
| 122 | + if !resolved { |
| 123 | + unvalidated_keys.push(key); |
113 | 124 | } |
114 | 125 | } |
115 | 126 |
|
116 | | - Ok(()) |
| 127 | + if unvalidated_keys.is_empty() { |
| 128 | + Ok(()) |
| 129 | + } else { |
| 130 | + Err(Error::Provider(anyhow::anyhow!( |
| 131 | + "no provider resolved required variables: {unvalidated_keys:?}", |
| 132 | + ))) |
| 133 | + } |
117 | 134 | } |
118 | 135 |
|
119 | 136 | async fn resolve_variable(&self, key: &str) -> Result<String> { |
@@ -351,8 +368,8 @@ mod tests { |
351 | 368 | } |
352 | 369 | } |
353 | 370 |
|
354 | | - fn kind(&self) -> &ProviderVariableKind { |
355 | | - &ProviderVariableKind::Static |
| 371 | + fn kind(&self) -> ProviderVariableKind { |
| 372 | + ProviderVariableKind::Static |
356 | 373 | } |
357 | 374 | } |
358 | 375 |
|
|
0 commit comments