Skip to content

Commit

Permalink
compress, hash and upload artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Sep 27, 2023
1 parent 371f079 commit 95099c0
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 42 deletions.
208 changes: 206 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ clap = { version = "4.4.5", features = ["derive"] }
reqwest = { version = "0.11.20", features = ["json"] }
tokio = { version = "1.32.0", features = ["full"] }
atty = "0.2"
dirs = "5.0.1"
sha2 = "0.10.8"
tar = "0.4.40"
tempfile = "3.8.0"

[dependencies.flate2]
git = "https://github.com/alexcrichton/flate2-rs"
4 changes: 3 additions & 1 deletion src/commands/download.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub fn download() -> vercel_cache_helper::Result<()> {
pub async fn download(
remote_client: vercel_cache_helper::vercel::remote_cache_client::RemoteClient
) -> vercel_cache_helper::Result<()> {
Ok(())
}
3 changes: 2 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod download;
pub mod download;
pub mod upload;
58 changes: 57 additions & 1 deletion src/commands/upload.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
pub fn upload() -> vercel_cache_helper::Result<()> {
use std::io::Read;

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
} else {
println!("Cache dir not found");
return Ok(());
};

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

if !build_dir.exists() {
println!("Build dir does not exist: {:?}", build_dir);
return Ok(());
}

let mut build_dir_archive = tempfile::tempfile()?;
let mut cache_dir_archive = tempfile::tempfile()?;

vercel_cache_helper::utils::create_tar_gz_archive(&cache_dir, &cache_dir_archive)?;
vercel_cache_helper::utils::create_tar_gz_archive(&build_dir, &build_dir_archive)?;

let mut build_archive_buf: Vec<u8> = Vec::new();
let mut cache_archive_buf: Vec<u8> = Vec::new();

build_dir_archive.read_to_end(&mut build_archive_buf)?;
cache_dir_archive.read_to_end(&mut cache_archive_buf)?;

let build_archive_size = build_dir_archive.metadata()?.len();
let cache_archive_size = cache_dir_archive.metadata()?.len();

println!("Build archive size: {} bytes", build_archive_size);
println!("Cache archive size: {} bytes", cache_archive_size);

let build_archive_hash = vercel_cache_helper::utils::generate_hash(&build_archive_buf);
let cache_dir_hash = vercel_cache_helper::utils::generate_hash(&cache_archive_buf);

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)?;
build_put_req.buffer(&mut build_archive_buf, build_archive_size).await?;

let mut cache_put_req = remote_client.put(cache_dir_hash, None)?;
let res = cache_put_req.buffer(&mut cache_archive_buf, cache_archive_size).await?;

println!("Response: {:?}", res);
println!("Response body: {:?}", res.text().await?);

println!("done!");

Ok(())
}
Loading

0 comments on commit 95099c0

Please sign in to comment.