From 72e171b960872238053d1705cd859e931052a51b Mon Sep 17 00:00:00 2001 From: seth Date: Sat, 27 Jan 2024 23:03:34 -0500 Subject: [PATCH] chore: cleanup unused config properties --- src/config/discord.rs | 79 ++++++++++--------------------------------- src/config/github.rs | 65 ----------------------------------- src/config/mod.rs | 21 +++--------- src/main.rs | 2 +- src/utils/macros.rs | 6 ---- src/utils/mod.rs | 2 -- 6 files changed, 23 insertions(+), 152 deletions(-) delete mode 100644 src/config/github.rs delete mode 100644 src/utils/macros.rs diff --git a/src/config/discord.rs b/src/config/discord.rs index 9897ef7..1201c83 100644 --- a/src/config/discord.rs +++ b/src/config/discord.rs @@ -1,15 +1,7 @@ -use crate::required_var; +use std::str::FromStr; -use color_eyre::eyre::{Context as _, Result}; use log::*; -use poise::serenity_prelude::{ApplicationId, ChannelId}; -use url::Url; - -#[derive(Debug, Clone)] -pub struct RefractionOAuth2 { - pub redirect_uri: Url, - pub scope: String, -} +use poise::serenity_prelude::ChannelId; #[derive(Debug, Clone, Default)] pub struct RefractionChannels { @@ -18,70 +10,33 @@ pub struct RefractionChannels { #[derive(Debug, Clone, Default)] pub struct DiscordConfig { - pub client_id: ApplicationId, - pub client_secret: String, - pub bot_token: String, - pub oauth2: RefractionOAuth2, pub channels: RefractionChannels, } -impl Default for RefractionOAuth2 { - fn default() -> Self { - Self { - scope: "identify connections role_connections.write".to_string(), - redirect_uri: Url::parse("https://google.com").unwrap(), - } - } -} - -impl RefractionOAuth2 { - pub fn new_from_env() -> Result { - let unparsed = format!("{}/oauth2/callback", required_var!("PUBLIC_URI")); - let redirect_uri = Url::parse(&unparsed)?; - - debug!("OAuth2 Redirect URI is {redirect_uri}"); - Ok(Self { - redirect_uri, - ..Default::default() - }) - } -} - impl RefractionChannels { - pub fn new_from_env() -> Result { - let unparsed = std::env::var("DISCORD_SAY_LOG_CHANNELID"); - if let Ok(unparsed) = unparsed { - let id = unparsed.parse::()?; - let channel_id = ChannelId::from(id); + pub fn new_from_env() -> Self { + let say_log_channel_id = Self::get_channel_from_env("DISCORD_SAY_LOG_CHANNELID"); - debug!("Log channel is {id}"); - Ok(Self { - say_log_channel_id: Some(channel_id), - }) + if let Some(channel_id) = say_log_channel_id { + info!("Log channel is {channel_id}"); } else { warn!("DISCORD_SAY_LOG_CHANNELID is empty; this will disable logging in your server."); - Ok(Self { - say_log_channel_id: None, - }) } + + Self { say_log_channel_id } + } + + fn get_channel_from_env(var: &str) -> Option { + std::env::var(var) + .ok() + .and_then(|env_var| ChannelId::from_str(&env_var).ok()) } } impl DiscordConfig { - pub fn new_from_env() -> Result { - let unparsed_client = required_var!("DISCORD_CLIENT_ID").parse::()?; - let client_id = ApplicationId::from(unparsed_client); - let client_secret = required_var!("DISCORD_CLIENT_SECRET"); - let bot_token = required_var!("DISCORD_BOT_TOKEN"); - let oauth2 = RefractionOAuth2::new_from_env()?; - let channels = RefractionChannels::new_from_env()?; + pub fn new_from_env() -> Self { + let channels = RefractionChannels::new_from_env(); - Ok(Self { - client_id, - client_secret, - bot_token, - oauth2, - channels, - }) + Self { channels } } } diff --git a/src/config/github.rs b/src/config/github.rs deleted file mode 100644 index d7b2c04..0000000 --- a/src/config/github.rs +++ /dev/null @@ -1,65 +0,0 @@ -use color_eyre::eyre::{Context as _, Result}; - -use crate::required_var; - -#[derive(Debug, Clone)] -pub struct RefractionRepo { - pub owner: String, - pub repo: String, - pub key: String, - pub name: String, -} - -#[derive(Debug, Clone)] -pub struct GithubConfig { - pub token: String, - pub repos: Vec, - pub cache_sec: u16, - pub update_cron_job: String, -} - -impl Default for GithubConfig { - fn default() -> Self { - let owner = "PrismLauncher".to_string(); - let repos = Vec::::from([ - RefractionRepo { - owner: owner.clone(), - repo: "PrismLauncher".to_string(), - key: "launcher".to_string(), - name: "Launcher contributor".to_string(), - }, - RefractionRepo { - owner: owner.clone(), - repo: "prismlauncher.org".to_string(), - - key: "website".to_string(), - name: "Web developer".to_string(), - }, - RefractionRepo { - owner: owner.clone(), - repo: "Translations".to_string(), - - key: "translations".to_string(), - name: "Translator".to_string(), - }, - ]); - - Self { - repos, - cache_sec: 3600, - update_cron_job: "0 */10 * * * *".to_string(), // every 10 minutes - token: String::default(), - } - } -} - -impl GithubConfig { - pub fn new_from_env() -> Result { - let token = required_var!("GITHUB_TOKEN"); - - Ok(Self { - token, - ..Default::default() - }) - } -} diff --git a/src/config/mod.rs b/src/config/mod.rs index b04938c..156da9a 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,16 +1,9 @@ -use color_eyre::eyre::Result; - mod discord; -mod github; - -pub use discord::*; -pub use github::*; +use discord::DiscordConfig; #[derive(Debug, Clone)] pub struct Config { pub discord: DiscordConfig, - pub github: GithubConfig, - pub http_port: u16, pub redis_url: String, } @@ -18,22 +11,18 @@ impl Default for Config { fn default() -> Self { Self { discord: DiscordConfig::default(), - github: GithubConfig::default(), - http_port: 3000, redis_url: "redis://localhost:6379".to_string(), } } } impl Config { - pub fn new_from_env() -> Result { - let discord = DiscordConfig::new_from_env()?; - let github = GithubConfig::new_from_env()?; + pub fn new_from_env() -> Self { + let discord = DiscordConfig::new_from_env(); - Ok(Self { + Self { discord, - github, ..Default::default() - }) + } } } diff --git a/src/main.rs b/src/main.rs index 72ccddc..9cf8bd6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,7 @@ pub struct Data { impl Data { pub fn new() -> Result { - let config = Config::new_from_env()?; + let config = Config::new_from_env(); let storage = Storage::new(&config.redis_url)?; let octocrab = octocrab::instance(); diff --git a/src/utils/macros.rs b/src/utils/macros.rs deleted file mode 100644 index c167420..0000000 --- a/src/utils/macros.rs +++ /dev/null @@ -1,6 +0,0 @@ -#[macro_export] -macro_rules! required_var { - ($name: expr) => { - std::env::var($name).wrap_err_with(|| format!("Couldn't find {} in environment!", $name))? - }; -} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e8928e2..7592306 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,8 +1,6 @@ use color_eyre::eyre::{eyre, Result}; use rand::seq::SliceRandom; -#[macro_use] -mod macros; mod resolve_message; pub use resolve_message::resolve as resolve_message;