From 75cb795c768846adc15c9e471055bc365d972822 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Sat, 13 Feb 2016 14:36:03 -0700 Subject: [PATCH] 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. --- .env => .env.sample | 0 .gitignore | 1 + Cargo.lock | 9 +++++++++ Cargo.toml | 1 + src/bin/delete-crate.rs | 9 +-------- src/bin/delete-version.rs | 9 +-------- src/bin/migrate.rs | 8 +------- src/bin/populate.rs | 9 ++------- src/bin/server.rs | 8 +------- src/bin/update-downloads.rs | 13 +++---------- src/lib.rs | 9 +++++++++ src/tests/all.rs | 2 ++ 12 files changed, 31 insertions(+), 47 deletions(-) rename .env => .env.sample (100%) diff --git a/.env b/.env.sample similarity index 100% rename from .env rename to .env.sample diff --git a/.gitignore b/.gitignore index 3a9963e3d61..9a569b66ceb 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ /libpeerconnection.log npm-debug.log testem.log +.env diff --git a/Cargo.lock b/Cargo.lock index 05bcc08dcc0..5ad7d56184d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,6 +15,7 @@ dependencies = [ "conduit-static 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "conduit-test 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "curl 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)", + "dotenv 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -252,6 +253,14 @@ dependencies = [ "pkg-config 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "dotenv" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "regex 0.1.46 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "env_logger" version = "0.3.2" diff --git a/Cargo.toml b/Cargo.toml index 32aefa0b65d..20f307a8ec2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/bin/delete-crate.rs b/src/bin/delete-crate.rs index 520f4dd235b..3ef72fb22b2 100644 --- a/src/bin/delete-crate.rs +++ b/src/bin/delete-crate.rs @@ -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() { @@ -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 } diff --git a/src/bin/delete-version.rs b/src/bin/delete-version.rs index 6434d771236..e3bb7ffba30 100644 --- a/src/bin/delete-version.rs +++ b/src/bin/delete-version.rs @@ -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() { @@ -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 } diff --git a/src/bin/migrate.rs b/src/bin/migrate.rs index 02ea5c61da4..c873e776e06 100644 --- a/src/bin/migrate.rs +++ b/src/bin/migrate.rs @@ -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; @@ -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, diff --git a/src/bin/populate.rs b/src/bin/populate.rs index ce41a7bc159..44cc64db495 100644 --- a/src/bin/populate.rs +++ b/src/bin/populate.rs @@ -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")[..], @@ -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::().ok() diff --git a/src/bin/server.rs b/src/bin/server.rs index 483d708c344..3c6ebd2c134 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -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}; @@ -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), - } -} diff --git a/src/bin/update-downloads.rs b/src/bin/update-downloads.rs index b5886b66276..a0325c62dfd 100644 --- a/src/bin/update-downloads.rs +++ b/src/bin/update-downloads.rs @@ -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; @@ -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 { @@ -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() } diff --git a/src/lib.rs b/src/lib.rs index 093434ab022..3d01759d200 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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), + } +} diff --git a/src/tests/all.rs b/src/tests/all.rs index bb07703a37a..a74cf472d8e 100755 --- a/src/tests/all.rs +++ b/src/tests/all.rs @@ -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; @@ -72,6 +73,7 @@ mod version; mod team; fn app() -> (record::Bomb, Arc, conduit_middleware::MiddlewareBuilder) { + dotenv::dotenv().ok(); static INIT: Once = ONCE_INIT; git::init();