Skip to content

Commit

Permalink
Make it more usable
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita2206 committed Jan 28, 2019
1 parent d548f2a commit 9318f84
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 121 deletions.
106 changes: 0 additions & 106 deletions .travis.yml.back

This file was deleted.

123 changes: 123 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 @@ -8,3 +8,4 @@ license = "MIT"

[dependencies]
text_io = "0.1.7"
clap = "2.32.0"
43 changes: 28 additions & 15 deletions src/main.rs
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())))
};
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9318f84

Please sign in to comment.