-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support display download progress bar #525
Changes from 4 commits
36650a5
f71773d
08a84d6
015e906
9579586
2e7deef
ad2f52c
4851224
9c14ed7
f27c342
1007345
d1a3e27
b52de55
e5a7c25
6019191
c6cf7ab
70ca89a
d470928
46cb028
a6d9edf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,7 +171,7 @@ pub fn download(url: &str) -> Result<Vec<u8>> { | |
match handle.get(url).call() { | ||
Ok(response) => { | ||
let mut data = Vec::new(); | ||
write_response_with_progress_bar(response, &mut data)?; | ||
write_response_with_progress_bar(response, &mut data, String::new())?; | ||
return Ok(data); | ||
} | ||
Err(ureq::Error::Status(404, r)) => { | ||
|
@@ -203,7 +203,11 @@ pub fn download_file(url: &str, path: &PathBuf) -> Result<()> { | |
for _ in 1..RETRY_ATTEMPTS { | ||
match handle.get(url).call() { | ||
Ok(response) => { | ||
if let Err(e) = write_response_with_progress_bar(response, &mut file) { | ||
if let Err(e) = write_response_with_progress_bar( | ||
response, | ||
&mut file, | ||
path.display().to_string(), | ||
) { | ||
fs::remove_file(path)?; | ||
return Err(e); | ||
} | ||
|
@@ -216,8 +220,6 @@ pub fn download_file(url: &str, path: &PathBuf) -> Result<()> { | |
let retry = retry.unwrap_or(RETRY_DELAY_SECS); | ||
info!("Retrying.."); | ||
thread::sleep(Duration::from_secs(retry)); | ||
// clean file content every retry | ||
file = OpenOptions::new().write(true).truncate(true).open(path)?; | ||
} | ||
Err(e) => { | ||
fs::remove_file(path)?; | ||
|
@@ -286,9 +288,10 @@ pub fn unpack_bins(dir: &Path, dst_dir: &Path) -> Result<Vec<PathBuf>> { | |
} | ||
|
||
/// write Ok(Response) to provided writer with progress bar displaying writing status | ||
pub fn write_response_with_progress_bar<W: Write>( | ||
fn write_response_with_progress_bar<W: Write>( | ||
response: Response, | ||
writer: &mut W, | ||
target: String, | ||
) -> Result<()> { | ||
let total_size = response | ||
.header("Content-Length") | ||
|
@@ -299,7 +302,7 @@ pub fn write_response_with_progress_bar<W: Write>( | |
let progress_bar = ProgressBar::new(total_size); | ||
progress_bar.set_style( | ||
ProgressStyle::default_bar() | ||
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} {bytes_per_sec} ({eta}) - {msg:.cyan}") | ||
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta}) - {msg:.green}") | ||
.unwrap() | ||
.progress_chars("##-"), | ||
); | ||
|
@@ -311,35 +314,30 @@ pub fn write_response_with_progress_bar<W: Write>( | |
break; | ||
} | ||
if let Err(e) = writer.write_all(&buffer[..bytes_read]) { | ||
debug!( | ||
"[{}] [{}] {}/{} {}/s ({}) - {}", | ||
FormattedDuration(progress_bar.elapsed()), | ||
"#".repeat( | ||
(progress_bar.position() * 40 / progress_bar.length().unwrap()) as usize | ||
), | ||
HumanBytes(progress_bar.position()), | ||
HumanBytes(progress_bar.length().unwrap_or(progress_bar.position())), | ||
HumanBytes(progress_bar.per_sec() as u64), | ||
HumanDuration(progress_bar.eta()), | ||
progress_bar.message(), | ||
); | ||
error!("Something went wrong writing data: {}", e) | ||
log_progress_bar(&progress_bar); | ||
if target.is_empty() { | ||
bail!("Something went wrong writing data: {}", e) | ||
} | ||
bail!("Something went wrong writing data to {}: {}", target, e) | ||
}; | ||
downloaded_size += bytes_read as u64; | ||
progress_bar.set_position(downloaded_size); | ||
} | ||
progress_bar.finish_with_message("Download complete"); | ||
log_progress_bar(&progress_bar); | ||
Ok(()) | ||
} | ||
|
||
fn log_progress_bar(progress_bar: &ProgressBar) { | ||
debug!( | ||
"[{}] [{}] {}/{} {}/s ({}) - {}", | ||
"[{}] [{}] {}/{} ({}) - {}", | ||
FormattedDuration(progress_bar.elapsed()), | ||
"#".repeat(40), | ||
"#".repeat((progress_bar.position() * 40 / progress_bar.length().unwrap()) as usize), | ||
HumanBytes(progress_bar.position()), | ||
HumanBytes(progress_bar.length().unwrap_or(progress_bar.position())), | ||
HumanBytes(progress_bar.per_sec() as u64), | ||
HumanDuration(progress_bar.eta()), | ||
progress_bar.message(), | ||
); | ||
Ok(()) | ||
} | ||
|
||
/// Read the version (as a plain String) used by the `fuels` dependency, if it exists. | ||
|
@@ -402,8 +400,31 @@ pub fn fetch_fuels_version(cfg: &DownloadCfg) -> Result<String> { | |
mod tests { | ||
use super::*; | ||
use dirs::home_dir; | ||
use std::io::{self, Result}; | ||
use tempfile; | ||
|
||
struct MockWriter; | ||
|
||
impl Write for MockWriter { | ||
fn write_all(&mut self, _: &[u8]) -> Result<()> { | ||
Err(io::Error::new( | ||
io::ErrorKind::Interrupted, | ||
"Mock Interrupted Error", | ||
)) | ||
} | ||
|
||
fn write(&mut self, _: &[u8]) -> Result<usize> { | ||
Err(io::Error::new( | ||
io::ErrorKind::Interrupted, | ||
"Mock Interrupted Error", | ||
)) | ||
} | ||
|
||
fn flush(&mut self) -> Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub(crate) fn with_toolchain_dir<F>(f: F) -> Result<()> | ||
where | ||
F: FnOnce(tempfile::TempDir) -> Result<()>, | ||
|
@@ -469,7 +490,7 @@ fuels = { version = "0.1", features = ["some-feature"] } | |
} | ||
|
||
#[test] | ||
fn test_write_response_with_progress_bar() -> Result<()> { | ||
fn test_write_response_with_progress_bar() -> anyhow::Result<()> { | ||
let mut data = Vec::new(); | ||
let len = 100; | ||
let body = "A".repeat(len); | ||
|
@@ -480,9 +501,26 @@ fuels = { version = "0.1", features = ["some-feature"] } | |
body, | ||
); | ||
let res = s.parse::<Response>().unwrap(); | ||
assert!(write_response_with_progress_bar(res, &mut data).is_ok()); | ||
assert!(write_response_with_progress_bar(res, &mut data, String::new()).is_ok()); | ||
let written_res = String::from_utf8(data)?; | ||
assert!(written_res.trim().eq(&body)); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_write_response_with_progress_bar_throw_error() { | ||
let mut mock_writer = MockWriter; | ||
let len = 9000; | ||
let body = "A".repeat(len); | ||
let s = format!( | ||
"HTTP/1.1 200 OK\r\n\ | ||
Content-Length: {}\r\n | ||
\r\n | ||
{}", | ||
len, body, | ||
); | ||
let res = s.parse::<Response>().unwrap(); | ||
assert!(write_response_with_progress_bar(res, &mut mock_writer, String::new()).is_ok()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe that we need to assert or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. If we are able to catch the exact error and assert that the specific error matches what we expect in the test then we should do that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Done |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this shows that the download completed even if there was an error. Could you add a unit test where it fails?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tested in local, when download encount error, won't show "download completed"