Skip to content

Commit

Permalink
Remove the internet checker as it was just very slow and wastes requests
Browse files Browse the repository at this point in the history
Replaced with a warning when the program errors out because of a possible lack of internet connectivity
  • Loading branch information
theRookieCoder committed Feb 7, 2024
1 parent 125d893 commit 40d0af8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 48 deletions.
63 changes: 16 additions & 47 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ use libium::config::{
structs::{Config, ModIdentifier, Modpack, Profile},
};
use octocrab::OctocrabBuilder;
use once_cell::sync::{Lazy, OnceCell};
use reqwest::header::{AUTHORIZATION, USER_AGENT};
use once_cell::sync::Lazy;
use std::{
env::{var, var_os},
process::ExitCode,
Expand All @@ -49,7 +48,6 @@ const CROSS: &str = "×";
pub static TICK: Lazy<ColoredString> = Lazy::new(|| "✓".green());
pub static YELLOW_TICK: Lazy<ColoredString> = Lazy::new(|| "✓".yellow());
pub static THEME: Lazy<ColorfulTheme> = Lazy::new(Default::default);
pub static GITHUB_TOKEN: OnceCell<String> = OnceCell::new();
#[allow(clippy::expect_used)]
pub static STYLE_NO: Lazy<ProgressStyle> = Lazy::new(|| {
ProgressStyle::default_bar()
Expand Down Expand Up @@ -84,6 +82,18 @@ fn main() -> ExitCode {
let runtime = builder.build().expect("Could not initialise Tokio runtime");
if let Err(err) = runtime.block_on(actual_main(cli)) {
eprintln!("{}", err.to_string().red().bold());
if err
.to_string()
.to_lowercase()
.contains("error trying to connect")
{
eprintln!(
"{}",
"Verify that you are connnected to the internet"
.yellow()
.bold()
);
}
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
Expand Down Expand Up @@ -115,15 +125,11 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
};
}

let mut github = OctocrabBuilder::new();
if let Some(token) = cli_app.github_token {
GITHUB_TOKEN.get_or_init(|| token.to_string());
github = github.personal_token(token);
} else if let Ok(token) = var("GITHUB_TOKEN") {
GITHUB_TOKEN.get_or_init(|| token.to_string());
}

let mut github = OctocrabBuilder::new();
if let Some(token) = GITHUB_TOKEN.get() {
github = github.personal_token(token.clone());
github = github.personal_token(token);
}

let modrinth = Ferinth::new(
Expand Down Expand Up @@ -160,7 +166,6 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
ignore_mod_loader,
} => {
let profile = get_active_profile(&mut config)?;
check_internet().await?;
eprint!("Adding mod... ");
if let Ok(project_id) = identifier.parse::<i32>() {
let name = libium::add::curseforge(
Expand Down Expand Up @@ -225,7 +230,6 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
let profile = get_active_profile(&mut config)?;
check_empty_profile(profile)?;
if verbose {
check_internet().await?;
subcommands::list::verbose(modrinth, curseforge, profile, markdown).await?;
} else {
println!(
Expand Down Expand Up @@ -267,7 +271,6 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
output_dir,
install_overrides,
} => {
check_internet().await?;
if let Ok(project_id) = identifier.parse::<i32>() {
subcommands::modpack::add::curseforge(
&curseforge,
Expand Down Expand Up @@ -323,7 +326,6 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
subcommands::modpack::switch(&mut config, modpack_name)?;
}
ModpackSubCommands::Upgrade => {
check_internet().await?;
subcommands::modpack::upgrade(
&modrinth,
&curseforge,
Expand Down Expand Up @@ -353,7 +355,6 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
name,
output_dir,
} => {
check_internet().await?;
subcommands::profile::configure(
get_active_profile(&mut config)?,
game_version,
Expand All @@ -370,9 +371,6 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
name,
output_dir,
} => {
if game_version.is_none() {
check_internet().await?;
}
subcommands::profile::create(
&mut config,
import,
Expand Down Expand Up @@ -417,7 +415,6 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
subcommands::remove(profile, mod_names)?;
}
SubCommands::Upgrade => {
check_internet().await?;
let profile = get_active_profile(&mut config)?;
check_empty_profile(profile)?;
subcommands::upgrade(modrinth, curseforge, github.build()?, profile).await?;
Expand Down Expand Up @@ -482,31 +479,3 @@ fn check_empty_profile(profile: &Profile) -> Result<()> {
}
Ok(())
}

/// Check for an internet connection
async fn check_internet() -> Result<()> {
let client = reqwest::Client::default();

client
.get(ferinth::BASE_URL.as_ref())
.send()
.await?
.error_for_status()?;

client
.get("https://api.curseforge.com/")
.send()
.await?
.error_for_status()?;

let mut github = client.get("https://api.github.com/").header(
USER_AGENT,
concat!(env!("CARGO_PKG_NAME"), " ", env!("CARGO_PKG_VERSION")),
);
if let Some(token) = GITHUB_TOKEN.get() {
github = github.header(AUTHORIZATION, format!("Bearer {token}"));
}
github.send().await?.error_for_status()?;

Ok(())
}
1 change: 0 additions & 1 deletion src/subcommands/list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(clippy::unwrap_used)]
#![allow(clippy::needless_pass_by_value)]

use crate::TICK;
use anyhow::{Context, Result};
Expand Down

0 comments on commit 40d0af8

Please sign in to comment.