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

Add output option for wrangler publish #1507

Closed
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 23 additions & 3 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 super::DEFAULT_CONFIG_PATH;
use crate::terminal;

pub fn run(matches: &ArgMatches) -> Result<(), failure::Error> {
log::info!("Getting project settings");
Expand All @@ -14,5 +14,25 @@ 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)?;
build(&target)
if matches.is_present("output") {
nataliescottdavidson marked this conversation as resolved.
Show resolved Hide resolved
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");
nataliescottdavidson marked this conversation as resolved.
Show resolved Hide resolved
}
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);
build(&target)
}
}
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
nataliescottdavidson marked this conversation as resolved.
Show resolved Hide resolved
)
.arg(wrangler_file.clone())
.arg(silent_verbose_arg.clone()),
)
Expand Down
43 changes: 42 additions & 1 deletion src/terminal/message.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
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<String>,
nataliescottdavidson marked this conversation as resolved.
Show resolved Hide resolved
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
}

static OUTPUT_TYPE: OnceCell<OutputType> = OnceCell::new();

pub fn set_output_type(typ: OutputType) {
match OUTPUT_TYPE.set(typ) {
Ok(_) => {}
Err(_) => {
message(&"Output type already set".to_string());
nataliescottdavidson marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

// Always goes to stdout
pub fn jsonout<T>(value: &T)
where
T: ?Sized + Serialize,
{
println!("{}", &serde_json::to_string(value).unwrap());
}

fn message(msg: &str) {
println!("{}", msg);
match OUTPUT_TYPE.get() {
Some(OutputType::Json) => {
eprintln!("{}", msg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these backwards? i think json should go to stdout and human readable should be stderr, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json should go to stdout and human readable should be stderr, no?
Yes- IFF json output flag is given. I made the assumption messages are for human readable output. It made sense to me to make a json type here. I'm a rust novice so let me know if there is a better way.
wrangler build | publish --output json

Wrangler supports structured output for core commands build and publish. JSON string goes to stdout, stderr displays user output.

js

nat@cf:~/examplejs$wrangler build
💁  JavaScript project found. Skipping unnecessary build!
nat@cf:~/examplejs$wrangler build --output json
{"success":true,"name":"examplejs"}
nat@davidson:~/examplejs$ ../wrangler/target/debug/wrangler publish --output json `returns no output, need to fix.

Webpack build and publish

nat@davidson:~/demo-site$ ../wrangler/target/debug/wrangler build --output json
{"success":true,"name":"demo-site"}
nat@davidson:~/demo-site$ ../wrangler/target/debug/wrangler publish --output json
{"success":true,"name":"demo-site","urls":["https://demo-site.nats.workers.dev"]}

Next steps: extend to other worker types

}
_ => {
println!("{}", msg);
}
}
}

pub fn billboard(msg: &str) {
Expand Down