From b86946b78e1f88def2c0e42379dd460040015215 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 10 Feb 2021 11:33:29 -0600 Subject: [PATCH] use toml-edit for modifying provided wrangler.toml for wrangler generate so format is preserved (#1745) --- Cargo.lock | 52 +++++++++++++++++ Cargo.toml | 1 + src/settings/toml/manifest.rs | 107 ++++++++++++++++++++++------------ src/settings/toml/site.rs | 2 +- 4 files changed, 123 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a69387a74..74fca2119 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,6 +57,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "ascii" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eab1c04a571841102f5345a8fc0f6bb3d31c315dec879b5c6e42e40ce7ffa34e" + [[package]] name = "assert_cmd" version = "1.0.2" @@ -367,6 +373,19 @@ dependencies = [ "url", ] +[[package]] +name = "combine" +version = "3.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da3da6baa321ec19e1cc41d31bf599f00c783d0517095cdaf0332e3fe8d20680" +dependencies = [ + "ascii", + "byteorder", + "either", + "memchr", + "unreachable", +] + [[package]] name = "config" version = "0.10.1" @@ -700,6 +719,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + [[package]] name = "encode_unicode" version = "0.3.6" @@ -2769,6 +2794,17 @@ dependencies = [ "serde 1.0.119", ] +[[package]] +name = "toml_edit" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09391a441b373597cf0888d2b052dcf82c5be4fee05da3636ae30fb57aad8484" +dependencies = [ + "chrono", + "combine", + "linked-hash-map 0.5.4", +] + [[package]] name = "tower-service" version = "0.3.0" @@ -2925,6 +2961,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +dependencies = [ + "void", +] + [[package]] name = "untrusted" version = "0.7.1" @@ -2976,6 +3021,12 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + [[package]] name = "wait-timeout" version = "0.2.0" @@ -3221,6 +3272,7 @@ dependencies = [ "tokio-rustls", "tokio-tungstenite", "toml", + "toml_edit", "twox-hash", "url", "uuid", diff --git a/Cargo.toml b/Cargo.toml index 4d730e958..61d20fc5f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,6 +58,7 @@ tokio-native-tls = "0.1.0" tokio-rustls = "0.14.1" tokio-tungstenite = { version = "0.11.0", features = ["tls"] } toml = "0.5.8" +toml_edit = "0.2.0" twox-hash = "1.6.0" url = "2.2.0" uuid = { version = "0.8", features = ["v4"] } diff --git a/src/settings/toml/manifest.rs b/src/settings/toml/manifest.rs index 94a4afeaa..0f745b1ec 100644 --- a/src/settings/toml/manifest.rs +++ b/src/settings/toml/manifest.rs @@ -89,60 +89,91 @@ impl Manifest { config_path: &PathBuf, site: Option, ) -> Result { - let config_file = config_path.join("wrangler.toml"); - let template_config_content = fs::read_to_string(&config_file); - let template_config = match &template_config_content { - Ok(content) => { - let config: Manifest = toml::from_str(content)?; - config.warn_on_account_info(); - if let Some(target_type) = &target_type { - if config.target_type != *target_type { - StdOut::warn(&format!("The template recommends the \"{}\" type. Using type \"{}\" may cause errors, we recommend changing the type field in wrangler.toml to \"{}\"", config.target_type, target_type, config.target_type)); - } - } - Ok(config) - } - Err(err) => Err(err), - }; - let mut template_config = match template_config { - Ok(config) => config, - Err(err) => { - log::info!("Error parsing template {}", err); - log::debug!("template content {:?}", template_config_content); + let config_file = &config_path.join("wrangler.toml"); + let config_template_str = fs::read_to_string(config_file).unwrap_or_else(|err| { + log::info!("Error reading config template: {}", err); + log::info!("Using default instead"); + toml::to_string_pretty(&Manifest::default()) + .expect("serializing the default toml should never fail") + }); + + let config_template = + toml::from_str::(&config_template_str).unwrap_or_else(|err| { + log::info!("Error parsing config template: {}", err); + log::info!("Using default instead"); Manifest::default() - } - }; + }); - let default_workers_dev = match &template_config.route { - Some(route) => { - if route.is_empty() { - Some(true) - } else { - None - } + config_template.warn_on_account_info(); + if let Some(target_type) = &target_type { + if config_template.target_type != *target_type { + StdOut::warn(&format!("The template recommends the \"{}\" type. Using type \"{}\" may cause errors, we recommend changing the type field in wrangler.toml to \"{}\"", config_template.target_type, target_type, config_template.target_type)); } + } + + let default_workers_dev = match &config_template.route { + Some(route) if route.is_empty() => Some(true), None => Some(true), + _ => None, }; - template_config.name = name; - template_config.workers_dev = default_workers_dev; + /* + * We use toml-edit for actually changing the template provided wrangler.toml, + * since toml-edit is a format-preserving parser. Elsewhere, we use only toml-rs, + * as only toml-rs supports serde. + */ + + let mut config_template_doc = + config_template_str + .parse::() + .map_err(|err| { + failure::err_msg(format!( + "toml_edit failed to parse config template. {}", + err + )) + })?; + + config_template_doc["name"] = toml_edit::value(name); + if let Some(default_workers_dev) = default_workers_dev { + config_template_doc["workers_dev"] = toml_edit::value(default_workers_dev); + } if let Some(target_type) = &target_type { - template_config.target_type = target_type.clone(); + config_template_doc["target_type"] = toml_edit::value(target_type.to_string()); } - - if let Some(arg_site) = site { - if template_config.site.is_none() { - template_config.site = Some(arg_site); + if let Some(site) = site { + if config_template.site.is_none() { + config_template_doc["site"]["bucket"] = + toml_edit::value(site.bucket.to_string_lossy().as_ref()); + + if let Some(entry_point) = &site.entry_point { + config_template_doc["site"]["entry_point"] = + toml_edit::value(entry_point.to_string_lossy().as_ref()); + } + if let Some(include) = &site.include { + let mut arr = toml_edit::Array::default(); + include.iter().for_each(|i| { + arr.push(i.as_ref()).unwrap(); + }); + config_template_doc["site"]["include"] = toml_edit::value(arr); + } + if let Some(exclude) = &site.exclude { + let mut arr = toml_edit::Array::default(); + exclude.iter().for_each(|i| { + arr.push(i.as_ref()).unwrap(); + }); + config_template_doc["site"]["exclude"] = toml_edit::value(arr); + } } } // TODO: https://github.com/cloudflare/wrangler/issues/773 - let toml = toml::to_string(&template_config)?; + let toml = config_template_doc.to_string_in_original_order(); + let manifest = toml::from_str::(&toml)?; log::info!("Writing a wrangler.toml file at {}", config_file.display()); fs::write(&config_file, &toml)?; - Ok(template_config) + Ok(manifest) } pub fn worker_name(&self, env_arg: Option<&str>) -> String { diff --git a/src/settings/toml/site.rs b/src/settings/toml/site.rs index 0a0acb04a..9a188b7e6 100644 --- a/src/settings/toml/site.rs +++ b/src/settings/toml/site.rs @@ -13,7 +13,7 @@ const SITE_ENTRY_POINT: &str = "workers-site"; pub struct Site { pub bucket: PathBuf, #[serde(rename = "entry-point")] - entry_point: Option, + pub entry_point: Option, pub include: Option>, pub exclude: Option>, }