Skip to content

Use rust-dotenv to manage env vars in development #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
/libpeerconnection.log
npm-debug.log
testem.log
.env
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ log = "0.3"
env_logger = "0.3"
rustc-serialize = "0.3"
license-exprs = "^1.1"
dotenv = "0.8.0"

conduit = "0.7"
conduit-conditional-get = "0.7"
Expand Down
9 changes: 1 addition & 8 deletions src/bin/delete-crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::env;
use std::io;
use std::io::prelude::*;

use cargo_registry::Crate;
use cargo_registry::{Crate, env};

#[allow(dead_code)]
fn main() {
Expand All @@ -30,13 +30,6 @@ fn main() {
}
}

fn env(s: &str) -> String {
match env::var(s).ok() {
Some(s) => s,
None => panic!("must have `{}` defined", s),
}
}

fn delete(tx: &postgres::Transaction) {
let name = match env::args().nth(1) {
None => { println!("needs a crate-name argument"); return }
Expand Down
9 changes: 1 addition & 8 deletions src/bin/delete-version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::env;
use std::io;
use std::io::prelude::*;

use cargo_registry::{Crate, Version};
use cargo_registry::{Crate, Version, env};

#[allow(dead_code)]
fn main() {
Expand All @@ -30,13 +30,6 @@ fn main() {
}
}

fn env(s: &str) -> String {
match env::var(s).ok() {
Some(s) => s,
None => panic!("must have `{}` defined", s),
}
}

fn delete(tx: &postgres::Transaction) {
let name = match env::args().nth(1) {
None => { println!("needs a crate-name argument"); return }
Expand Down
8 changes: 1 addition & 7 deletions src/bin/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::env;
use std::collections::HashSet;
use migrate::Migration;

use cargo_registry::env;
use cargo_registry::krate::Crate;
use cargo_registry::model::Model;

Expand All @@ -23,13 +24,6 @@ fn main() {
} else {
apply(conn.transaction().unwrap(), migrations).unwrap();
}

fn env(s: &str) -> String {
match env::var(s).ok() {
Some(s) => s,
None => panic!("must have `{}` defined", s),
}
}
}

fn apply(tx: postgres::Transaction,
Expand Down
9 changes: 2 additions & 7 deletions src/bin/populate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use std::env;
use time::Duration;
use rand::{StdRng, Rng};

use cargo_registry::env;

#[allow(dead_code)]
fn main() {
let conn = postgres::Connection::connect(&env("DATABASE_URL")[..],
Expand All @@ -27,13 +29,6 @@ fn main() {
}
}

fn env(s: &str) -> String {
match env::var(s).ok() {
Some(s) => s,
None => panic!("must have `{}` defined", s),
}
}

fn update(tx: &postgres::Transaction) -> postgres::Result<()> {
let ids = env::args().skip(1).filter_map(|arg| {
arg.parse::<i32>().ok()
Expand Down
8 changes: 1 addition & 7 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate civet;
extern crate git2;
extern crate env_logger;

use cargo_registry::env;
use civet::Server;
use std::env;
use std::fs::{self, File};
Expand Down Expand Up @@ -78,10 +79,3 @@ fn main() {
let (_tx, rx) = channel::<()>();
rx.recv().unwrap();
}

fn env(s: &str) -> String {
match env::var(s).ok() {
Some(s) => s,
None => panic!("must have `{}` defined", s),
}
}
13 changes: 3 additions & 10 deletions src/bin/update-downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::env;
use std::collections::HashMap;
use std::time::Duration;

use cargo_registry::{VersionDownload, Version, Model};
use cargo_registry::{VersionDownload, Version, Model, env};

static LIMIT: i64 = 1000;

Expand All @@ -31,13 +31,6 @@ fn main() {
}
}

fn env(s: &str) -> String {
match env::var(s).ok() {
Some(s) => s,
None => panic!("must have `{}` defined", s),
}
}

fn update(conn: &postgres::GenericConnection) -> postgres::Result<()> {
let mut max = 0;
loop {
Expand Down Expand Up @@ -148,10 +141,10 @@ mod test {
use postgres;
use semver;

use cargo_registry::{Version, Crate, User, Model};
use cargo_registry::{Version, Crate, User, Model, env};

fn conn() -> postgres::Connection {
postgres::Connection::connect(&::env("TEST_DATABASE_URL")[..],
postgres::Connection::connect(&env("TEST_DATABASE_URL")[..],
postgres::SslMode::None).unwrap()
}

Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
extern crate postgres as pg;
extern crate rustc_serialize;
extern crate curl;
extern crate dotenv;
extern crate flate2;
extern crate git2;
extern crate license_exprs;
Expand Down Expand Up @@ -188,3 +189,11 @@ pub fn now() -> time::Timespec {
pub fn encode_time(ts: time::Timespec) -> String {
time::at_utc(ts).rfc3339().to_string()
}

pub fn env(s: &str) -> String {
dotenv::dotenv().ok();
match ::std::env::var(s) {
Ok(s) => s,
Err(_) => panic!("must have `{}` defined", s),
}
}
2 changes: 2 additions & 0 deletions src/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate conduit;
extern crate conduit_middleware;
extern crate conduit_test;
extern crate curl;
extern crate dotenv;
extern crate git2;
extern crate postgres;
extern crate rustc_serialize;
Expand Down Expand Up @@ -72,6 +73,7 @@ mod version;
mod team;

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

Expand Down