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,11 +72,15 @@ impl<'a> Step<'a> {
pub async fn invoke(&self, ctx: &Context) -> Result<()> {
match self {
Self::DownloadFile { res, file } => {
if file.exists() {
println!("File {file} already downloaded.");
} else {
println!(
"Downloading file {file} from {res:?}...",
file = file.to_str().unwrap_or("<NON UTF-8>")
);
const FETCH_FILE_ERROR_MSG: &'static str = "Fetching the remote resource failed.";
const FETCH_FILE_ERROR_MSG: &'static str =
"Fetching the remote resource failed.";
const WRITE_FILE_ERROR_MSG: &'static str =
"Writing the remote resource to disk failed.";
let url = match res {
@ -87,7 +91,9 @@ impl<'a> Step<'a> {
let mut release = fetch_latest_release(&ctx.reqwest, repo).await?;
let pattern = ramhorns::Template::new(*pattern)
.into_diagnostic()
.wrap_err("Invalid pattern for artifact matching in download step.")?;
.wrap_err(
"Invalid pattern for artifact matching in download step.",
)?;
release.meta.tag_name_strip_prefix = release
.meta
.tag_name
@ -111,11 +117,9 @@ impl<'a> Step<'a> {
.into_diagnostic()
.wrap_err(FETCH_FILE_ERROR_MSG)?;
let _content_length = resp.content_length();
mkdir_all(
file.parent().ok_or_else(|| {
mkdir_all(file.parent().ok_or_else(|| {
miette!("Destination file for download step has no parent.")
})?,
)
})?)
.await?;
let mut writer = tokio::io::BufWriter::new(
tokio::fs::File::create(file.as_os_str())
@ -141,6 +145,7 @@ impl<'a> Step<'a> {
.into_diagnostic()
.wrap_err(WRITE_FILE_ERROR_MSG)?;
}
}
Self::ExtractFile { file, dest } => {
const EXTRACT_FILE_ERROR_MSG: &'static str = "Extracting file failed.";
mkdir_all(&dest).await.wrap_err(EXTRACT_FILE_ERROR_MSG)?;