diff options
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs index b86aada..d583b05 100644 --- a/src/config.rs +++ b/src/config.rs @@ -311,6 +311,63 @@ impl Config { .to_string() } + /// Refuse a configuration that cannot work, before anything builds with it. + /// + /// The enum already makes the impossible unrepresentable; this catches what + /// a type cannot see — a field that is present but empty, and the one + /// assertion the type deliberately leaves defaulted so the refusal can + /// explain itself instead of arriving as a parse error. Called at startup + /// (so the operator hears about it now, not at the first build) and again + /// where a backend is constructed (so a hand-built `Config` cannot skip it). + pub fn validate(&self) -> crate::error::Result<()> { + let require = |field: &str, value: &str| -> crate::error::Result<()> { + if value.trim().is_empty() { + return Err(crate::error::Error::invalid(format!( + "sandbox kind \"{}\" needs a non-empty \"{field}\"", + self.sandbox.kind() + ))); + } + Ok(()) + }; + match &self.sandbox { + Sandbox::Loopback => {} + Sandbox::Remote { listen, token, .. } => { + require("listen", listen)?; + require("token", token)?; + } + Sandbox::AgentSandbox { api, namespace, bearer, image, listen, token, .. } => { + for (field, value) in [ + ("api", api), + ("namespace", namespace), + ("bearer", bearer), + ("image", image), + ("listen", listen), + ("token", token), + ] { + require(field, value)?; + } + } + } + if self.sandbox.shared_workspace() { + return Ok(()); + } + // The one gap a remote configuration cannot be trusted to notice by + // itself: the workspace is not shipped to the sandbox, so a remote + // build only works where hub and sandbox see the same path. Refusing + // here is what stops "remote" from silently meaning "on the hub". + Err(crate::error::Error::invalid(format!( + concat!( + "sandbox kind \"{}\" was not told that hub and sandbox share a filesystem: ", + "set \"shared_workspace\": true in the sandbox object. The build workspace is ", + "not shipped to a remote sandbox — `Run` frames carry a hub-side working ", + "directory — so a remote build only works where the staging directory is ", + "visible at the same path on both sides. Asserting it here is what keeps a ", + "remote configuration from silently building on the hub it was meant to leave." + ), + self.sandbox.kind() + ))) + } + /// Read `<root>/config.json`. /// /// The crate's own error type, like everything else that can fail (m-15): |
