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

Commit

Permalink
Showing 4 changed files with 14 additions and 50 deletions.
19 changes: 9 additions & 10 deletions src/commands/publish/mod.rs
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ pub fn publish(user: &GlobalUser, project: &Project, release: bool) -> Result<()
info!("release = {}", release);

validate_project(project, release)?;
upload_assets(project)?;
upload_bucket(project)?;
commands::build(&project)?;
publish_script(&user, &project, release)?;
if release {
@@ -82,20 +82,18 @@ fn publish_script(
Ok(())
}

fn upload_assets(project: &Project) -> Result<(), failure::Error> {
match &project.assets {
Some(assets) => {
let path = Path::new(&assets.directory);
fn upload_bucket(project: &Project) -> Result<(), failure::Error> {
for namespace in &project.kv_namespaces() {
if let Some(bucket) = &namespace.bucket {
let path = Path::new(&bucket);
match metadata(path) {
Ok(ref file_type) if file_type.is_file() => {
panic!("assets should point to a directory");
panic!("bucket should point to a directory");
}
Ok(ref file_type) if file_type.is_dir() => {
println!("Publishing contents of directory {:?}", path.as_os_str());

let (directory, namespace_id) = project.asset_directory_with_kv()?;

kv::write_bulk(&namespace_id, Path::new(&directory))
kv::write_bulk(&namespace.id, Path::new(&path))?;
}
Ok(file_type) => {
// any other file types (namely, symlinks)
@@ -108,8 +106,9 @@ fn upload_assets(project: &Project) -> Result<(), failure::Error> {
Err(e) => panic!(e),
}
}
None => panic!("no assets directory specified"),
}

Ok(())
}

fn build_subdomain_request() -> String {
1 change: 1 addition & 0 deletions src/settings/project/kv_namespace.rs
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
pub struct KvNamespace {
pub id: String,
pub binding: String,
pub bucket: Option<String>,
}

impl fmt::Display for KvNamespace {
27 changes: 2 additions & 25 deletions src/settings/project/mod.rs
Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@ pub struct Project {
pub routes: Option<HashMap<String, String>>,
#[serde(rename = "kv-namespaces")]
pub kv_namespaces: Option<Vec<KvNamespace>>,
pub assets: Option<Assets>,
}

impl Project {
@@ -48,7 +47,6 @@ impl Project {
routes: None,
kv_namespaces: None,
webpack_config: None,
assets: None,
};

let toml = toml::to_string(&project)?;
@@ -73,23 +71,6 @@ impl Project {
pub fn kv_namespaces(&self) -> Vec<KvNamespace> {
self.kv_namespaces.clone().unwrap_or_else(Vec::new)
}

pub fn asset_directory_with_kv(&self) -> Result<(String, String), failure::Error> {
let directory;
let binding;
if let Some(a) = &self.assets {
directory = a.directory.clone();
binding = a.kv.clone();
} else {
failure::bail!("No assets defined");
}

let namespaces = self.kv_namespaces();

let namespace = namespaces.iter().find(|ns| ns.binding == binding).unwrap();

Ok((directory, namespace.id.clone()))
}
}

fn get_project_config(config_path: &Path) -> Result<Project, failure::Error> {
@@ -118,9 +99,11 @@ fn get_project_config(config_path: &Path) -> Result<Project, failure::Error> {
[[kv-namespaces]]
binding = "BINDING_NAME"
id = "0f2ac74b498b48028cb68387c421e279"
bucket = "./public" # OPTIONAL
# binding is the variable name you wish to bind the namespace to in your script.
# id is the namespace_id assigned to your kv namespace upon creation. e.g. (per namespace)
# bucket is the path to the directory you want to upload relative to your wrangler.toml
"##;

println!("{}", fmt_demo);
@@ -145,11 +128,5 @@ id = "0f2ac74b498b48028cb68387c421e279"
}
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Assets {
pub directory: String,
pub kv: String,
}

#[cfg(test)]
mod tests;
17 changes: 2 additions & 15 deletions src/settings/project/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -21,10 +21,12 @@ fn it_builds_from_config_with_kv() {
let kv_1 = KvNamespace {
id: "somecrazylongidentifierstring".to_string(),
binding: "prodKV".to_string(),
bucket: None,
};
let kv_2 = KvNamespace {
id: "anotherwaytoolongidstring".to_string(),
binding: "stagingKV".to_string(),
bucket: None,
};

match project.kv_namespaces {
@@ -37,21 +39,6 @@ fn it_builds_from_config_with_kv() {
}
}

#[test]
fn it_builds_from_config_with_assets() {
let toml_path = toml_fixture_path("assets");

let project = get_project_config(&toml_path).unwrap();

match project.assets {
Some(assets) => {
assert_eq!(assets.directory, "./");
assert_eq!(assets.kv, "prodKV");
}
None => assert!(false),
}
}

fn toml_fixture_path(fixture: &str) -> PathBuf {
let current_dir = env::current_dir().unwrap();

0 comments on commit 3652c16

Please sign in to comment.