From d9c1460524902982c9d7d66929f9a4384828b2a8 Mon Sep 17 00:00:00 2001 From: Ashley Lewis Date: Tue, 13 Aug 2019 18:18:40 -0500 Subject: [PATCH] refactor: make validate a method on project --- src/commands/publish/mod.rs | 66 +------------------------------------ src/settings/project/mod.rs | 59 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 65 deletions(-) diff --git a/src/commands/publish/mod.rs b/src/commands/publish/mod.rs index 6dab662b9..0a9032edf 100644 --- a/src/commands/publish/mod.rs +++ b/src/commands/publish/mod.rs @@ -20,7 +20,7 @@ use crate::terminal::message; pub fn publish(user: &GlobalUser, project: &Project, release: bool) -> Result<(), failure::Error> { info!("release = {}", release); - validate_project(project, release)?; + project.validate(release)?; commands::build(&project)?; publish_script(&user, &project, release)?; if release { @@ -115,67 +115,3 @@ fn make_public_on_subdomain(project: &Project, user: &GlobalUser) -> Result<(), } Ok(()) } - -fn validate_project(project: &Project, release: bool) -> Result<(), failure::Error> { - let mut missing_fields = Vec::new(); - - if project.account_id.is_empty() { - missing_fields.push("account_id") - }; - if project.name.is_empty() { - missing_fields.push("name") - }; - - match &project.kv_namespaces { - Some(kv_namespaces) => { - for kv in kv_namespaces { - if kv.binding.is_empty() { - missing_fields.push("kv-namespace binding") - } - - if kv.id.is_empty() { - missing_fields.push("kv-namespace id") - } - } - } - None => {} - } - - let destination = if release { - //check required fields for release - if project - .zone_id - .as_ref() - .unwrap_or(&"".to_string()) - .is_empty() - { - missing_fields.push("zone_id") - }; - if project.route.as_ref().unwrap_or(&"".to_string()).is_empty() { - missing_fields.push("route") - }; - //zoned deploy destination - "a route" - } else { - //zoneless deploy destination - "your subdomain" - }; - - let (field_pluralization, is_are) = match missing_fields.len() { - n if n >= 2 => ("fields", "are"), - 1 => ("field", "is"), - _ => ("", ""), - }; - - if !missing_fields.is_empty() { - failure::bail!( - "Your wrangler.toml is missing the {} {:?} which {} required to publish to {}!", - field_pluralization, - missing_fields, - is_are, - destination - ); - }; - - Ok(()) -} diff --git a/src/settings/project/mod.rs b/src/settings/project/mod.rs index 9ef0e589b..e08736142 100644 --- a/src/settings/project/mod.rs +++ b/src/settings/project/mod.rs @@ -71,6 +71,65 @@ impl Project { pub fn kv_namespaces(&self) -> Vec { self.kv_namespaces.clone().unwrap_or_else(Vec::new) } + + pub fn validate(&self, release: bool) -> Result<(), failure::Error> { + let mut missing_fields = Vec::new(); + + if self.account_id.is_empty() { + missing_fields.push("account_id") + }; + if self.name.is_empty() { + missing_fields.push("name") + }; + + match &self.kv_namespaces { + Some(kv_namespaces) => { + for kv in kv_namespaces { + if kv.binding.is_empty() { + missing_fields.push("kv-namespace binding") + } + + if kv.id.is_empty() { + missing_fields.push("kv-namespace id") + } + } + } + None => {} + } + + let destination = if release { + //check required fields for release + if self.zone_id.as_ref().unwrap_or(&"".to_string()).is_empty() { + missing_fields.push("zone_id") + }; + if self.route.as_ref().unwrap_or(&"".to_string()).is_empty() { + missing_fields.push("route") + }; + //zoned deploy destination + "a route" + } else { + //zoneless deploy destination + "your subdomain" + }; + + let (field_pluralization, is_are) = match missing_fields.len() { + n if n >= 2 => ("fields", "are"), + 1 => ("field", "is"), + _ => ("", ""), + }; + + if !missing_fields.is_empty() { + failure::bail!( + "Your wrangler.toml is missing the {} {:?} which {} required to publish to {}!", + field_pluralization, + missing_fields, + is_are, + destination + ); + }; + + Ok(()) + } } fn get_project_config(config_path: &Path) -> Result {