|
3 | 3 | use anyhow::Result; |
4 | 4 | use postgresql_archive::configuration::{custom, theseus}; |
5 | 5 | use postgresql_archive::repository::github::repository::GitHub; |
6 | | -use postgresql_archive::{VersionReq, matcher}; |
| 6 | +use postgresql_archive::{VersionReq, matcher, ExactVersion}; |
7 | 7 | use postgresql_archive::{get_archive, repository}; |
8 | 8 | use std::fs::File; |
9 | 9 | use std::io::Write; |
10 | 10 | use std::path::PathBuf; |
11 | 11 | use std::str::FromStr; |
12 | 12 | use std::{env, fs}; |
| 13 | +use std::env::home_dir; |
13 | 14 | use url::Url; |
14 | 15 |
|
15 | 16 | /// Stage the PostgreSQL archive when the `bundled` feature is enabled so that |
@@ -53,7 +54,31 @@ pub(crate) async fn stage_postgresql_archive() -> Result<()> { |
53 | 54 | return Ok(()); |
54 | 55 | } |
55 | 56 |
|
56 | | - let (asset_version, archive) = get_archive(&releases_url, &version_req).await?; |
| 57 | + panic!(); |
| 58 | + |
| 59 | + |
| 60 | + let (asset_version, archive); |
| 61 | + |
| 62 | + // Only works when exact version is specified |
| 63 | + if let Some(exact_version) = version_req.exact_version() { |
| 64 | + let cached_file_name = home_dir().unwrap_or_else(|| env::current_dir().unwrap_or_default()).join(".theseus") |
| 65 | + .join("postgresql") |
| 66 | + .join(format!("postgresql-{}-{}-{}.tar.gz", ver, cfg!(target_os), cfg!(target_arch))); |
| 67 | + |
| 68 | + println!("Cached file name: {cached_file_name:?} - exists: {}", cached_file_name.exists()); |
| 69 | + if !cached_file_name.is_file() { |
| 70 | + (asset_version, archive) = get_archive(&releases_url, &version_req).await?; |
| 71 | + fs::create_dir_all(cached_file_name.parent().unwrap())?; |
| 72 | + fs::write(&cached_file_name, &archive)?; |
| 73 | + println!("Cached PostgreSQL archive to: {cached_file_name:?}"); |
| 74 | + } else { |
| 75 | + asset_version = exact_version; |
| 76 | + archive = fs::read(&cached_file_name)?; |
| 77 | + println!("Using cached PostgreSQL archive: {cached_file_name:?}"); |
| 78 | + } |
| 79 | + } else { |
| 80 | + (asset_version, archive) = get_archive(&releases_url, &version_req).await?; |
| 81 | + } |
57 | 82 |
|
58 | 83 | fs::write(archive_version_file.clone(), asset_version.to_string())?; |
59 | 84 | let mut file = File::create(archive_file.clone())?; |
|
0 commit comments