Skip to content

Commit

Permalink
admin/upload_index: Use indicatif for progress reporting (#4836)
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed May 31, 2022
1 parent cca976d commit 64feb48
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ hex = "=0.4.3"
http = "=0.2.7"
hyper = { version = "=0.14.19", features = ["client", "http1"] }
indexmap = { version = "=1.8.2", features = ["serde-1"] }
indicatif = "=0.16.2"
ipnetwork = "=0.19.0"
tikv-jemallocator = { version = "=0.5.0", features = ['unprefixed_malloc_on_supported_platforms', 'profiling'] }
lettre = { version = "=0.10.0-rc.6", default-features = false, features = ["file-transport", "smtp-transport", "native-tls", "hostname", "builder"] }
Expand Down
18 changes: 6 additions & 12 deletions src/admin/upload_index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::time::{Duration, Instant};

use crate::admin::dialoguer;
use cargo_registry_index::{Repository, RepositoryConfig};
use indicatif::{ProgressBar, ProgressIterator, ProgressStyle};
use reqwest::blocking::Client;

use crate::config;
Expand Down Expand Up @@ -33,23 +32,18 @@ pub fn run(opts: Opts) -> anyhow::Result<()> {
return Ok(());
}

let mut progress_update_time = Instant::now();
for (i, file) in files.iter().enumerate() {
let pb = ProgressBar::new(files.len() as u64);
pb.set_style(ProgressStyle::default_bar().template("{bar:60} ({pos}/{len}, ETA {eta})"));

for file in files.iter().progress_with(pb) {
let crate_name = file.file_name().unwrap().to_str().unwrap();
let path = repo.index_file(crate_name);
if !path.exists() {
println!("skipping file `{}`", crate_name);
continue;
}

let contents = std::fs::read_to_string(&path)?;
uploader.upload_index(&client, crate_name, contents)?;

// Print a progress update every 10 seconds.
let now = Instant::now();
if now - progress_update_time > Duration::from_secs(10) {
progress_update_time = now;
println!("uploading {}/{}", i, files.len());
}
}

println!(
Expand Down

0 comments on commit 64feb48

Please sign in to comment.