summaryrefslogtreecommitdiff
path: root/cli/tools
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tools')
-rw-r--r--cli/tools/standalone.rs27
-rw-r--r--cli/tools/upgrade.rs70
2 files changed, 37 insertions, 60 deletions
diff --git a/cli/tools/standalone.rs b/cli/tools/standalone.rs
index 558adc460..ffc4d1f38 100644
--- a/cli/tools/standalone.rs
+++ b/cli/tools/standalone.rs
@@ -9,6 +9,8 @@ use crate::http_util::HttpClient;
use crate::standalone::Metadata;
use crate::standalone::MAGIC_TRAILER;
use crate::util::path::path_has_trailing_slash;
+use crate::util::progress_bar::ProgressBar;
+use crate::util::progress_bar::ProgressBarStyle;
use crate::ProcState;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
@@ -122,23 +124,26 @@ async fn download_base_binary(
binary_path_suffix: &str,
) -> Result<(), AnyError> {
let download_url = format!("https://dl.deno.land/{}", binary_path_suffix);
+ let maybe_bytes = {
+ let progress_bars = ProgressBar::new(ProgressBarStyle::DownloadBars);
+ let progress = progress_bars.update(&download_url);
- log::info!("Checking {}", &download_url);
-
- let res = client.get(&download_url).send().await?;
-
- let binary_content = if res.status().is_success() {
- log::info!("Download has been found");
- res.bytes().await?.to_vec()
- } else {
- log::info!("Download could not be found, aborting");
- std::process::exit(1)
+ client
+ .download_with_progress(download_url, &progress)
+ .await?
+ };
+ let bytes = match maybe_bytes {
+ Some(bytes) => bytes,
+ None => {
+ log::info!("Download could not be found, aborting");
+ std::process::exit(1)
+ }
};
std::fs::create_dir_all(output_directory)?;
let output_path = output_directory.join(binary_path_suffix);
std::fs::create_dir_all(output_path.parent().unwrap())?;
- tokio::fs::write(output_path, binary_content).await?;
+ tokio::fs::write(output_path, bytes).await?;
Ok(())
}
diff --git a/cli/tools/upgrade.rs b/cli/tools/upgrade.rs
index 1e0be728e..4b745b573 100644
--- a/cli/tools/upgrade.rs
+++ b/cli/tools/upgrade.rs
@@ -7,7 +7,6 @@ use crate::args::UpgradeFlags;
use crate::colors;
use crate::http_util::HttpClient;
use crate::proc_state::ProcState;
-use crate::util::display::human_download_size;
use crate::util::progress_bar::ProgressBar;
use crate::util::progress_bar::ProgressBarStyle;
use crate::version;
@@ -17,7 +16,6 @@ use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::futures::future::BoxFuture;
use deno_core::futures::FutureExt;
-use deno_core::futures::StreamExt;
use once_cell::sync::Lazy;
use std::borrow::Cow;
use std::env;
@@ -353,7 +351,9 @@ pub async fn upgrade(
)
};
- let archive_data = download_package(&client, &download_url).await?;
+ let archive_data = download_package(&client, &download_url)
+ .await
+ .with_context(|| format!("Failed downloading {}", download_url))?;
log::info!("Deno is upgrading to version {}", &install_version);
@@ -402,22 +402,20 @@ pub async fn upgrade(
async fn get_latest_release_version(
client: &HttpClient,
) -> Result<String, AnyError> {
- let res = client
- .get("https://dl.deno.land/release-latest.txt")
- .send()
+ let text = client
+ .download_text("https://dl.deno.land/release-latest.txt")
.await?;
- let version = res.text().await?.trim().to_string();
+ let version = text.trim().to_string();
Ok(version.replace('v', ""))
}
async fn get_latest_canary_version(
client: &HttpClient,
) -> Result<String, AnyError> {
- let res = client
- .get("https://dl.deno.land/canary-latest.txt")
- .send()
+ let text = client
+ .download_text("https://dl.deno.land/canary-latest.txt")
.await?;
- let version = res.text().await?.trim().to_string();
+ let version = text.trim().to_string();
Ok(version)
}
@@ -426,47 +424,21 @@ async fn download_package(
download_url: &str,
) -> Result<Vec<u8>, AnyError> {
log::info!("Downloading {}", &download_url);
-
- let res = client.get(download_url).send().await?;
-
- if res.status().is_success() {
- let total_size = res.content_length().unwrap();
- let mut current_size = 0;
- let mut data = Vec::with_capacity(total_size as usize);
- let mut stream = res.bytes_stream();
- let mut skip_print = 0;
+ let maybe_bytes = {
let progress_bar = ProgressBar::new(ProgressBarStyle::DownloadBars);
+ // provide an empty string here in order to prefer the downloading
+ // text above which will stay alive after the progress bars are complete
let progress = progress_bar.update("");
- progress.set_total_size(total_size);
- while let Some(item) = stream.next().await {
- let bytes = item?;
- current_size += bytes.len() as u64;
- data.extend_from_slice(&bytes);
- if progress_bar.is_enabled() {
- progress.set_position(current_size);
- } else if skip_print == 0 {
- log::info!(
- "{} / {} ({:^5.1}%)",
- human_download_size(current_size, total_size),
- human_download_size(total_size, total_size),
- (current_size as f64 / total_size as f64) * 100.0,
- );
- skip_print = 10;
- } else {
- skip_print -= 1;
- }
+ client
+ .download_with_progress(download_url, &progress)
+ .await?
+ };
+ match maybe_bytes {
+ Some(bytes) => Ok(bytes),
+ None => {
+ log::info!("Download could not be found, aborting");
+ std::process::exit(1)
}
- drop(progress);
- log::info!(
- "{} / {} (100.0%)",
- human_download_size(current_size, total_size),
- human_download_size(total_size, total_size)
- );
-
- Ok(data)
- } else {
- log::info!("Download could not be found, aborting");
- std::process::exit(1)
}
}