Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Github API key #157 #161

Merged
merged 5 commits into from
Nov 8, 2023
Merged
Changes from 1 commit
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
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") {
dhruvinsh marked this conversation as resolved.
Show resolved Hide resolved
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
Loading