Skip to content

Commit 75cb795

Browse files
committed
Use rust-dotenv to manage env vars in development
This repo includes a `.env` file, but it doesn't appear to be used (at least not on the Rust side. I haven't looked at the ember side). This adds the `dotenv` crate to use the .env file to automatically set the environment variables from that file when set, without messing with the acutal environment of the machine. As part of this, I've taken the liberty of moving the heavily duplicated `env` function to somewhere common. I've also moved `.env` to `.env.sample`, and added `.env` to gitignore, as a person's specific environment variables shouldn't be in the repo, and the `.env` file provided isn't actually valid.
1 parent 02029a3 commit 75cb795

File tree

12 files changed

+31
-47
lines changed

12 files changed

+31
-47
lines changed

.env renamed to .env.sample

File renamed without changes.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
/libpeerconnection.log
1717
npm-debug.log
1818
testem.log
19+
.env

Cargo.lock

Lines changed: 9 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
@@ -57,6 +57,7 @@ log = "0.3"
5757
env_logger = "0.3"
5858
rustc-serialize = "0.3"
5959
license-exprs = "^1.1"
60+
dotenv = "0.8.0"
6061

6162
conduit = "0.7"
6263
conduit-conditional-get = "0.7"

src/bin/delete-crate.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::env;
1616
use std::io;
1717
use std::io::prelude::*;
1818

19-
use cargo_registry::Crate;
19+
use cargo_registry::{Crate, env};
2020

2121
#[allow(dead_code)]
2222
fn main() {
@@ -30,13 +30,6 @@ fn main() {
3030
}
3131
}
3232

33-
fn env(s: &str) -> String {
34-
match env::var(s).ok() {
35-
Some(s) => s,
36-
None => panic!("must have `{}` defined", s),
37-
}
38-
}
39-
4033
fn delete(tx: &postgres::Transaction) {
4134
let name = match env::args().nth(1) {
4235
None => { println!("needs a crate-name argument"); return }

src/bin/delete-version.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::env;
1616
use std::io;
1717
use std::io::prelude::*;
1818

19-
use cargo_registry::{Crate, Version};
19+
use cargo_registry::{Crate, Version, env};
2020

2121
#[allow(dead_code)]
2222
fn main() {
@@ -30,13 +30,6 @@ fn main() {
3030
}
3131
}
3232

33-
fn env(s: &str) -> String {
34-
match env::var(s).ok() {
35-
Some(s) => s,
36-
None => panic!("must have `{}` defined", s),
37-
}
38-
}
39-
4033
fn delete(tx: &postgres::Transaction) {
4134
let name = match env::args().nth(1) {
4235
None => { println!("needs a crate-name argument"); return }

src/bin/migrate.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::env;
88
use std::collections::HashSet;
99
use migrate::Migration;
1010

11+
use cargo_registry::env;
1112
use cargo_registry::krate::Crate;
1213
use cargo_registry::model::Model;
1314

@@ -23,13 +24,6 @@ fn main() {
2324
} else {
2425
apply(conn.transaction().unwrap(), migrations).unwrap();
2526
}
26-
27-
fn env(s: &str) -> String {
28-
match env::var(s).ok() {
29-
Some(s) => s,
30-
None => panic!("must have `{}` defined", s),
31-
}
32-
}
3327
}
3428

3529
fn apply(tx: postgres::Transaction,

src/bin/populate.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use std::env;
1515
use time::Duration;
1616
use rand::{StdRng, Rng};
1717

18+
use cargo_registry::env;
19+
1820
#[allow(dead_code)]
1921
fn main() {
2022
let conn = postgres::Connection::connect(&env("DATABASE_URL")[..],
@@ -27,13 +29,6 @@ fn main() {
2729
}
2830
}
2931

30-
fn env(s: &str) -> String {
31-
match env::var(s).ok() {
32-
Some(s) => s,
33-
None => panic!("must have `{}` defined", s),
34-
}
35-
}
36-
3732
fn update(tx: &postgres::Transaction) -> postgres::Result<()> {
3833
let ids = env::args().skip(1).filter_map(|arg| {
3934
arg.parse::<i32>().ok()

src/bin/server.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate civet;
66
extern crate git2;
77
extern crate env_logger;
88

9+
use cargo_registry::env;
910
use civet::Server;
1011
use std::env;
1112
use std::fs::{self, File};
@@ -78,10 +79,3 @@ fn main() {
7879
let (_tx, rx) = channel::<()>();
7980
rx.recv().unwrap();
8081
}
81-
82-
fn env(s: &str) -> String {
83-
match env::var(s).ok() {
84-
Some(s) => s,
85-
None => panic!("must have `{}` defined", s),
86-
}
87-
}

src/bin/update-downloads.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::env;
99
use std::collections::HashMap;
1010
use std::time::Duration;
1111

12-
use cargo_registry::{VersionDownload, Version, Model};
12+
use cargo_registry::{VersionDownload, Version, Model, env};
1313

1414
static LIMIT: i64 = 1000;
1515

@@ -31,13 +31,6 @@ fn main() {
3131
}
3232
}
3333

34-
fn env(s: &str) -> String {
35-
match env::var(s).ok() {
36-
Some(s) => s,
37-
None => panic!("must have `{}` defined", s),
38-
}
39-
}
40-
4134
fn update(conn: &postgres::GenericConnection) -> postgres::Result<()> {
4235
let mut max = 0;
4336
loop {
@@ -148,10 +141,10 @@ mod test {
148141
use postgres;
149142
use semver;
150143

151-
use cargo_registry::{Version, Crate, User, Model};
144+
use cargo_registry::{Version, Crate, User, Model, env};
152145

153146
fn conn() -> postgres::Connection {
154-
postgres::Connection::connect(&::env("TEST_DATABASE_URL")[..],
147+
postgres::Connection::connect(&env("TEST_DATABASE_URL")[..],
155148
postgres::SslMode::None).unwrap()
156149
}
157150

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
extern crate postgres as pg;
99
extern crate rustc_serialize;
1010
extern crate curl;
11+
extern crate dotenv;
1112
extern crate flate2;
1213
extern crate git2;
1314
extern crate license_exprs;
@@ -188,3 +189,11 @@ pub fn now() -> time::Timespec {
188189
pub fn encode_time(ts: time::Timespec) -> String {
189190
time::at_utc(ts).rfc3339().to_string()
190191
}
192+
193+
pub fn env(s: &str) -> String {
194+
dotenv::dotenv().ok();
195+
match ::std::env::var(s) {
196+
Ok(s) => s,
197+
Err(_) => panic!("must have `{}` defined", s),
198+
}
199+
}

src/tests/all.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate conduit;
66
extern crate conduit_middleware;
77
extern crate conduit_test;
88
extern crate curl;
9+
extern crate dotenv;
910
extern crate git2;
1011
extern crate postgres;
1112
extern crate rustc_serialize;
@@ -72,6 +73,7 @@ mod version;
7273
mod team;
7374

7475
fn app() -> (record::Bomb, Arc<App>, conduit_middleware::MiddlewareBuilder) {
76+
dotenv::dotenv().ok();
7577
static INIT: Once = ONCE_INIT;
7678
git::init();
7779

0 commit comments

Comments
 (0)