Skip to content

Commit

Permalink
perf: use static variable for cmd arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
hlf20010508 committed Jan 19, 2024
1 parent 3867e9e commit 70324b1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
7 changes: 7 additions & 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 @@ opt-level = 'z'
actix-web = { version = "4.4.1", default-features = false, features = ["macros"] }
anyhow = { version = "1.0.79", default-features = false, features = ["std"] }
env_logger = { version = "0.10.1",default-features = false, features = ["humantime"] }
lazy_static = { version = "1.4.0", default-features = false }
pico-args = { version = "0.5.0", default-features = false, features = ["short-space-opt"] }
reqwest = { version = "0.11.23", default-features = false, features = ["rustls-tls"] }
serde = { version = "1.0.193", default-features = false, features = ["derive"] }
Expand Down
25 changes: 25 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
:project: gpts-code-analyst
:author: L-ING
:copyright: (C) 2024 L-ING <[email protected]>
:license: MIT, see LICENSE for more details.
*/

use lazy_static::lazy_static;
use pico_args::{Arguments, Error};
use std::fmt::Display;
use std::str::FromStr;

fn get_arg_value<T>(arg_name: &'static str) -> Result<T, Error>
where
T: FromStr,
T::Err: Display,
{
let mut args = Arguments::from_env();
let value: T = args.value_from_str(arg_name)?;
return Ok(value);
}

lazy_static! {
pub static ref GITHUB_TOKEN: Result<String, Error> = get_arg_value("--token");
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use env_logger::Env;
mod handlers;
mod structures;
mod utils;
mod env;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
Expand Down
5 changes: 2 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
*/

use anyhow::Context;
use pico_args::Arguments;
use std::error::Error;

use crate::env::GITHUB_TOKEN;
use crate::structures::{FileInfo, PathTree, RepoInfo};

pub fn create_client() -> Result<reqwest::Client, Box<dyn Error>> {
let mut args = Arguments::from_env();
let github_token = args.value_from_str::<&str, String>("--token")?;
let github_token = GITHUB_TOKEN.as_ref()?;
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
reqwest::header::AUTHORIZATION,
Expand Down

0 comments on commit 70324b1

Please sign in to comment.