Skip to content

Commit e5527f9

Browse files
committed
Use the Config struct for almost all env vars
1 parent 3d34d60 commit e5527f9

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/main.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ pub(crate) type Result = crate::commands::Result<()>;
2929
struct Config {
3030
tags: bool,
3131
crates: bool,
32+
discord_token: String,
33+
mod_id: String,
34+
talk_id: String,
35+
wg_and_teams_id: Option<String>,
3236
}
3337

3438
fn init_data(config: &Config) -> Result {
3539
use crate::schema::roles;
3640
info!("Loading data into database");
3741

38-
let mod_role = std::env::var("MOD_ID").map_err(|_| "MOD_ID env var not found")?;
39-
let talk_role = std::env::var("TALK_ID").map_err(|_| "TALK_ID env var not found")?;
40-
4142
let conn = DB.get()?;
4243

4344
let upsert_role = |name: &str, role_id: &str| -> Result {
@@ -55,11 +56,14 @@ fn init_data(config: &Config) -> Result {
5556
.build_transaction()
5657
.read_write()
5758
.run::<_, Box<dyn std::error::Error>, _>(|| {
58-
upsert_role("mod", &mod_role)?;
59-
upsert_role("talk", &talk_role)?;
59+
upsert_role("mod", &config.mod_id)?;
60+
upsert_role("talk", &config.talk_id)?;
61+
6062
if config.tags || config.crates {
61-
let wg_and_teams_role = &std::env::var("WG_AND_TEAMS_ID")
62-
.map_err(|_| "WG_AND_TEAMS_ID env var not found")?;
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.")?;
6367
upsert_role("wg_and_teams", &wg_and_teams_role)?;
6468
}
6569

@@ -70,12 +74,9 @@ fn init_data(config: &Config) -> Result {
7074
}
7175

7276
fn app() -> Result {
73-
info!("starting...");
74-
7577
let config = envy::from_env::<Config>()?;
7678

77-
let token = std::env::var("DISCORD_TOKEN")
78-
.map_err(|_| "missing environment variable: DISCORD_TOKEN")?;
79+
info!("starting...");
7980

8081
let _ = db::run_migrations()?;
8182

@@ -122,7 +123,7 @@ fn app() -> Result {
122123
Ok(())
123124
});
124125

125-
let mut client = Client::new_with_extras(&token, |e| {
126+
let mut client = Client::new_with_extras(&config.discord_token, |e| {
126127
e.event_handler(Messages { cmds });
127128
e.raw_event_handler(Events);
128129
e
@@ -136,8 +137,8 @@ fn app() -> Result {
136137
fn main() {
137138
env_logger::init();
138139

139-
if let Err(err) = app() {
140-
eprintln!("error: {}", err);
140+
if let Err(e) = app() {
141+
error!("{}", e);
141142
std::process::exit(1);
142143
}
143144
}

0 commit comments

Comments
 (0)