Skip to content

Commit

Permalink
Fix #148 colouring on conhost
Browse files Browse the repository at this point in the history
Add github token to connnection check too
  • Loading branch information
theRookieCoder committed Feb 6, 2024
1 parent ce7206e commit fec818c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
62 changes: 26 additions & 36 deletions Cargo.lock

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

41 changes: 26 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use libium::config::{
structs::{Config, ModIdentifier, Modpack, Profile},
};
use octocrab::OctocrabBuilder;
use once_cell::sync::Lazy;
use reqwest::header::USER_AGENT;
use once_cell::sync::{Lazy, OnceCell};
use reqwest::header::{AUTHORIZATION, USER_AGENT};
use std::{
env::{var, var_os},
process::ExitCode,
Expand All @@ -49,6 +49,7 @@ 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 All @@ -74,6 +75,11 @@ fn main() -> ExitCode {
if let Some(threads) = cli.threads {
builder.worker_threads(threads);
}
#[cfg(windows)]
{
#[allow(clippy::unwrap_used)]
colored::control::set_virtual_terminal(true).unwrap();
}
#[allow(clippy::expect_used)] // No error handling yet
let runtime = builder.build().expect("Could not initialise Tokio runtime");
if let Err(err) = runtime.block_on(actual_main(cli)) {
Expand Down Expand Up @@ -109,11 +115,15 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
};
}

let mut github = OctocrabBuilder::new();
if let Some(token) = cli_app.github_token {
github = github.personal_token(token);
GITHUB_TOKEN.get_or_init(|| token.to_string());
} else if let Ok(token) = var("GITHUB_TOKEN") {
github = github.personal_token(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());
}

let modrinth = Ferinth::new(
Expand Down Expand Up @@ -542,26 +552,27 @@ fn check_empty_profile(profile: &Profile) -> Result<()> {
/// 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()?;
client
.get("https://api.github.com/")
// GitHub API seems to work better with a proper user agent
.header(
USER_AGENT,
concat!(env!("CARGO_PKG_NAME"), " ", env!("CARGO_PKG_VERSION")),
)
.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(())
}

0 comments on commit fec818c

Please sign in to comment.