Skip to content

Commit

Permalink
use the github archive url information
Browse files Browse the repository at this point in the history
use the github archive url information so we only download things it claims to have

removed the now unused BASE_URL constant
  • Loading branch information
ahornby committed Jun 24, 2023
1 parent 00bdb93 commit 2e718f3
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use url::Url;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

const BASE_URL: &str = "https://github.com/facebook/buck2/releases/download/";

fn get_buckle_dir() -> Result<PathBuf, Error> {
let mut dir = match env::var("BUCKLE_HOME") {
Ok(home) => Ok(PathBuf::from(home)),
Expand Down Expand Up @@ -54,6 +52,13 @@ fn find_project_root() -> Result<Option<PathBuf>, Error> {
Ok(None)
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct Asset {
pub name: String,
pub browser_download_url: Url,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct Release {
Expand All @@ -74,7 +79,7 @@ pub struct Release {
pub created_at: Option<String>,
pub published_at: Option<String>,
pub author: serde_json::Value,
pub assets: Vec<serde_json::Value>,
pub assets: Vec<Asset>,
}

fn get_releases(path: &Path) -> Result<Vec<Release>, Error> {
Expand Down Expand Up @@ -128,15 +133,33 @@ fn download_http(version: String, output_dir: &Path) -> Result<PathBuf, Error> {
let releases = get_releases(output_dir)?;
let mut path = output_dir.to_path_buf();

let mut artefact = None;
let arch = get_arch()?;

for release in releases {
if release.tag_name == "latest" {
path.push(release.target_commitish);
} else {
return Err(anyhow!(
"Meta has changed their releasing method. Please update buckle."
));
if release.name.as_ref() == Some(&version) {
if release.tag_name == "latest" {
path.push(release.target_commitish);
}
for asset in release.assets {
let name = asset.name;
let url = asset.browser_download_url;
if name == format!("buck2-{}.zst", arch) {
artefact = Some((name, url));
break;
}
}
}
}

let (_name, url) = if let Some((name, url)) = artefact {
(name, url)
} else {
return Err(anyhow!(
"Meta has changed their releasing method. Please update buckle."
));
};

path.push("buck2");
if path.exists() {
return Ok(path);
Expand All @@ -146,8 +169,7 @@ fn download_http(version: String, output_dir: &Path) -> Result<PathBuf, Error> {
}

let buck2_bin = File::create(&path)?;
let arch = get_arch()?;
let resp = reqwest::blocking::get(format!("{BASE_URL}/{version}/buck2-{arch}.zst"))?;
let resp = reqwest::blocking::get(url)?;
zstd::stream::copy_decode(resp, buck2_bin).unwrap();
let permissions = fs::Permissions::from_mode(0o755);
fs::set_permissions(&path, permissions)?;
Expand Down

0 comments on commit 2e718f3

Please sign in to comment.