diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cli.rs | 6 | ||||
-rw-r--r-- | src/commands/source.rs | 90 | ||||
-rw-r--r-- | src/commands/util.rs | 5 |
3 files changed, 63 insertions, 38 deletions
@@ -28,7 +28,7 @@ pub fn cli<'a>() -> App<'a> { .about("Generic Build Orchestration System for building linux packages with docker") .after_help(r#" - The folowing environment variables can be passed to butido: + The following environment variables can be passed to butido: RUST_LOG - to enable logging, for exact usage see the rust cookbook "#) @@ -732,14 +732,14 @@ pub fn cli<'a>() -> App<'a> { .multiple(false) .index(1) .value_name("PKG") - .about("Verify the sources of this package (optional, if left out, all packages are checked)") + .about("Download the sources of this package (optional, if left out, all packages are downloaded)") ) .arg(Arg::new("package_version") .required(false) .multiple(false) .index(2) .value_name("VERSION_CONSTRAINT") - .about("Verify the sources of this package version (optional, if left out, all packages are checked)") + .about("Download the sources of this package version (optional, if left out, all packages are downloaded)") ) .arg(Arg::new("force") .required(false) diff --git a/src/commands/source.rs b/src/commands/source.rs index 0fc1eac..cd33921 100644 --- a/src/commands/source.rs +++ b/src/commands/source.rs @@ -260,55 +260,75 @@ pub async fn download( if source_path_exists && !force { Err(anyhow!("Source exists: {}", source.path().display())) } else { - if source_path_exists { - let _ = source.remove_file().await?; - } + async fn perform_download(source: &SourceEntry, bar: &indicatif::ProgressBar) -> Result<()> { + trace!("Creating: {:?}", source); + let file = source.create().await.with_context(|| { + anyhow!( + "Creating source file destination: {}", + source.path().display() + ) + })?; + + let mut file = tokio::io::BufWriter::new(file); + let response = match reqwest::get(source.url().as_ref()).await { + Ok(resp) => resp, + Err(e) => { + bar.finish_with_message(format!("Failed: {}", source.url())); + return Err(e).with_context(|| anyhow!("Downloading '{}'", source.url())) + } + }; - trace!("Creating: {:?}", source); - let file = source.create().await.with_context(|| { - anyhow!( - "Creating source file destination: {}", - source.path().display() - ) - })?; - - let mut file = tokio::io::BufWriter::new(file); - let response = reqwest::get(source.url().as_ref()) - .await - .with_context(|| anyhow!("Downloading '{}'", source.url()))?; - - if let Some(len) = response.content_length() { - bar.set_length(len); - } - let mut stream = reqwest::get(source.url().as_ref()).await?.bytes_stream(); - let mut bytes_written = 0; - while let Some(bytes) = stream.next().await { - let bytes = bytes?; - file.write_all(bytes.as_ref()).await?; - bytes_written += bytes.len(); - - bar.inc(bytes.len() as u64); if let Some(len) = response.content_length() { - bar.set_message(format!("Downloading {} ({}/{} bytes)", source.url(), bytes_written, len)); - } else { - bar.set_message(format!("Downloading {} ({} bytes)", source.url(), bytes_written)); + bar.set_length(len); + } + + let mut stream = reqwest::get(source.url().as_ref()).await?.bytes_stream(); + let mut bytes_written = 0; + while let Some(bytes) = stream.next().await { + let bytes = bytes?; + file.write_all(bytes.as_ref()).await?; + bytes_written += bytes.len(); + + bar.inc(bytes.len() as u64); + if let Some(len) = response.content_length() { + bar.set_message(format!("Downloading {} ({}/{} bytes)", source.url(), bytes_written, len)); + } else { + bar.set_message(format!("Downloading {} ({} bytes)", source.url(), bytes_written)); + } + } + + file.flush() + .await + .map_err(Error::from) + .map(|_| ()) + } + + if source_path_exists /* && force is implied by 'if' above*/ { + if let Err(e) = source.remove_file().await { + bar.finish_with_message(format!("Failed to remove existing file: {}", source.path().display())); + return Err(e) } } - file.flush().await?; - bar.finish_with_message("Finished download"); - Ok(()) + + if let Err(e) = perform_download(&source, &bar).await { + bar.finish_with_message(format!("Failed: {}", source.url())); + Err(e) + } else { + bar.finish_with_message(format!("Finished: {}", source.url())); + Ok(()) + } } } }) }) .flatten() .collect::<futures::stream::FuturesUnordered<_>>() - .collect::<Result<()>>(); + .collect::<Vec<Result<()>>>(); let multibar_block = tokio::task::spawn_blocking(move || multi.join()); let (r, _) = tokio::join!(r, multibar_block); - r + r.into_iter().collect() } async fn of( diff --git a/src/commands/util.rs b/src/commands/util.rs index 9ed38d9..e892875 100644 --- a/src/commands/util.rs +++ b/src/commands/util.rs @@ -47,6 +47,11 @@ where I: Iterator<Item = &'a Package> + 'a, { let shebang = Shebang::from(config.shebang().clone()); + bar.set_length({ + let (lower, upper) = iter.size_hint(); + upper.unwrap_or(lower) as u64 + }); + let lint_results = iter .map(|pkg| { let shebang = shebang.clone(); |