Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
refactor: make validate a method on project
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleymichal committed Aug 13, 2019
1 parent 60be134 commit d9c1460
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 65 deletions.
66 changes: 1 addition & 65 deletions src/commands/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
}
59 changes: 59 additions & 0 deletions src/settings/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,65 @@ impl Project {
pub fn kv_namespaces(&self) -> Vec<KvNamespace> {
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<Project, failure::Error> {
Expand Down

0 comments on commit d9c1460

Please sign in to comment.