use reqwest::Client;
use serde_derive::{Deserialize, Serialize};
pub mod env;
pub mod fs;
#[derive(Debug, Clone)]
pub enum Origin {
Github,
Gitee,
}
impl Origin {
pub fn get_pkg_url(&self) -> String {
match self {
Origin::Github => String::from("https://github.com/ngd-b"),
Origin::Gitee => String::from("https://gitee.com/hboot"),
}
}
}
const VERSION: &str = "latest";
pub fn get_pkg_url(origin: Option<Origin>) -> (String, String) {
let os = std::env::consts::OS;
let origin = origin.unwrap_or(Origin::Github);
let mut url = format!(
"{}/rsup/releases/download/{}",
origin.get_pkg_url(),
VERSION
);
let mut web_url = format!(
"{}/rsup-web/releases/download/{}",
origin.get_pkg_url(),
VERSION
);
let file_suffix = match os {
"windows" => "windows-latest",
"macos" => "macos-latest",
_ => "ubuntu-latest",
};
url = format!("{}/rsup-{}.tar.gz", url, file_suffix);
web_url = format!("{}/rsup-web.tar.gz", web_url);
(url, web_url)
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Version {
url: String,
tag_name: String,
name: String,
}
pub async fn get_version(
name: String,
owner: Option<String>,
) -> Result<String, Box<dyn std::error::Error>> {
let url = format!(
"https://api.github.com/repos/{}/{}/releases/latest",
owner.unwrap_or("ngd-b".to_string()),
name
);
let client = Client::new();
println!("will load repo latest version from {}", &url);
let res = client
.get(url)
.header("Accept", "application/vnd.github+json")
.header("User-Agent", "rsup_utils")
.send()
.await?;
if res.status().is_success() {
let data = res.text().await?;
let version: Version = serde_json::from_str(&data)?;
Ok(version.name)
} else {
let error_message = format!("Request failed with status code: {}", res.status());
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
error_message,
)))
}
}