|
| 1 | +use crate::admin::dialoguer; |
| 2 | +use cargo_registry_index::{Repository, RepositoryConfig}; |
| 3 | +use reqwest::blocking::Client; |
| 4 | + |
| 5 | +use crate::config; |
| 6 | + |
| 7 | +#[derive(clap::Parser, Debug)] |
| 8 | +#[clap( |
| 9 | + name = "upload-index", |
| 10 | + about = "Upload index from git to S3 (http-based index)" |
| 11 | +)] |
| 12 | +pub struct Opts { |
| 13 | + /// Incremental commit. Any changed files made after this commit will be uploaded. |
| 14 | + incremental_commit: Option<String>, |
| 15 | +} |
| 16 | + |
| 17 | +pub fn run(opts: Opts) -> anyhow::Result<()> { |
| 18 | + let config = config::Base::from_environment(); |
| 19 | + let uploader = config.uploader(); |
| 20 | + let client = Client::new(); |
| 21 | + |
| 22 | + println!("fetching git repo"); |
| 23 | + let config = RepositoryConfig::from_environment(); |
| 24 | + let repo = Repository::open(&config)?; |
| 25 | + repo.reset_head()?; |
| 26 | + println!("HEAD is at {}", repo.head_oid()?); |
| 27 | + |
| 28 | + let files = repo.get_files_modified_since(opts.incremental_commit.as_deref())?; |
| 29 | + println!("found {} files to upload", files.len()); |
| 30 | + if !dialoguer::confirm("continue with upload?") { |
| 31 | + return Ok(()); |
| 32 | + } |
| 33 | + |
| 34 | + for file in files { |
| 35 | + let crate_name = file.file_name().unwrap().to_str().unwrap(); |
| 36 | + let path = repo.index_file(crate_name); |
| 37 | + if !path.exists() { |
| 38 | + println!("skipping file `{}`", crate_name); |
| 39 | + continue; |
| 40 | + } |
| 41 | + let contents = std::fs::read_to_string(&path)?; |
| 42 | + uploader.upload_index(&client, crate_name, contents)?; |
| 43 | + } |
| 44 | + |
| 45 | + println!( |
| 46 | + "uploading completed; use `upload-index {}` for an incremental run", |
| 47 | + repo.head_oid()? |
| 48 | + ); |
| 49 | + Ok(()) |
| 50 | +} |
0 commit comments