From a17c09a5e19b3738c0230d1a6218b9590230ec6b Mon Sep 17 00:00:00 2001 From: Ibrahim Rahhal Date: Sat, 12 Jul 2025 12:42:46 +0300 Subject: [PATCH] Message when new version available --- src/main.rs | 25 ++++++++++++++++++++++++- src/utils/github.rs | 23 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/utils/github.rs diff --git a/src/main.rs b/src/main.rs index 24421c7..99478d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,9 +14,11 @@ mod utils { pub mod terminal; pub mod generic; pub mod api; + pub mod github; } -use std::str::FromStr; + +use std::{str::FromStr}; use clap::{Parser, Subcommand, CommandFactory}; use config::Config; use scanners::fortify::parse as fortify_parse; @@ -154,9 +156,30 @@ impl FromStr for Scanner { } } +fn check_latest_version() { + let latest_version = utils::github::get_latest_release_version("Corgea/cli"); + let current_version = env!("CARGO_PKG_VERSION"); + + match latest_version { + Ok(latest) => { + if latest != current_version { + println!("{}", utils::terminal::set_text_color( + &format!("A new version of Corgea CLI is available: {} (current: {})", latest, current_version), + utils::terminal::TerminalColor::Red + )); + println!("Update with: pip install --upgrade corgea-cli"); + println!(); + } + } + Err(_) => { + // ignore the error + } + } +} fn main() { let cli = Cli::parse(); let mut corgea_config = Config::load().expect("Failed to load config"); + check_latest_version(); fn verify_token_and_exit_when_fail (config: &Config) { if config.get_token().is_empty() { eprintln!("No token set.\nPlease run 'corgea login' to authenticate.\nFor more info checkout our docs at Check out our docs at https://docs.corgea.app/install_cli#login-with-the-cli"); diff --git a/src/utils/github.rs b/src/utils/github.rs new file mode 100644 index 0000000..ddde9f0 --- /dev/null +++ b/src/utils/github.rs @@ -0,0 +1,23 @@ +use reqwest; +use serde_json::Value; + +pub fn get_latest_release_version(repo: &str) -> Result> { + let client = reqwest::blocking::Client::new(); + let url = format!("https://api.github.com/repos/{}/releases/latest", repo); + + let response = client + .get(&url) + .header("User-Agent", "Corgea-CLI") + .send()?; + + if !response.status().is_success() { + return Err(format!("Failed to fetch release info: {}", response.status()).into()); + } + + let release_info: Value = response.json()?; + + match release_info.get("tag_name") { + Some(tag) => Ok(tag.as_str().unwrap_or("").trim_start_matches("v.").trim_start_matches('v').to_string()), + None => Err("No tag_name found in release info".into()) + } +}