Skip to content

Commit

Permalink
🔧 prepare configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed May 11, 2023
1 parent cb2964d commit d253f4d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .env.example
@@ -0,0 +1,2 @@
GH_GPT_GITHUB_TOKEN="..."
GH_GPT_OPENAI_API_KEY="..."
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -15,5 +15,6 @@ Cargo.lock


# Added by cargo

/target

.env
9 changes: 9 additions & 0 deletions Cargo.toml
Expand Up @@ -7,5 +7,14 @@ edition = "2021"

[dependencies]
clap = {version = "4.2.7", features = ["derive"]}
color-eyre = "0.6.2"
config = "0.13.3"
dotenv = "0.15.0"
env_logger = "0.10.0"
lazy_static = "1.4.0"
log = "0.4.17"
serde = "1.0.163"
serde_derive = "1.0.163"

[profile.dev.package.backtrace]
opt-level = 3
29 changes: 21 additions & 8 deletions src/config.rs
@@ -1,14 +1,27 @@
//! General configuration

/// Configuration for the application.
#[derive(Debug, Default)]
pub struct Config {
pub api_keys: ApiKeys,
}
use color_eyre::eyre::Result;
use config::Config;
use serde_derive::Deserialize;

/// Configuration for the API keys.
#[derive(Debug, Default)]
pub struct ApiKeys {
/// Configuration for the application.
#[derive(Debug, Default, Deserialize)]
pub struct GhGptConfig {
pub github_token: String,
pub openai_api_key: String,
}

impl GhGptConfig {
/// Create a new configuration from environment variables.
pub fn new() -> Result<Self> {
CONFIG.clone().try_deserialize().map_err(|e| e.into())
}
}

lazy_static::lazy_static! {
#[derive(Debug)]
pub static ref CONFIG: Config = Config::builder()
.add_source(config::Environment::with_prefix("gh_gpt"))
.build()
.unwrap();
}
6 changes: 4 additions & 2 deletions src/labels.rs
@@ -1,13 +1,15 @@
//! Module related to Github labels.

use color_eyre::eyre::Result;
use log::debug;

use crate::config::Config;
use crate::config::GhGptConfig;

/// Labelize a Github issue.
/// # Arguments
/// * `cfg` - The application configuration.
/// * `gh_issue_number` - The Github issue number.
pub fn labelize(_cfg: &Config, gh_issue_number: u64) {
pub fn labelize(_cfg: &GhGptConfig, gh_issue_number: u64) -> Result<()> {
debug!("labelize: {}", gh_issue_number);
Ok(())
}
35 changes: 21 additions & 14 deletions src/main.rs
@@ -1,32 +1,39 @@
#[macro_use]
extern crate log;
use clap::Parser;
use color_eyre::eyre::Result;
use dotenv::dotenv;
use gh_gpt::cli::commands::{Cli, Commands};
use gh_gpt::config::Config;
use gh_gpt::config::GhGptConfig;
use gh_gpt::labels::labelize;

fn main() {
fn main() -> Result<()> {
// Initialize the logger.
env_logger::init();

// Initialize the error handler.
color_eyre::install()?;

// Load the environment variables from the .env file.
dotenv()?;

// Say hello.
info!("hello from gh-gpt 🤖 !");

// Parse the command line arguments.
let cli = Cli::parse();

// Retrieve the application configuration.
let cfg = config();
let cfg = GhGptConfig::new()?;

if let Some(command) = cli.command {
match command {
Commands::Labelize { gh_issue_number } => {
labelize(&cfg, gh_issue_number);
}
// Execute the command.
match cli.command {
Some(command) => match command {
Commands::Labelize { gh_issue_number } => labelize(&cfg, gh_issue_number),
},
None => {
info!("nothing to do there, bye 👋");
Ok(())
}
}
}

/// Parse and return the application configuration.
fn config() -> Config {
// TODO: Parse the configuration from the environment variables.
Config::default()
}

0 comments on commit d253f4d

Please sign in to comment.