Skip to content

Commit

Permalink
Merge pull request #11 from 0xPolygonHermez/edu/remove_example
Browse files Browse the repository at this point in the history
fix get_toolchain_download_url
  • Loading branch information
eduadiez authored Jul 19, 2024
2 parents 0e342d6 + cf703d6 commit 7db7586
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ pub mod commands;
use anyhow::{Context, Result};
use futures::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
use reqwest::header::HeaderMap;
use reqwest::header::HeaderValue;
use reqwest::Client;
use std::process::{Command, Stdio};
use std::{cmp::min, fs::File, io::Write};
use reqwest::{
header::{HeaderMap, HeaderValue},
Client,
};
use std::{
cmp::min,
fs::File,
io::Write,
process::{Command, Stdio},
};

pub const RUSTUP_TOOLCHAIN_NAME: &str = "zisk";

pub const ZISK_VERSION_MESSAGE: &str = concat!(
"zisk",
" (",
env!("VERGEN_GIT_SHA"),
" ",
env!("VERGEN_BUILD_TIMESTAMP"),
")"
);
pub const ZISK_VERSION_MESSAGE: &str =
concat!("zisk", " (", env!("VERGEN_GIT_SHA"), " ", env!("VERGEN_BUILD_TIMESTAMP"), ")");

trait CommandExecutor {
fn run(&mut self) -> Result<()>;
Expand Down Expand Up @@ -60,27 +59,25 @@ pub fn is_supported_target() -> bool {
false
}

pub async fn get_toolchain_download_url(client: &Client, _target: String) -> String {
pub async fn get_toolchain_download_url(client: &Client, target: String) -> String {
// Get latest tag from https://api.github.com/repos/0xPolygonHermez/rust/releases/latest
// and use it to construct the download URL.
let url = format!(
"https://{}@api.github.com/repos/0xPolygonHermez/rust/releases/latest",
std::env::var("GITHUB_ACCESS_TOKEN").unwrap()
);
let json = client
.get(url)
.send()
.await
.unwrap()
.json::<serde_json::Value>()
.await
.unwrap();
let json = client.get(url).send().await.unwrap().json::<serde_json::Value>().await.unwrap();

let name: String = format!("rust-toolchain-{}.tar.gz", target);
if let Some(assets) = json["assets"].as_array() {
// Iterate over the array and extract the desired URL
for asset in assets {
if let Some(url) = asset["url"].as_str() {
return url.to_string();
if let Some(asset_name) = asset["name"].as_str() {
if asset_name == name {
if let Some(url) = asset["url"].as_str() {
return url.to_string();
}
}
}
}
}
Expand All @@ -98,21 +95,18 @@ pub async fn download_file(
"Authorization",
HeaderValue::from_str(
format!("Bearer {}", std::env::var("GITHUB_ACCESS_TOKEN").unwrap()).as_str(),
).unwrap(),
);
headers.insert(
"Accept",
HeaderValue::from_static("application/octet-stream"),
)
.unwrap(),
);
headers.insert("Accept", HeaderValue::from_static("application/octet-stream"));
let res = client
.get(url)
.headers(headers)
.send()
.await
.or(Err(format!("Failed to GET from '{}'", &url)))?;
let total_size = res
.content_length()
.ok_or(format!("Failed to get content length from '{}'", &url))?;
let total_size =
res.content_length().ok_or(format!("Failed to get content length from '{}'", &url))?;

let pb = ProgressBar::new(total_size);
pb.set_style(ProgressStyle::default_bar()
Expand All @@ -125,8 +119,7 @@ pub async fn download_file(

while let Some(item) = stream.next().await {
let chunk = item.or(Err("Error while downloading file"))?;
file.write_all(&chunk)
.or(Err("Error while writing to file"))?;
file.write_all(&chunk).or(Err("Error while writing to file"))?;
let new = min(downloaded + (chunk.len() as u64), total_size);
downloaded = new;
pb.set_position(new);
Expand Down

0 comments on commit 7db7586

Please sign in to comment.