Skip to content

Commit 639e843

Browse files
committed
Add runtime feature flags CRATES=<true|false> TAGS=<true|false> for crates and tags commands
1 parent 036f1bd commit 639e843

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ serde_derive = "1.0"
1717
lazy_static = "1.4.0"
1818
log = "0.4.0"
1919
env_logger = "0.7.1"
20+
envy = "0.4"

postgres-docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM ubuntu:16.04
22

33
# Add the PostgreSQL PGP key to verify their Debian packages.
44
# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
5-
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
5+
RUN apt-key adv --keyserver hkp://pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
66

77
# Add PostgreSQL's repository. It contains the most recent stable release
88
# of PostgreSQL, ``9.5``.

src/main.rs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,23 @@ use crate::db::DB;
2020
use commands::{Args, Commands};
2121
use diesel::prelude::*;
2222
use serenity::{model::prelude::*, prelude::*};
23+
use serde::Deserialize;
24+
use envy;
2325

2426
pub(crate) type Result = crate::commands::Result<()>;
2527

26-
fn init_data() -> Result {
28+
#[derive(Deserialize)]
29+
struct Config {
30+
tags: bool,
31+
crates: bool,
32+
}
33+
34+
fn init_data(config: &Config) -> Result {
2735
use crate::schema::roles;
2836
info!("Loading data into database");
37+
2938
let mod_role = std::env::var("MOD_ID").map_err(|_| "MOD_ID env var not found")?;
3039
let talk_role = std::env::var("TALK_ID").map_err(|_| "TALK_ID env var not found")?;
31-
let wg_and_teams_role =
32-
std::env::var("WG_AND_TEAMS_ID").map_err(|_| "WG_AND_TEAMS_ID env var not found")?;
3340

3441
let conn = DB.get()?;
3542

@@ -50,7 +57,9 @@ fn init_data() -> Result {
5057
.run::<_, Box<dyn std::error::Error>, _>(|| {
5158
upsert_role("mod", &mod_role)?;
5259
upsert_role("talk", &talk_role)?;
53-
upsert_role("wg_and_teams", &wg_and_teams_role)?;
60+
if config.tags || config.crates {
61+
upsert_role("wg_and_teams", &std::env::var("WG_AND_TEAMS_ID").map_err(|_| "WG_AND_TEAMS_ID env var not found")?)?;
62+
}
5463

5564
Ok(())
5665
})?;
@@ -60,29 +69,36 @@ fn init_data() -> Result {
6069

6170
fn app() -> Result {
6271
info!("starting...");
72+
73+
let config = envy::from_env::<Config>()?;
74+
6375
let token = std::env::var("DISCORD_TOKEN")
6476
.map_err(|_| "missing environment variable: DISCORD_TOKEN")?;
6577

6678
let _ = db::run_migrations()?;
6779

68-
let _ = init_data()?;
80+
let _ = init_data(&config)?;
6981

7082
let mut cmds = Commands::new();
7183

72-
// Tags
73-
cmds.add("?tags delete {key}", tags::delete);
74-
cmds.add("?tags create {key} value...", tags::post);
75-
cmds.add("?tags help", tags::help);
76-
cmds.add("?tags", tags::get_all);
77-
cmds.add("?tag {key}", tags::get);
84+
if config.tags {
85+
// Tags
86+
cmds.add("?tags delete {key}", tags::delete);
87+
cmds.add("?tags create {key} value...", tags::post);
88+
cmds.add("?tags help", tags::help);
89+
cmds.add("?tags", tags::get_all);
90+
cmds.add("?tag {key}", tags::get);
91+
}
7892

79-
// crates.io
80-
cmds.add("?crate help", crates::help);
81-
cmds.add("?crate query...", crates::search);
93+
if config.crates {
94+
// crates.io
95+
cmds.add("?crate help", crates::help);
96+
cmds.add("?crate query...", crates::search);
8297

83-
// docs.rs
84-
cmds.add("?docs help", crates::doc_help);
85-
cmds.add("?docs query...", crates::doc_search);
98+
// docs.rs
99+
cmds.add("?docs help", crates::doc_help);
100+
cmds.add("?docs query...", crates::doc_search);
101+
}
86102

87103
// Slow mode.
88104
// 0 seconds disables slowmode

0 commit comments

Comments
 (0)