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

fixes more clippy warnings #1306

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/dev/gcs/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(super) async fn serve(
let https = HttpsConnector::new();
let client = HyperClient::builder().build::<_, Body>(https);

let listening_address = server_config.listening_address.clone();
let listening_address = server_config.listening_address;

// create a closure that hyper will use later to handle HTTP requests
// this takes care of sending an incoming request along to
Expand Down
2 changes: 1 addition & 1 deletion src/commands/dev/gcs/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn watch_for_changes(
let (sender, receiver) = mpsc::channel();
watch_and_build(&target, Some(sender))?;

while let Ok(_) = receiver.recv() {
while receiver.recv().is_ok() {
let user = user.clone();
let target = target.clone();

Expand Down
6 changes: 3 additions & 3 deletions src/commands/dev/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ pub async fn listen(session_id: String) -> Result<(), failure::Error> {
let printer = print_ws_messages(read);

// run the heartbeat and message printer in parallel
match tokio::try_join!(heartbeat, keep_alive_to_ws, printer) {
Ok(_) => break Ok(()),
Err(_) => {}
if tokio::try_join!(heartbeat, keep_alive_to_ws, printer).is_ok() {
break Ok(());
} else {
}
}
}
Expand Down
11 changes: 3 additions & 8 deletions src/commands/preview.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use crate::preview;
use crate::preview::PreviewOpt;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::Target;

pub fn run(
target: Target,
user: Option<GlobalUser>,
method: &str,
url: &str,
body: Option<String>,
livereload: bool,
options: PreviewOpt,
verbose: bool,
headless: bool,
) -> Result<(), failure::Error> {
preview(
target, user, method, url, body, livereload, verbose, headless,
)
preview(target, user, options, verbose)
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
extern crate text_io;

mod build;
mod preview;
pub use build::build;
pub use preview::preview;
pub mod commands;
pub mod deploy;
pub mod http;
pub mod install;
pub mod installer;
pub mod preview;
pub mod settings;
pub mod tail;
pub mod terminal;
Expand Down
27 changes: 25 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ use std::str::FromStr;

use clap::{App, AppSettings, Arg, ArgGroup, SubCommand};
use exitfailure::ExitFailure;
use url::Url;

use wrangler::commands;
use wrangler::commands::kv::key::KVMetaData;
use wrangler::installer;
use wrangler::preview::{HttpMethod, PreviewOpt};
use wrangler::settings;
use wrangler::settings::global_user::GlobalUser;
use wrangler::settings::toml::TargetType;
Expand Down Expand Up @@ -655,11 +657,32 @@ fn run() -> Result<(), failure::Error> {
None => None,
};

let watch = matches.is_present("watch");
let livereload = matches.is_present("watch");
let verbose = matches.is_present("verbose");
let headless = matches.is_present("headless");

commands::preview(target, user, method, url, body, watch, verbose, headless)?;
let method = HttpMethod::from_str(method)?;
let url = Url::parse(url)?;

// Validate the URL scheme
failure::ensure!(
match url.scheme() {
"http" => true,
"https" => true,
_ => false,
},
"Invalid URL scheme (use either \"https\" or \"http\")"
);

let options = PreviewOpt {
method,
url,
body,
livereload,
headless,
};

commands::preview(target, user, options, verbose)?;
} else if let Some(matches) = matches.subcommand_matches("dev") {
log::info!("Starting dev server");
let port: Option<u16> = matches
Expand Down
1 change: 1 addition & 0 deletions src/preview/http_method.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::str::FromStr;

#[derive(Clone, Debug)]
pub enum HttpMethod {
Get,
Post,
Expand Down
41 changes: 16 additions & 25 deletions src/preview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod upload;
pub use upload::upload;

use std::process::Command;
use std::str::FromStr;
use std::sync::mpsc::channel;
use std::thread;

Expand All @@ -29,46 +28,29 @@ use crate::watch::watch_and_build;
pub fn preview(
mut target: Target,
user: Option<GlobalUser>,
method: &str,
url: &str,
body: Option<String>,
livereload: bool,
options: PreviewOpt,
verbose: bool,
headless: bool,
) -> Result<(), failure::Error> {
let method = HttpMethod::from_str(method)?;
let url = Url::parse(url)?;

// Validate the URL scheme
failure::ensure!(
match url.scheme() {
"http" => true,
"https" => true,
_ => false,
},
"Invalid URL scheme (use either \"https\" or \"http\")"
);

build(&target)?;

let sites_preview: bool = target.site.is_some();

let script_id = upload(&mut target, user.as_ref(), sites_preview, verbose)?;

let request_payload = RequestPayload::create(method, url, body);
let request_payload = RequestPayload::create(options.method, options.url, options.body);

let session = &request_payload.session;
let browser_url = &request_payload.browser_url;

if livereload {
if options.livereload {
// explicitly use 127.0.0.1, since localhost can resolve to 2 addresses
let server = WebSocket::new(|out| FiddleMessageServer { out })?.bind("127.0.0.1:0")?;

let ws_port = server.local_addr()?.port();

info!("Opened websocket server on port {}", ws_port);

if !headless {
if !options.headless {
open_browser(&format!(
"https://cloudflareworkers.com/?wrangler_session_id={0}&wrangler_ws_port={1}&hide_editor#{2}:{3}",
session, ws_port, script_id, browser_url
Expand All @@ -85,11 +67,11 @@ pub fn preview(
user.as_ref(),
broadcaster,
verbose,
headless,
options.headless,
request_payload,
)?;
} else {
if !headless {
if !options.headless {
open_browser(&format!(
"https://cloudflareworkers.com/?hide_editor#{0}:{1}",
script_id, browser_url
Expand All @@ -102,6 +84,15 @@ pub fn preview(
Ok(())
}

#[derive(Clone, Debug)]
pub struct PreviewOpt {
pub method: HttpMethod,
pub url: Url,
pub body: Option<String>,
pub livereload: bool,
pub headless: bool,
}

fn open_browser(url: &str) -> Result<(), failure::Error> {
let _output = if cfg!(target_os = "windows") {
let url_escaped = url.replace("&", "^&");
Expand Down Expand Up @@ -181,7 +172,7 @@ fn watch_for_changes(
let (tx, rx) = channel();
watch_and_build(&target, Some(tx))?;

while let Ok(_) = rx.recv() {
while rx.recv().is_ok() {
if let Ok(new_id) = upload(&mut target, user, sites_preview, verbose) {
let script_id = new_id.to_string();

Expand Down
4 changes: 2 additions & 2 deletions src/settings/toml/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn it_builds_from_environments_config_with_kv() {
assert!(kv_namespaces.contains(&kv_1));
assert!(kv_namespaces.contains(&kv_2));
}
None => assert!(false),
None => panic!("found no kv namespaces"),
}

let target = manifest.get_target(Some("staging")).unwrap();
Expand All @@ -78,7 +78,7 @@ fn it_builds_from_environments_config_with_kv() {
assert!(kv_namespaces.contains(&kv_1));
assert!(kv_namespaces.contains(&kv_2));
}
None => assert!(false),
None => panic!("found no kv namespaces"),
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/tail/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ async fn get_tunnel_url(metrics_port: u16) -> Result<String, failure::Error> {
if let Ok(resp) = reqwest::get(&metrics_url).await {
let body = resp.text().await?;

for url_match in url_regex.captures_iter(&body) {
let url = url_match[1].to_string();
return Ok(url);
if let Some(capture) = url_regex.captures(&body) {
if let Some(url) = capture.get(1) {
return Ok(url.as_str().to_string());
}
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/terminal/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ pub fn get_user_input_multi_line(prompt_string: &str) -> String {
println!("{}", prompt_string);
let mut input = String::new();
// are we reading from user input?
if atty::is(Stream::Stdin) {
input = read!("{}\n");
let mut input = if atty::is(Stream::Stdin) {
read!("{}\n")
} else {
// or is this data from a pipe? (support newlines)
drop(io::stdin().read_to_string(&mut input));
}
let tmp = String::new();
let _ = io::stdin().read_to_string(&mut input);
tmp
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this overwriting input with an empty string? I was trying to trace down an empty input when using pipe in 1.9.1, reverting back to 1.9.0 worked for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

whhooooppss - it looks like that should be tmp not input. probably gonna need another point release for this one. thanks @dmcgowan

};
input = strip_trailing_whitespace(input);
input
}
Expand Down
2 changes: 1 addition & 1 deletion src/watch/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn wait_for_changes(
Ok(Some(path)) => {
message::working("Detected changes...");
// wait for cooldown
while let Ok(_) = rx.recv_timeout(cooldown) {}
while rx.recv_timeout(cooldown).is_ok() {}
return Ok(path);
}
Ok(None) => {
Expand Down
4 changes: 2 additions & 2 deletions src/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn setup_build(target: &Target) -> Result<(Command, PathBuf, Bundle), failure::E

command.arg(format!(
"--output-file={}",
temp_file.clone().to_str().unwrap().to_string()
temp_file.to_str().unwrap().to_string()
));

let bundle = Bundle::new(&build_dir);
Expand Down Expand Up @@ -316,7 +316,7 @@ fn install() -> Result<PathBuf, failure::Error> {
wranglerjs_path.path()
};

run_npm_install(&wranglerjs_path.clone()).expect("could not install wranglerjs dependencies");
run_npm_install(&wranglerjs_path).expect("could not install wranglerjs dependencies");
Ok(wranglerjs_path)
}

Expand Down