Skip to content

Commit

Permalink
Adding support for Github API key
Browse files Browse the repository at this point in the history
resolves #157
  • Loading branch information
dhruvinsh committed Nov 8, 2023
1 parent cc5aaf1 commit 2223c79
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,31 @@ use crate::{
use anyhow::Result;
use clap::{Args, CommandFactory, Parser};
use clap_complete::Shell;
use reqwest::Client;
use reqwest::{Client, Error};
use tracing::{error, info};

fn create_reqwest_client() -> Result<Client, Error> {
// fetch env variable
let github_token = match std::env::var("GITHUB_TOKEN") {
Ok(token) => token,
Err(_) => String::new(),
};

let mut headers = reqwest::header::HeaderMap::new();
if !github_token.is_empty() {
let auth_header_value =
reqwest::header::HeaderValue::from_str(&format!("Bearer {}", github_token))
.expect("Invalid header value");
headers.insert(reqwest::header::AUTHORIZATION, auth_header_value);
}

let client = reqwest::Client::builder()
.default_headers(headers)
.build()?;

Ok(client)
}

#[derive(Debug, Parser)]
#[command(version)]
enum Cli {
Expand Down Expand Up @@ -76,20 +98,19 @@ pub struct Update {
}

pub async fn start(config: Config) -> Result<()> {
let client = create_reqwest_client()?;
let cli = Cli::parse();

match cli {
Cli::Use {
version,
no_install,
} => {
let client = Client::new();
let version = super::version::parse_version_type(&client, &version).await?;

handlers::use_handler::start(version, !no_install, &client, config).await?;
}
Cli::Install { version } => {
let client = Client::new();
let mut version = super::version::parse_version_type(&client, &version).await?;

match handlers::install_handler::start(&mut version, &client, &config).await? {
Expand All @@ -108,7 +129,6 @@ pub async fn start(config: Config) -> Result<()> {
}
}
Cli::Sync => {
let client = Client::new();
info!("Starting sync process");
sync_handler::start(&client, config).await?;
}
Expand All @@ -124,7 +144,6 @@ pub async fn start(config: Config) -> Result<()> {
}
Cli::Update(data) => {
if data.version.is_some() || data.all {
let client = Client::new();
update_handler::start(data, &client, config).await?;
} else {
error!("Please provide a version or use the --all flag");
Expand Down

0 comments on commit 2223c79

Please sign in to comment.