Skip to content
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
25 changes: 24 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
23 changes: 23 additions & 0 deletions src/utils/github.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use reqwest;
use serde_json::Value;

pub fn get_latest_release_version(repo: &str) -> Result<String, Box<dyn std::error::Error>> {
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())
}
}
Loading