Skip to content

Commit

Permalink
feat: --no-fail
Browse files Browse the repository at this point in the history
  • Loading branch information
adamperkowski committed Nov 17, 2024
1 parent b1b3fcf commit 4db55bc
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 47 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ github = ["reqwest"]
[dependencies]
clap = { version = "4.5.21", features = ["derive", "color", "error-context", "help", "std", "usage"], default-features = false }
colored = "2.1.0"
lazy_static = "1.5.0"
reqwest = { version = "0.12.9", features = ["__tls", "charset", "default-tls", "h2", "http2", "json"], default-features = false, optional = true }
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.132"
Expand Down
7 changes: 4 additions & 3 deletions src/api/aur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ pub fn get_latest(package: String, _: String) -> crate::api::ReleaseFuture {
match result.status() {
StatusCode::OK => (),
status => {
crate::custom_error("GET request didn't return 200", format!("\n{}", status));
crate::custom_error("GET request didn't return 200", format!("\n{}", status), "");
return None;
}
}

let json: serde_json::Value = result.json().await.unwrap();
let first_result = json.get("results").unwrap().get(0).unwrap();

crate::api::Release {
Some(crate::api::Release {
tag_name: first_result
.get("Version")
.unwrap()
Expand All @@ -33,6 +34,6 @@ pub fn get_latest(package: String, _: String) -> crate::api::ReleaseFuture {
.replace("\"", "")
.to_string(),
html_url: first_result.get("URL").unwrap().to_string(),
}
})
})
}
19 changes: 16 additions & 3 deletions src/api/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use reqwest::{
StatusCode,
};

pub fn get_latest(_: String, repo: String) -> crate::api::ReleaseFuture {
pub fn get_latest(package: String, repo: String) -> crate::api::ReleaseFuture {
Box::pin(async move {
let url = format!("https://api.github.com/repos/{}/releases/latest", repo);
let mut headers = HeaderMap::new();
Expand All @@ -22,11 +22,24 @@ pub fn get_latest(_: String, repo: String) -> crate::api::ReleaseFuture {

match result.status() {
StatusCode::OK => (),
StatusCode::FORBIDDEN => {
crate::custom_error(
"GET request returned 430: ",
format!("{}\nwe might be getting rate-limited here", package),
"",
);
return None;
}
status => {
crate::custom_error("GET request didn't return 200", format!("\n{}", status));
crate::custom_error(
"GET request didn't return 200: ",
format!("{}\n{}", package, status),
"",
);
return None;
}
}

result.json().await.unwrap()
Some(result.json().await.unwrap())
})
}
3 changes: 2 additions & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub struct Release {
pub html_url: String,
}

pub type ReleaseFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Release> + Send>>;
pub type ReleaseFuture =
std::pin::Pin<Box<dyn std::future::Future<Output = Option<Release>> + Send>>;

pub struct Api {
pub name: &'static str,
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn load(custom_path: Option<String>) -> (Config, PathBuf) {
PathBuf::from(config_path),
);
} else {
crate::custom_error("specified config file not found", String::new());
crate::custom_error("specified config file not found", String::new(), "");
}
}

Expand Down Expand Up @@ -93,7 +93,7 @@ pub fn load(custom_path: Option<String>) -> (Config, PathBuf) {
crate::custom_error(
"no config found",
"\nconfig file locations:\n ~/.config/nvrs.toml\n ./nvrs.toml\nmake sure the file is not empty".to_string(),
);
"");
}

(
Expand Down
82 changes: 46 additions & 36 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use clap::Parser;
use colored::Colorize;
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};

mod api;
pub mod config;
mod verfiles;

lazy_static::lazy_static! {
static ref MSG_NOEXIT: Mutex<bool> = Mutex::new(false);
}

#[derive(Parser)]
#[command(version, about)]
struct Cli {
Expand Down Expand Up @@ -41,6 +46,9 @@ struct Cli {
)]
custom_config: Option<String>,

#[arg(long, help = "Don't exit the program on recoverable errors")]
no_fail: bool,

#[arg(long, help = "Display copyright information")]
copyright: bool,
}
Expand All @@ -49,6 +57,10 @@ struct Cli {
async fn main() {
let cli = Cli::parse();

if cli.no_fail {
*MSG_NOEXIT.lock().unwrap() = true;
}

if cli.copyright {
let current_year = 1970
+ (SystemTime::now()
Expand Down Expand Up @@ -115,7 +127,7 @@ copies or substantial portions of the Software.",
oldver.data.data.insert(package_name, package.1.clone());
}
} else {
custom_error_noexit("package not in newver: ", package_name);
custom_error("package not in newver: ", package_name, "noexit");
}
}

Expand All @@ -129,7 +141,7 @@ copies or substantial portions of the Software.",
if config_content.packages.contains_key(&package_name) {
config_content.packages.remove(&package_name);
} else {
custom_error_noexit("package not in config: ", package_name.clone());
custom_error("package not in config: ", package_name.clone(), "noexit");
}
newver.data.data.remove(&package_name);
oldver.data.data.remove(&package_name);
Expand All @@ -144,35 +156,33 @@ copies or substantial portions of the Software.",

for package in config_content.packages {
if let Some(pkg) = newver.data.data.iter_mut().find(|p| p.0 == &package.0) {
let latest = run_source(package.clone())
.await
.unwrap()
.tag_name
.replacen(&package.1.prefix, "", 1);
if let Some(latest) = run_source(package.clone()).await {
let latest_tag = latest.tag_name.replacen(&package.1.prefix, "", 1);

if pkg.1.version != latest {
println!(
"| {} {} -> {}",
package.0.blue(),
pkg.1.version.red(),
latest.green()
);
pkg.1.version = latest;
if pkg.1.version != latest_tag {
println!(
"| {} {} -> {}",
package.0.blue(),
pkg.1.version.red(),
latest_tag.green()
);
pkg.1.version = latest_tag;
}
}
} else {
let latest = run_source(package.clone()).await.unwrap();

let tag = latest.tag_name.replacen(&package.1.prefix, "", 1);

println!("| {} {} -> {}", package.0.blue(), "NONE".red(), tag.green());
newver.data.data.insert(
package.0,
verfiles::Package {
version: tag,
gitref: format!("refs/tags/{}", latest.tag_name),
url: latest.html_url,
},
);
if let Some(latest) = run_source(package.clone()).await {
let tag = latest.tag_name.replacen(&package.1.prefix, "", 1);

println!("| {} {} -> {}", package.0.blue(), "NONE".red(), tag.green());
newver.data.data.insert(
package.0,
verfiles::Package {
version: tag,
gitref: format!("refs/tags/{}", latest.tag_name),
url: latest.html_url,
},
);
}
}
}

Expand All @@ -183,17 +193,17 @@ copies or substantial portions of the Software.",
async fn run_source(package: (String, config::Package)) -> Option<api::Release> {
let source = package.1.source.clone();
if let Some(api_used) = api::API_LIST.iter().find(|a| a.name == source) {
Some((api_used.func)(package.0, package.1.get_api_arg(api_used.name).unwrap()).await)
Some((api_used.func)(package.0, package.1.get_api_arg(api_used.name).unwrap()).await?)
} else {
custom_error("api not found: ", source);
custom_error("api not found: ", source, "");
None
}
}

pub fn custom_error(message: &'static str, message_ext: String) {
custom_error_noexit(message, message_ext);
std::process::exit(1);
}
pub fn custom_error_noexit(message: &'static str, message_ext: String) {
println!("! {}{}", message.red(), message_ext);
pub fn custom_error(message: &'static str, message_ext: String, override_exit: &str) {
println!("! {}{}", message.red(), message_ext.replace("\n", "\n "));

if override_exit != "noexit" && !*MSG_NOEXIT.lock().unwrap() {
std::process::exit(1);
}
}
5 changes: 3 additions & 2 deletions src/verfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct Verfile {

pub fn load(config_table: Option<ConfigTable>) -> Option<(Verfile, Verfile)> {
if config_table.is_none() {
crate::custom_error(CONFIG_NONE_M, CONFIG_NONE_E.to_string());
crate::custom_error(CONFIG_NONE_M, CONFIG_NONE_E.to_string(), "");
}
let config_table = config_table.unwrap();

Expand All @@ -50,12 +50,13 @@ pub fn load(config_table: Option<ConfigTable>) -> Option<(Verfile, Verfile)> {
crate::custom_error(
"unsupported verfile version",
"\nplease update your verfiles".to_string(),
"",
);
}

Some((oldver, newver))
} else {
crate::custom_error(XVER_NONE_M, CONFIG_NONE_E.to_string());
crate::custom_error(XVER_NONE_M, CONFIG_NONE_E.to_string(), "");
None
}
}
Expand Down

0 comments on commit 4db55bc

Please sign in to comment.