mirror of
https://github.com/shiroyashik/sculptor.git
synced 2025-12-06 13:01:12 +03:00
Added updates check
This commit is contained in:
parent
d101c84f21
commit
63f0b0d732
6 changed files with 157 additions and 93 deletions
|
|
@ -42,7 +42,7 @@ use state::Config;
|
|||
|
||||
// Utils
|
||||
mod utils;
|
||||
use utils::update_advanced_users;
|
||||
use utils::{check_updates, update_advanced_users};
|
||||
|
||||
// // Config
|
||||
// mod config;
|
||||
|
|
@ -76,9 +76,9 @@ async fn main() -> Result<()> {
|
|||
.pretty()
|
||||
.init();
|
||||
|
||||
info!("The Sculptor v{}{}", SCULPTOR_VERSION, check_updates("shiroyashik/sculptor", &SCULPTOR_VERSION).await?);
|
||||
|
||||
let config_file = std::env::var("CONFIG_PATH").unwrap_or_else(|_| "Config.toml".into());
|
||||
|
||||
info!("The Sculptor v{}", SCULPTOR_VERSION);
|
||||
// Config
|
||||
let config = Arc::new(Mutex::new(Config::parse(config_file.clone().into())));
|
||||
let listen = config.lock().await.listen.clone();
|
||||
|
|
|
|||
57
src/utils/check_updates.rs
Normal file
57
src/utils/check_updates.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use anyhow::anyhow;
|
||||
use reqwest::Client;
|
||||
use semver::Version;
|
||||
use serde::Deserialize;
|
||||
use tracing::error;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Tag {
|
||||
name: String
|
||||
}
|
||||
|
||||
async fn get_latest_version(repo: &str, current_version: Version) -> anyhow::Result<Option<String>> {
|
||||
let url = format!("https://api.github.com/repos/{repo}/tags");
|
||||
let client = Client::new();
|
||||
let response = client.get(&url).send().await?;
|
||||
|
||||
if response.status().is_success() {
|
||||
let tags: Vec<Tag> = response.json().await?;
|
||||
let latest_tag = tags.iter()
|
||||
.filter_map(|tag| {
|
||||
if tag.name.starts_with('v') { // v#.#.#
|
||||
Version::parse(&tag.name[1..]).ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.max();
|
||||
if let Some(latest_version) = latest_tag {
|
||||
if latest_version > current_version {
|
||||
Ok(Some(latest_version.to_string()))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
Err(anyhow!("Can't find version tags!"))
|
||||
}
|
||||
} else {
|
||||
Err(anyhow!("Response status code: {}", response.status().as_u16()))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check_updates(repo: &str, current_version: &str) -> anyhow::Result<String> {
|
||||
let current_version = semver::Version::parse(¤t_version)?;
|
||||
|
||||
match get_latest_version(repo, current_version).await {
|
||||
Ok(d) => if let Some(latest_version) = d {
|
||||
Ok(format!(" - Available new v{latest_version}!"))
|
||||
} else {
|
||||
Ok(String::new())
|
||||
},
|
||||
Err(e) => {
|
||||
error!("Can't fetch updates: {e:?}");
|
||||
Ok(String::new())
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
5
src/utils/mod.rs
Normal file
5
src/utils/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
mod utils;
|
||||
mod check_updates;
|
||||
|
||||
pub use utils::*;
|
||||
pub use check_updates::check_updates;
|
||||
Loading…
Add table
Add a link
Reference in a new issue