Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check for updates #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion egui/src/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod widgets;
pub mod styling;
pub mod styling;
pub mod update;
10 changes: 10 additions & 0 deletions updates/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "cirrus_updates"
version = "0.1.0"
edition = "2021"

[dependencies]
reqwest = {version = "0.12.12", features = ["blocking", "json"]}
semver = "1.0.24"

serde = {workspace = true, features = ["derive"]}
1 change: 1 addition & 0 deletions updates/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v1;
57 changes: 57 additions & 0 deletions updates/src/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use reqwest::{blocking, header::USER_AGENT};
use semver::{Version, VersionReq};

#[derive(serde::Deserialize)]
struct GitHubData {
tag_name: String,
}

pub fn get_latest_version(username: String, repository: String, local_version: String) -> Result<Option<Version>, String> {
let github_api = format!("https://api.github.com/repos/{}/{}/releases", username, repository);

let client = blocking::Client::new();

match client.get(github_api).header(USER_AGENT, "Reqwest").send() {
Ok(resp) => {
if !resp.status().is_success() {
return Err(
format!("GitHub API responded with: {:?}", resp.status())
)
}

let data: Vec<GitHubData> = match resp.json() {
Ok(data) => data,
Err(err) => {
return Err(
format!("Failed to deserialize data: {}", err)
);
}
};

match data.first() {
Some(newest_release) => {
let mut tag = newest_release.tag_name.clone();
if tag.starts_with("v") {
tag.remove(0);
}

let req_string = format!(">{}", local_version).to_string();
let req = VersionReq::parse(&req_string).unwrap();
let remote = Version::parse(&tag).unwrap();

if req.matches(&remote) {
return Ok(Some(remote))
} else {
return Ok(None)
}
}
None => return Err("No release found".to_string())
};
},
Err(err) => {
return Err(
format!("Failed to send request to github api: {}", err.to_string())
)
}
};
}