Don't download file if it already exists.

This commit is contained in:
Michael Pfaff 2022-06-10 13:23:07 -04:00
parent 12991439fa
commit ceeaed0b8d
Signed by: michael
GPG Key ID: CF402C4A012AA9D4
1 changed files with 65 additions and 60 deletions

View File

@ -72,74 +72,79 @@ impl<'a> Step<'a> {
pub async fn invoke(&self, ctx: &Context) -> Result<()> { pub async fn invoke(&self, ctx: &Context) -> Result<()> {
match self { match self {
Self::DownloadFile { res, file } => { Self::DownloadFile { res, file } => {
println!( if file.exists() {
"Downloading file {file} from {res:?}...", println!("File {file} already downloaded.");
file = file.to_str().unwrap_or("<NON UTF-8>") } else {
); println!(
const FETCH_FILE_ERROR_MSG: &'static str = "Fetching the remote resource failed."; "Downloading file {file} from {res:?}...",
const WRITE_FILE_ERROR_MSG: &'static str = file = file.to_str().unwrap_or("<NON UTF-8>")
"Writing the remote resource to disk failed."; );
let url = match res { const FETCH_FILE_ERROR_MSG: &'static str =
RemoteResource::Url(url) => url::Url::parse(url) "Fetching the remote resource failed.";
.into_diagnostic() const WRITE_FILE_ERROR_MSG: &'static str =
.wrap_err("Invalid url for download step.")?, "Writing the remote resource to disk failed.";
RemoteResource::GitHubArtifact { repo, pattern } => { let url = match res {
let mut release = fetch_latest_release(&ctx.reqwest, repo).await?; RemoteResource::Url(url) => url::Url::parse(url)
let pattern = ramhorns::Template::new(*pattern)
.into_diagnostic() .into_diagnostic()
.wrap_err("Invalid pattern for artifact matching in download step.")?; .wrap_err("Invalid url for download step.")?,
release.meta.tag_name_strip_prefix = release RemoteResource::GitHubArtifact { repo, pattern } => {
.meta let mut release = fetch_latest_release(&ctx.reqwest, repo).await?;
.tag_name let pattern = ramhorns::Template::new(*pattern)
.strip_prefix('v') .into_diagnostic()
.unwrap_or(&release.meta.tag_name); .wrap_err(
let asset_name = pattern.render(&release.meta); "Invalid pattern for artifact matching in download step.",
let artifact = release.assets.into_iter().filter(move |asset| asset.name == asset_name).next().ok_or_else(|| miette!("No artifact of the latest release matched the pattern in download step."))?; )?;
url::Url::parse(&artifact.browser_download_url) release.meta.tag_name_strip_prefix = release
.into_diagnostic() .meta
.wrap_err( .tag_name
"Invalid url returned by GitHub for latest release artifact.", .strip_prefix('v')
)? .unwrap_or(&release.meta.tag_name);
} let asset_name = pattern.render(&release.meta);
}; let artifact = release.assets.into_iter().filter(move |asset| asset.name == asset_name).next().ok_or_else(|| miette!("No artifact of the latest release matched the pattern in download step."))?;
let mut resp = ctx url::Url::parse(&artifact.browser_download_url)
.reqwest .into_diagnostic()
.get(url) .wrap_err(
.header("User-Agent", USER_AGENT_STR) "Invalid url returned by GitHub for latest release artifact.",
.send() )?
.await }
.into_diagnostic() };
.wrap_err(FETCH_FILE_ERROR_MSG)?; let mut resp = ctx
let _content_length = resp.content_length(); .reqwest
mkdir_all( .get(url)
file.parent().ok_or_else(|| { .header("User-Agent", USER_AGENT_STR)
miette!("Destination file for download step has no parent.") .send()
})?,
)
.await?;
let mut writer = tokio::io::BufWriter::new(
tokio::fs::File::create(file.as_os_str())
.await .await
.into_diagnostic() .into_diagnostic()
.wrap_err(WRITE_FILE_ERROR_MSG)?, .wrap_err(FETCH_FILE_ERROR_MSG)?;
); let _content_length = resp.content_length();
while let Some(mut chunk) = resp mkdir_all(file.parent().ok_or_else(|| {
.chunk() miette!("Destination file for download step has no parent.")
.await })?)
.into_diagnostic() .await?;
.wrap_err(FETCH_FILE_ERROR_MSG)? let mut writer = tokio::io::BufWriter::new(
{ tokio::fs::File::create(file.as_os_str())
.await
.into_diagnostic()
.wrap_err(WRITE_FILE_ERROR_MSG)?,
);
while let Some(mut chunk) = resp
.chunk()
.await
.into_diagnostic()
.wrap_err(FETCH_FILE_ERROR_MSG)?
{
writer
.write_all_buf(&mut chunk)
.await
.into_diagnostic()
.wrap_err(WRITE_FILE_ERROR_MSG)?;
}
writer writer
.write_all_buf(&mut chunk) .flush()
.await .await
.into_diagnostic() .into_diagnostic()
.wrap_err(WRITE_FILE_ERROR_MSG)?; .wrap_err(WRITE_FILE_ERROR_MSG)?;
} }
writer
.flush()
.await
.into_diagnostic()
.wrap_err(WRITE_FILE_ERROR_MSG)?;
} }
Self::ExtractFile { file, dest } => { Self::ExtractFile { file, dest } => {
const EXTRACT_FILE_ERROR_MSG: &'static str = "Extracting file failed."; const EXTRACT_FILE_ERROR_MSG: &'static str = "Extracting file failed.";