-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d548f2a
commit 9318f84
Showing
4 changed files
with
152 additions
and
121 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ license = "MIT" | |
|
||
[dependencies] | ||
text_io = "0.1.7" | ||
clap = "2.32.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,38 @@ | ||
#[macro_use] extern crate text_io; | ||
extern crate clap; | ||
|
||
use std::io::{BufRead, BufReader, Error, ErrorKind, Write}; | ||
use std::fs::File; | ||
use std::fs::OpenOptions; | ||
use std::collections::HashMap; | ||
use std::env; | ||
use clap::{Arg, App}; | ||
|
||
fn main() -> std::io::Result<()> { | ||
const ENV_FILE: &str = ".env"; | ||
const ENV_DIST_FILE: &str = ".env.dist"; | ||
|
||
if env::args().any(|arg| arg.eq(&String::from("--help")) || arg.eq(&String::from("-h"))) { | ||
println!("usage: envpopulate [--quiet|-q]"); | ||
|
||
return Ok(()); | ||
} | ||
|
||
let quiet = env::args().any(|arg| arg.eq(&String::from("--quiet")) || arg.eq(&String::from("-q"))); | ||
|
||
let env_file = File::open(ENV_FILE); | ||
let env_dist_file = match File::open(ENV_DIST_FILE) { | ||
let args = App::new("envpopulate") | ||
.version("0.1.0") | ||
.author("Nikita Nefedov <[email protected]>") | ||
.about("Populate your .env file from distribution .env.dist interactively and incrementally") | ||
.arg(Arg::with_name("quiet") | ||
.short("q").long("quiet") | ||
.help("Don't ask for user input on each env-variable, just take the values from distribution file")) | ||
.arg(Arg::with_name("env") | ||
.long("env") | ||
.help("Custom filename of your .env file") | ||
.takes_value(true) | ||
.default_value(".env")) | ||
.arg(Arg::with_name("env-dist") | ||
.long("env-dist") | ||
.help("Custom filename of your .env.dist (distribution) file, this file will be taken as a source of defaults") | ||
.takes_value(true) | ||
.default_value(".env.dist")) | ||
.get_matches(); | ||
|
||
let env_file_name = args.value_of("env").unwrap_or(".env"); | ||
let env_dist_file_name = args.value_of("env-dist").unwrap_or(".env.dist"); | ||
let quiet = args.is_present("quiet"); | ||
|
||
let env_file = File::open(env_file_name); | ||
let env_dist_file = match File::open(env_dist_file_name) { | ||
Ok(f) => f, | ||
Err(err) => return Err(Error::new(ErrorKind::Other, format!("Couldn't open .env.dist: {}", err.to_string()))) | ||
}; | ||
|
@@ -58,7 +71,7 @@ fn main() -> std::io::Result<()> { | |
.write(true) | ||
.create(true) | ||
.append(true) | ||
.open(ENV_FILE)?; | ||
.open(env_file_name)?; | ||
|
||
for (key, value) in env_dist_entries.iter() { | ||
if ! env_entries.contains_key(key) { | ||
|