Skip to content

Commit 0ee52dc

Browse files
committed
Add downloading cli progress output
1 parent b4eba9a commit 0ee52dc

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

crates/aspect-launcher/src/main.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::env;
55
use std::env::var;
66
use std::fs;
77
use std::fs::File;
8+
use std::io::{self, Write};
89
use std::os::unix::fs::PermissionsExt;
910
use std::os::unix::process::CommandExt;
1011
use std::path::PathBuf;
@@ -36,7 +37,7 @@ fn debug_mode() -> bool {
3637
}
3738
}
3839

39-
async fn _download_into_cache(client: &Client, cache_entry: &PathBuf, req: Request) -> Result<()> {
40+
async fn _download_into_cache(client: &Client, cache_entry: &PathBuf, req: Request, download_msg: &str) -> Result<()> {
4041
// Stream to a tempfile
4142
let tmp_file = cache_entry.with_extension("tmp");
4243
let tmpf = File::create(&tmp_file)
@@ -50,26 +51,46 @@ async fn _download_into_cache(client: &Client, cache_entry: &PathBuf, req: Reque
5051
fs::set_permissions(&tmp_file, permissions).into_diagnostic()?;
5152

5253
let mut tmp_writer = tokio::fs::File::from(tmpf);
53-
let mut byte_stream = client
54+
let response = client
5455
.execute(req)
5556
.await
5657
.into_diagnostic()?
5758
.error_for_status()
58-
.into_diagnostic()?
59-
.bytes_stream();
59+
.into_diagnostic()?;
60+
61+
eprintln!("{}", download_msg);
62+
63+
let total_size = response.content_length();
64+
let mut byte_stream = response.bytes_stream();
65+
66+
let mut downloaded: u64 = 0;
6067

6168
while let Some(item) = byte_stream
6269
.try_next()
6370
.await
6471
.into_diagnostic()
6572
.wrap_err("failed to stream content")?
6673
{
74+
let chunk_size = item.len() as u64;
6775
tokio::io::copy(&mut item.as_ref(), &mut tmp_writer)
6876
.await
6977
.into_diagnostic()
7078
.wrap_err("failed to slab stream to file")?;
79+
80+
downloaded += chunk_size;
81+
82+
if let Some(total) = total_size {
83+
let percent = ((downloaded as f64 / total as f64) * 100.0) as u64;
84+
eprint!("\r{:.0} / {:.0} KB ({}%)", downloaded as f64 / 1024.0, total as f64 / 1024.0, percent);
85+
io::stderr().flush().into_diagnostic()?;
86+
} else {
87+
eprint!("\r{:.0} KB", downloaded as f64 / 1024.0);
88+
io::stderr().flush().into_diagnostic()?;
89+
}
7190
}
7291

92+
eprintln!();
93+
7394
// And move it into the cache
7495
tokio::fs::rename(&tmp_file, &cache_entry)
7596
.await
@@ -194,7 +215,8 @@ async fn configure_tool_task(
194215
tool_dest_file
195216
);
196217
};
197-
if let err @ Err(_) = _download_into_cache(&client, &tool_dest_file, req).await
218+
let download_msg = format!("downloading aspect cli from {}", url);
219+
if let err @ Err(_) = _download_into_cache(&client, &tool_dest_file, req, &download_msg).await
198220
{
199221
errs.push(err);
200222
continue;
@@ -248,8 +270,8 @@ async fn configure_tool_task(
248270
.execute(req.try_clone().unwrap())
249271
.await
250272
.into_diagnostic()?;
251-
let release: Release = resp.json::<Release>().await.into_diagnostic()?;
252-
for asset in release.assets {
273+
let release_data: Release = resp.json::<Release>().await.into_diagnostic()?;
274+
for asset in release_data.assets {
253275
if asset.name == *artifact {
254276
if debug_mode() {
255277
eprintln!(
@@ -267,8 +289,9 @@ async fn configure_tool_task(
267289
)
268290
.build()
269291
.into_diagnostic()?;
292+
let download_msg = format!("downloading aspect cli version {} file {}", release, artifact);
270293
if let err @ Err(_) =
271-
_download_into_cache(&client, &tool_dest_file, req).await
294+
_download_into_cache(&client, &tool_dest_file, req, &download_msg).await
272295
{
273296
errs.push(err);
274297
break;
@@ -407,4 +430,4 @@ fn main() -> Result<ExitCode> {
407430
Ok(ExitCode::SUCCESS)
408431
}
409432
}
410-
}
433+
}

0 commit comments

Comments
 (0)