Skip to content

Commit 3dc8a62

Browse files
authored
Merge pull request #43 from rust-lang/runtime-feature-flags
Runtime feature flags
2 parents 036f1bd + e5527f9 commit 3dc8a62

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
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: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,25 @@ mod welcome;
1919
use crate::db::DB;
2020
use commands::{Args, Commands};
2121
use diesel::prelude::*;
22+
use envy;
23+
use serde::Deserialize;
2224
use serenity::{model::prelude::*, prelude::*};
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+
discord_token: String,
33+
mod_id: String,
34+
talk_id: String,
35+
wg_and_teams_id: Option<String>,
36+
}
37+
38+
fn init_data(config: &Config) -> Result {
2739
use crate::schema::roles;
2840
info!("Loading data into database");
29-
let mod_role = std::env::var("MOD_ID").map_err(|_| "MOD_ID env var not found")?;
30-
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")?;
3341

3442
let conn = DB.get()?;
3543

@@ -48,9 +56,16 @@ fn init_data() -> Result {
4856
.build_transaction()
4957
.read_write()
5058
.run::<_, Box<dyn std::error::Error>, _>(|| {
51-
upsert_role("mod", &mod_role)?;
52-
upsert_role("talk", &talk_role)?;
53-
upsert_role("wg_and_teams", &wg_and_teams_role)?;
59+
upsert_role("mod", &config.mod_id)?;
60+
upsert_role("talk", &config.talk_id)?;
61+
62+
if config.tags || config.crates {
63+
let wg_and_teams_role = config
64+
.wg_and_teams_id
65+
.as_ref()
66+
.ok_or_else(|| "missing value for field wg_and_teams_id.\n\nIf you enabled tags or crates then you need the WG_AND_TEAMS_ID env var.")?;
67+
upsert_role("wg_and_teams", &wg_and_teams_role)?;
68+
}
5469

5570
Ok(())
5671
})?;
@@ -59,30 +74,34 @@ fn init_data() -> Result {
5974
}
6075

6176
fn app() -> Result {
77+
let config = envy::from_env::<Config>()?;
78+
6279
info!("starting...");
63-
let token = std::env::var("DISCORD_TOKEN")
64-
.map_err(|_| "missing environment variable: DISCORD_TOKEN")?;
6580

6681
let _ = db::run_migrations()?;
6782

68-
let _ = init_data()?;
83+
let _ = init_data(&config)?;
6984

7085
let mut cmds = Commands::new();
7186

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);
87+
if config.tags {
88+
// Tags
89+
cmds.add("?tags delete {key}", tags::delete);
90+
cmds.add("?tags create {key} value...", tags::post);
91+
cmds.add("?tags help", tags::help);
92+
cmds.add("?tags", tags::get_all);
93+
cmds.add("?tag {key}", tags::get);
94+
}
7895

79-
// crates.io
80-
cmds.add("?crate help", crates::help);
81-
cmds.add("?crate query...", crates::search);
96+
if config.crates {
97+
// crates.io
98+
cmds.add("?crate help", crates::help);
99+
cmds.add("?crate query...", crates::search);
82100

83-
// docs.rs
84-
cmds.add("?docs help", crates::doc_help);
85-
cmds.add("?docs query...", crates::doc_search);
101+
// docs.rs
102+
cmds.add("?docs help", crates::doc_help);
103+
cmds.add("?docs query...", crates::doc_search);
104+
}
86105

87106
// Slow mode.
88107
// 0 seconds disables slowmode
@@ -104,7 +123,7 @@ fn app() -> Result {
104123
Ok(())
105124
});
106125

107-
let mut client = Client::new_with_extras(&token, |e| {
126+
let mut client = Client::new_with_extras(&config.discord_token, |e| {
108127
e.event_handler(Messages { cmds });
109128
e.raw_event_handler(Events);
110129
e
@@ -118,8 +137,8 @@ fn app() -> Result {
118137
fn main() {
119138
env_logger::init();
120139

121-
if let Err(err) = app() {
122-
eprintln!("error: {}", err);
140+
if let Err(e) = app() {
141+
error!("{}", e);
123142
std::process::exit(1);
124143
}
125144
}

0 commit comments

Comments
 (0)