Skip to content

Commit

Permalink
Added download command + bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Sep 28, 2023
1 parent eb3f2ac commit efbf25d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 19 deletions.
71 changes: 71 additions & 0 deletions src/commands/download.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,76 @@
pub async fn download(
remote_client: vercel_cache_helper::vercel::remote_cache_client::RemoteClient,
) -> vercel_cache_helper::Result<()> {
let cache_dir = if let Some(cache_dir) = vercel_cache_helper::utils::get_cache_dir() {
println!("Cache dir found: {:?}", cache_dir);
cache_dir
} else {
return Ok(());
};

let current_dir = std::env::current_dir()?;
let cache_key_path = current_dir.join(".cache_key");
let build_dir = current_dir.join(".build");

if !build_dir.exists() {
std::fs::create_dir(&build_dir).expect("Failed to create .build dir.");
}

let cache_key_file =
std::fs::read_to_string(cache_key_path).expect(".cache_key file not found");

let hash_keys = cache_key_file.split_once("\n").unwrap();

let (cache_dir_hash, build_archive_hash) = hash_keys;

println!("Build archive hash: {:?}", build_archive_hash);
println!("Cache dir hash: {:?}", cache_dir_hash);

println!("Looking for artifacts...");

let mut build_exists_req = remote_client.exists(build_archive_hash.to_string(), None)?;

let build_artifact_exists = build_exists_req.send().await?;

if build_artifact_exists {
println!(".build artifact found");
} else {
println!(".build artifact not found");
return Ok(());
}

let mut cache_exists_req = remote_client.exists(cache_dir_hash.to_string(), None)?;

let cache_artifact_exists = cache_exists_req.send().await?;

if cache_artifact_exists {
println!("cache artifact found");
} else {
println!("cache artifact not found");
return Ok(());
}

println!("Downloading .build artifact");

let mut build_get_req = remote_client.get(build_archive_hash.to_string(), None)?;

let build_get_res = build_get_req.get().await?;

println!("Downloaded .build artifact");

vercel_cache_helper::utils::extract_tar_gz(&build_get_res.bytes().await?.to_vec(), &build_dir)?;

println!("Downloading cache artifact");

let mut cache_get_req = remote_client.get(build_archive_hash.to_string(), None)?;

let cache_get_res = cache_get_req.get().await?;

println!("Downloaded cache artifact");

vercel_cache_helper::utils::extract_tar_gz(&cache_get_res.bytes().await?.to_vec(), &cache_dir)?;

println!("done!");

Ok(())
}
23 changes: 13 additions & 10 deletions src/commands/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::io::{Read, Seek};
pub async fn upload(
remote_client: vercel_cache_helper::vercel::remote_cache_client::RemoteClient,
) -> vercel_cache_helper::Result<()> {
println!("Here");
let cache_dir = if let Some(cache_dir) = vercel_cache_helper::utils::get_cache_dir() {
println!("Cache dir found: {:?}", cache_dir);
cache_dir
Expand All @@ -13,6 +12,7 @@ pub async fn upload(
};

let current_dir = std::env::current_dir()?;
let cache_key_path = current_dir.join(".cache_key");
let build_dir = current_dir.join(".build");

if !build_dir.exists() {
Expand All @@ -32,8 +32,6 @@ pub async fn upload(
let mut build_archive_buf: Vec<u8> = Vec::new();
let mut cache_archive_buf: Vec<u8> = Vec::new();

println!("{:?}", build_dir_archive.metadata());

let build_archive_size = build_dir_archive.read_to_end(&mut build_archive_buf)?;
let cache_archive_size = cache_dir_archive.read_to_end(&mut cache_archive_buf)?;

Expand All @@ -46,20 +44,25 @@ pub async fn upload(
println!("Build archive hash: {:?}", build_archive_hash);
println!("Cache dir hash: {:?}", cache_dir_hash);

let mut build_put_req = remote_client.put(build_archive_hash, None)?;
println!("Uploading build");
let mut build_put_req = remote_client.put(build_archive_hash.clone(), None)?;

println!("Uploading .build");

build_put_req
.buffer(&mut build_archive_buf, build_archive_size)
.await?;

let mut cache_put_req = remote_client.put(cache_dir_hash, None)?;
println!("Uploading Cache");
let res = cache_put_req
let mut cache_put_req = remote_client.put(cache_dir_hash.clone(), None)?;

println!("Uploading .cache");

cache_put_req
.buffer(&mut cache_archive_buf, cache_archive_size)
.await?;

println!("Response: {:?}", res);
println!("Response body: {:?}", res.text().await?);
let cache_key_content = vec![cache_dir_hash, build_archive_hash].join("\n");

std::fs::write(cache_key_path, cache_key_content)?;

println!("done!");

Expand Down
24 changes: 19 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub fn get_cache_dir() -> Option<std::path::PathBuf> {
let cache_dir = dirs::cache_dir()?;
let base_path = cache_dir.join("fastn.com");

println!("{}", base_path.to_string_lossy());

if !base_path.exists() {
eprintln!("Cache dir not found");
return None;
if let Err(err) = std::fs::create_dir_all(&base_path) {
eprintln!("Failed to create cache directory: {}", err);
return None;
}
}

Some(
Expand Down Expand Up @@ -61,8 +61,22 @@ pub fn create_tar_gz_archive(
));
let mut tar_builder = tar::Builder::new(gz_encoder);

tar_builder.append_dir_all("", src_folder)?;
tar_builder
.append_dir_all("", src_folder)
.map_err(|e| {
println!("Error creating archive: {:?}", e);
e
})?;

println!("Archive created successfully.");
Ok(())
}

pub fn extract_tar_gz(buffer: &[u8], dest_path: &std::path::Path) -> vercel_cache_helper::Result<()> {
println!("Preparing to extract archive...");
let tar = flate2::read::GzDecoder::new(buffer);
let mut archive = tar::Archive::new(tar);
archive.unpack(dest_path)?;
println!("Unpacked archive in: {}", &dest_path.to_string_lossy());
Ok(())
}
4 changes: 0 additions & 4 deletions src/vercel/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,8 @@ impl ArtifactPutRequest {
) -> vercel_cache_helper::Result<reqwest::Response> {
let client = reqwest::Client::new();

println!("{:?}", &artifact);

let headers = self.0.get_headers("PUT", Some(content_len));

println!("Headers: {:?}", headers);

let response = client
.put(&self.0.url)
.headers(headers)
Expand Down

0 comments on commit efbf25d

Please sign in to comment.