From b9effacafedf520ca154e19cb787fc0de05d799b Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Tue, 18 Aug 2020 12:44:50 -0400 Subject: [PATCH 01/12] pipe output to stderr on json output --- Cargo.lock | 5 +++-- Cargo.toml | 1 + src/commands/build.rs | 13 ++++++++++++- src/main.rs | 7 +++++++ src/terminal/message.rs | 26 +++++++++++++++++++++++++- 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33ce7f5b6..eeb153caa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1521,9 +1521,9 @@ checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5" [[package]] name = "once_cell" -version = "1.4.0" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d" +checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" [[package]] name = "opaque-debug" @@ -2977,6 +2977,7 @@ dependencies = [ "log 0.4.11", "notify", "number_prefix 0.4.0", + "once_cell", "openssl", "percent-encoding 2.1.0", "predicates", diff --git a/Cargo.toml b/Cargo.toml index e02d9566c..fdd902590 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ log = "0.4.11" notify = "4.0.15" number_prefix = "0.4.0" openssl = { version = "0.10.29", optional = true } +once_cell = "1.4.1" percent-encoding = "2.1.0" predicates = "1.0.5" prettytable-rs = "0.8.0" diff --git a/src/commands/build.rs b/src/commands/build.rs index 1135ea435..cf5a9acd9 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -4,7 +4,7 @@ use clap::ArgMatches; use crate::build; use crate::settings::toml::Manifest; - +use crate::terminal; use super::DEFAULT_CONFIG_PATH; pub fn run(matches: &ArgMatches) -> Result<(), failure::Error> { @@ -14,5 +14,16 @@ pub fn run(matches: &ArgMatches) -> Result<(), failure::Error> { let manifest = Manifest::new(&config_path)?; let env = matches.value_of("env"); let target = &manifest.get_target(env, false)?; + if matches.is_present("output") { + if matches.value_of("output") == Some("json") { + terminal::message::set_output_type(terminal::message::OutputType::Json) + } + else { + terminal::message::user_error("json is the only valid value for output flag"); + } + } + else { + terminal::message::set_output_type(terminal::message::OutputType::Human) + } build(&target) } diff --git a/src/main.rs b/src/main.rs index c607b6a76..98fe22a85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -440,6 +440,13 @@ fn run() -> Result<(), failure::Error> { .long("env") .takes_value(true) ) + .arg( + Arg::with_name("output") + .short("o") + .long("output") + .takes_value(true) + .help("Machine or human readable output") + ) .arg(wrangler_file.clone()) .arg(silent_verbose_arg.clone()), ) diff --git a/src/terminal/message.rs b/src/terminal/message.rs index b08ec2ec4..a9b4733ca 100644 --- a/src/terminal/message.rs +++ b/src/terminal/message.rs @@ -1,9 +1,33 @@ use super::emoji; +use once_cell::sync::OnceCell; use billboard::{Billboard, BorderColor, BorderStyle}; +pub enum OutputType { + Json, + Human +} + +static OUTPUT_TYPE: OnceCell = OnceCell::new(); + +pub fn set_output_type(typ: OutputType) { + OUTPUT_TYPE.set(typ); +} + fn message(msg: &str) { - println!("{}", msg); + match OUTPUT_TYPE.get() { + Some(OutputType::Json) => { + println!("json"); + eprintln!("{}", msg); + } + Some(OutputType::Human) => { + println!("human"); + println!("{}", msg); + } + _ => { + panic!("output not defined") + } + } } pub fn billboard(msg: &str) { From 34072e158ed516f3232c4a10385b7cd35a2fd450 Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Tue, 18 Aug 2020 13:37:48 -0400 Subject: [PATCH 02/12] Re-add json out --- src/commands/build.rs | 15 +++++++++++++-- src/terminal/message.rs | 18 ++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/commands/build.rs b/src/commands/build.rs index cf5a9acd9..6932c9321 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -21,9 +21,20 @@ pub fn run(matches: &ArgMatches) -> Result<(), failure::Error> { else { terminal::message::user_error("json is the only valid value for output flag"); } + let result = build(&target); + let jsonoutput = terminal::message::PublishOutput { + name: Some(target.name.clone()), + success: match result { + Ok(_) => Some("true".to_string()), + Err(_) => Some("false".to_string()) + }, + url: None, + }; + terminal::message::jsonout(&jsonoutput); + result } else { - terminal::message::set_output_type(terminal::message::OutputType::Human) + terminal::message::set_output_type(terminal::message::OutputType::Human); + build(&target) } - build(&target) } diff --git a/src/terminal/message.rs b/src/terminal/message.rs index a9b4733ca..bc0cc3b28 100644 --- a/src/terminal/message.rs +++ b/src/terminal/message.rs @@ -2,26 +2,40 @@ use super::emoji; use once_cell::sync::OnceCell; use billboard::{Billboard, BorderColor, BorderStyle}; +use serde::{Deserialize, Serialize}; pub enum OutputType { Json, Human } +#[derive(Serialize, Deserialize)] +pub struct PublishOutput { + pub success : Option, + pub name : Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub url : Option, +} + static OUTPUT_TYPE: OnceCell = OnceCell::new(); pub fn set_output_type(typ: OutputType) { OUTPUT_TYPE.set(typ); } +// Always goes to stdout +pub fn jsonout(value: &T) +where + T: ?Sized + Serialize, { + println!("{}", &serde_json::to_string(value).unwrap()); +} + fn message(msg: &str) { match OUTPUT_TYPE.get() { Some(OutputType::Json) => { - println!("json"); eprintln!("{}", msg); } Some(OutputType::Human) => { - println!("human"); println!("{}", msg); } _ => { From 0f869bc2b0a5075574f92d644ecbba4248868ce7 Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Tue, 18 Aug 2020 13:51:13 -0400 Subject: [PATCH 03/12] Formatting --- src/commands/build.rs | 8 +++----- src/terminal/message.rs | 17 ++++++++--------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/commands/build.rs b/src/commands/build.rs index 6932c9321..75a306008 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -17,8 +17,7 @@ pub fn run(matches: &ArgMatches) -> Result<(), failure::Error> { if matches.is_present("output") { if matches.value_of("output") == Some("json") { terminal::message::set_output_type(terminal::message::OutputType::Json) - } - else { + } else { terminal::message::user_error("json is the only valid value for output flag"); } let result = build(&target); @@ -26,14 +25,13 @@ pub fn run(matches: &ArgMatches) -> Result<(), failure::Error> { name: Some(target.name.clone()), success: match result { Ok(_) => Some("true".to_string()), - Err(_) => Some("false".to_string()) + Err(_) => Some("false".to_string()), }, url: None, }; terminal::message::jsonout(&jsonoutput); result - } - else { + } else { terminal::message::set_output_type(terminal::message::OutputType::Human); build(&target) } diff --git a/src/terminal/message.rs b/src/terminal/message.rs index bc0cc3b28..fbf7963e2 100644 --- a/src/terminal/message.rs +++ b/src/terminal/message.rs @@ -6,15 +6,15 @@ use serde::{Deserialize, Serialize}; pub enum OutputType { Json, - Human + Human, } #[derive(Serialize, Deserialize)] pub struct PublishOutput { - pub success : Option, - pub name : Option, + pub success: Option, + pub name: Option, #[serde(skip_serializing_if = "Option::is_none")] - pub url : Option, + pub url: Option, } static OUTPUT_TYPE: OnceCell = OnceCell::new(); @@ -26,8 +26,9 @@ pub fn set_output_type(typ: OutputType) { // Always goes to stdout pub fn jsonout(value: &T) where - T: ?Sized + Serialize, { - println!("{}", &serde_json::to_string(value).unwrap()); + T: ?Sized + Serialize, +{ + println!("{}", &serde_json::to_string(value).unwrap()); } fn message(msg: &str) { @@ -38,9 +39,7 @@ fn message(msg: &str) { Some(OutputType::Human) => { println!("{}", msg); } - _ => { - panic!("output not defined") - } + _ => panic!("output not defined"), } } From a86d6da47a1032947655fa4ca0908f9ce220aef8 Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Tue, 18 Aug 2020 14:10:35 -0400 Subject: [PATCH 04/12] cargo fmt wants to move this line --- src/commands/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/build.rs b/src/commands/build.rs index 75a306008..b28ed0d96 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -2,10 +2,10 @@ use std::path::Path; use clap::ArgMatches; +use super::DEFAULT_CONFIG_PATH; use crate::build; use crate::settings::toml::Manifest; use crate::terminal; -use super::DEFAULT_CONFIG_PATH; pub fn run(matches: &ArgMatches) -> Result<(), failure::Error> { log::info!("Getting project settings"); From f077c1c7fd6973800cd5435a6afbb1e69d773e8b Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Tue, 18 Aug 2020 14:30:15 -0400 Subject: [PATCH 05/12] Handle set output type failure --- src/terminal/message.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/terminal/message.rs b/src/terminal/message.rs index fbf7963e2..e93b9d5bb 100644 --- a/src/terminal/message.rs +++ b/src/terminal/message.rs @@ -20,7 +20,13 @@ pub struct PublishOutput { static OUTPUT_TYPE: OnceCell = OnceCell::new(); pub fn set_output_type(typ: OutputType) { - OUTPUT_TYPE.set(typ); + match OUTPUT_TYPE.set(typ) { + Ok(_) => {} + Err(_) => { + let msg = format!("Output type already set"); + message(&msg); + } + } } // Always goes to stdout From f4bcef35ea0361db51c5f7b33fe61638357c8077 Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Tue, 18 Aug 2020 14:36:43 -0400 Subject: [PATCH 06/12] fix idiomatic code --- src/terminal/message.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/terminal/message.rs b/src/terminal/message.rs index e93b9d5bb..462523f9b 100644 --- a/src/terminal/message.rs +++ b/src/terminal/message.rs @@ -23,8 +23,7 @@ pub fn set_output_type(typ: OutputType) { match OUTPUT_TYPE.set(typ) { Ok(_) => {} Err(_) => { - let msg = format!("Output type already set"); - message(&msg); + message(&"Output type already set".to_string()); } } } From 405ac3165ff8a11a832984bf648653c844b910f9 Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Tue, 18 Aug 2020 14:52:50 -0400 Subject: [PATCH 07/12] Fix tests --- src/terminal/message.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/terminal/message.rs b/src/terminal/message.rs index 462523f9b..2fa328d2f 100644 --- a/src/terminal/message.rs +++ b/src/terminal/message.rs @@ -41,10 +41,9 @@ fn message(msg: &str) { Some(OutputType::Json) => { eprintln!("{}", msg); } - Some(OutputType::Human) => { + _ => { println!("{}", msg); } - _ => panic!("output not defined"), } } From 2db0c313e561a99e9353da4a15f33048787acff7 Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Tue, 18 Aug 2020 16:11:05 -0400 Subject: [PATCH 08/12] Add json output option to `wrangler publish` --- src/commands/build.rs | 6 +++--- src/commands/publish.rs | 17 ++++++++++++++--- src/deploy/mod.rs | 15 ++++++++++++--- src/main.rs | 17 ++++++++++++++++- src/terminal/message.rs | 11 ++++++++--- 5 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/commands/build.rs b/src/commands/build.rs index b28ed0d96..88d2c3e6e 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -24,10 +24,10 @@ pub fn run(matches: &ArgMatches) -> Result<(), failure::Error> { let jsonoutput = terminal::message::PublishOutput { name: Some(target.name.clone()), success: match result { - Ok(_) => Some("true".to_string()), - Err(_) => Some("false".to_string()), + Ok(_) => Some(true), + Err(_) => Some(false), }, - url: None, + urls: Vec::new(), }; terminal::message::jsonout(&jsonoutput); result diff --git a/src/commands/publish.rs b/src/commands/publish.rs index 0ad0efef9..842088031 100644 --- a/src/commands/publish.rs +++ b/src/commands/publish.rs @@ -61,8 +61,19 @@ pub fn publish( // Next, upload and deploy the worker with the updated asset_manifest upload::script(&upload_client, &target, Some(asset_manifest))?; - deploy::worker(&user, &deploy_config)?; - + if message::is_json_output() { + let result = build(&target); + let mut jsonoutput = message::PublishOutput { + name: Some(target.name.clone()), + success: match result { + Ok(_) => Some(true), + Err(_) => Some(true), + }, + urls: Vec::new(), + }; + deploy::worker(&user, &deploy_config, Some(&mut jsonoutput))?; + message::jsonout(&jsonoutput); + } // Finally, remove any stale files if !to_delete.is_empty() { message::info("Deleting stale files..."); @@ -94,7 +105,7 @@ pub fn publish( upload::script(&upload_client, &target, None)?; - deploy::worker(&user, &deploy_config)?; + deploy::worker(&user, &deploy_config, None)?; } Ok(()) diff --git a/src/deploy/mod.rs b/src/deploy/mod.rs index dce5f2ca0..ca6c7e462 100644 --- a/src/deploy/mod.rs +++ b/src/deploy/mod.rs @@ -7,7 +7,11 @@ use crate::settings::global_user::GlobalUser; use crate::settings::toml::{DeployConfig, Zoneless}; use crate::terminal::message; -pub fn worker(user: &GlobalUser, deploy_config: &DeployConfig) -> Result<(), failure::Error> { +pub fn worker( + user: &GlobalUser, + deploy_config: &DeployConfig, + publish_out: Option<&mut message::PublishOutput>, +) -> Result<(), failure::Error> { match deploy_config { DeployConfig::Zoneless(zoneless_config) => { // this is a zoneless deploy @@ -18,6 +22,9 @@ pub fn worker(user: &GlobalUser, deploy_config: &DeployConfig) -> Result<(), fai "Successfully published your script to {}", deploy_address )); + if let Some(pub_out) = publish_out { + pub_out.urls.push(deploy_address); + }; Ok(()) } @@ -27,14 +34,16 @@ pub fn worker(user: &GlobalUser, deploy_config: &DeployConfig) -> Result<(), fai let published_routes = publish_routes(&user, zoned_config)?; - let display_results: Vec = + let mut display_results: Vec = published_routes.iter().map(|r| format!("{}", r)).collect(); message::success(&format!( "Deployed to the following routes:\n{}", display_results.join("\n") )); - + if let Some(pub_out) = publish_out { + pub_out.urls.append(&mut display_results); + }; Ok(()) } } diff --git a/src/main.rs b/src/main.rs index 98fe22a85..8b76330ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -565,6 +565,13 @@ fn run() -> Result<(), failure::Error> { .long("release") .takes_value(false) .help("[deprecated] alias of wrangler publish") + ) + .arg( + Arg::with_name("output") + .short("o") + .long("output") + .takes_value(true) + .help("Machine or human readable output") ), ) .subcommand( @@ -861,7 +868,15 @@ fn run() -> Result<(), failure::Error> { let env = matches.value_of("env"); let mut target = manifest.get_target(env, is_preview)?; let deploy_config = manifest.deploy_config(env)?; - + if matches.is_present("output") { + if matches.value_of("output") == Some("json") { + message::set_output_type(message::OutputType::Json) + } else { + message::user_error("json is the only valid value for output flag"); + } + } else { + message::set_output_type(message::OutputType::Human); + } commands::publish(&user, &mut target, deploy_config)?; } else if let Some(matches) = matches.subcommand_matches("subdomain") { log::info!("Getting project settings"); diff --git a/src/terminal/message.rs b/src/terminal/message.rs index 2fa328d2f..cca0b6152 100644 --- a/src/terminal/message.rs +++ b/src/terminal/message.rs @@ -4,6 +4,7 @@ use once_cell::sync::OnceCell; use billboard::{Billboard, BorderColor, BorderStyle}; use serde::{Deserialize, Serialize}; +#[derive(PartialEq)] pub enum OutputType { Json, Human, @@ -11,10 +12,10 @@ pub enum OutputType { #[derive(Serialize, Deserialize)] pub struct PublishOutput { - pub success: Option, + pub success: Option, pub name: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub url: Option, + #[serde(skip_serializing_if = "Vec::is_empty")] + pub urls: Vec, } static OUTPUT_TYPE: OnceCell = OnceCell::new(); @@ -28,6 +29,10 @@ pub fn set_output_type(typ: OutputType) { } } +pub fn is_json_output() -> bool { + OUTPUT_TYPE.get() == Some(&OutputType::Json) +} + // Always goes to stdout pub fn jsonout(value: &T) where From 204e8f96879af5ce1b98e3f37d575af9fce97490 Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Wed, 19 Aug 2020 16:30:12 -0400 Subject: [PATCH 09/12] Addressed comments re: terminal messages --- src/commands/build.rs | 8 ++------ src/main.rs | 12 ++++-------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/commands/build.rs b/src/commands/build.rs index 88d2c3e6e..1782ba8db 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -14,12 +14,8 @@ pub fn run(matches: &ArgMatches) -> Result<(), failure::Error> { let manifest = Manifest::new(&config_path)?; let env = matches.value_of("env"); let target = &manifest.get_target(env, false)?; - if matches.is_present("output") { - if matches.value_of("output") == Some("json") { - terminal::message::set_output_type(terminal::message::OutputType::Json) - } else { - terminal::message::user_error("json is the only valid value for output flag"); - } + if matches.is_present("output") && matches.value_of("output") == Some("json") { + terminal::message::set_output_type(terminal::message::OutputType::Json); let result = build(&target); let jsonoutput = terminal::message::PublishOutput { name: Some(target.name.clone()), diff --git a/src/main.rs b/src/main.rs index 8b76330ae..554803d02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -445,7 +445,7 @@ fn run() -> Result<(), failure::Error> { .short("o") .long("output") .takes_value(true) - .help("Machine or human readable output") + .possible_value("json") ) .arg(wrangler_file.clone()) .arg(silent_verbose_arg.clone()), @@ -571,7 +571,7 @@ fn run() -> Result<(), failure::Error> { .short("o") .long("output") .takes_value(true) - .help("Machine or human readable output") + .possible_value("json") ), ) .subcommand( @@ -868,12 +868,8 @@ fn run() -> Result<(), failure::Error> { let env = matches.value_of("env"); let mut target = manifest.get_target(env, is_preview)?; let deploy_config = manifest.deploy_config(env)?; - if matches.is_present("output") { - if matches.value_of("output") == Some("json") { - message::set_output_type(message::OutputType::Json) - } else { - message::user_error("json is the only valid value for output flag"); - } + if matches.is_present("output") && matches.value_of("output") == Some("json") { + message::set_output_type(message::OutputType::Json) } else { message::set_output_type(message::OutputType::Human); } From f52917d2b6b2db3b4f92bf763f286aefaea0917f Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Wed, 19 Aug 2020 17:32:02 -0400 Subject: [PATCH 10/12] discard output type assignment result --- src/terminal/message.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/terminal/message.rs b/src/terminal/message.rs index cca0b6152..6a31ecdcd 100644 --- a/src/terminal/message.rs +++ b/src/terminal/message.rs @@ -21,12 +21,7 @@ pub struct PublishOutput { static OUTPUT_TYPE: OnceCell = OnceCell::new(); pub fn set_output_type(typ: OutputType) { - match OUTPUT_TYPE.set(typ) { - Ok(_) => {} - Err(_) => { - message(&"Output type already set".to_string()); - } - } + let _ = OUTPUT_TYPE.set(typ); } pub fn is_json_output() -> bool { From f1135e2c7674ead0b9ada7fba1fd7440ab0485bd Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Thu, 20 Aug 2020 10:31:49 -0400 Subject: [PATCH 11/12] Fixed copypasta --- src/commands/publish.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/publish.rs b/src/commands/publish.rs index 842088031..d9029d5f9 100644 --- a/src/commands/publish.rs +++ b/src/commands/publish.rs @@ -67,7 +67,7 @@ pub fn publish( name: Some(target.name.clone()), success: match result { Ok(_) => Some(true), - Err(_) => Some(true), + Err(_) => Some(false), }, urls: Vec::new(), }; From 29750f58571f5a8787fbdadc7e4e9e7f5b74b090 Mon Sep 17 00:00:00 2001 From: Natalie Davidson Date: Thu, 20 Aug 2020 16:57:12 -0400 Subject: [PATCH 12/12] Move progress messages to loginfo when json output flag is present --- src/terminal/message.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/terminal/message.rs b/src/terminal/message.rs index 6a31ecdcd..0b61a989d 100644 --- a/src/terminal/message.rs +++ b/src/terminal/message.rs @@ -39,7 +39,7 @@ where fn message(msg: &str) { match OUTPUT_TYPE.get() { Some(OutputType::Json) => { - eprintln!("{}", msg); + log::info!("{}", msg); } _ => { println!("{}", msg);