From 9318f845c2ae80327f2711dcef5aad8bf81bc6fc Mon Sep 17 00:00:00 2001 From: Nikita Nefedov Date: Tue, 29 Jan 2019 00:45:07 +0300 Subject: [PATCH] Make it more usable --- .travis.yml.back | 106 ---------------------------------------- Cargo.lock | 123 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 43 +++++++++++------ 4 files changed, 152 insertions(+), 121 deletions(-) delete mode 100644 .travis.yml.back diff --git a/.travis.yml.back b/.travis.yml.back deleted file mode 100644 index 24137dd..0000000 --- a/.travis.yml.back +++ /dev/null @@ -1,106 +0,0 @@ - -language: rust -rust: - - stable -cache: cargo - -matrix: - include: - - os: linux - rust: stable - env: TARGET=aarch64-unknown-linux-gnu - # need Trusty because the glibc in Precise is too old and doesn't support 64-bit arm - dist: trusty - sudo: required - # Extra packages only for this job - addons: - apt: - packages: &aarch64_unknown_linux_gnu - # Transparent emulation - - qemu-user-static - - binfmt-support - - os: linux - rust: stable - env: TARGET=armv7-unknown-linux-gnueabihf - # sudo is needed for binfmt_misc, which is needed for transparent user qemu emulation - sudo: required - addons: - apt: - packages: &armv7_unknown_linux_gnueabihf - # Cross compiler and cross compiled C libraries - - gcc-arm-linux-gnueabihf - - libc6-armhf-cross - - libc6-dev-armhf-cross - # Transparent emulation - - qemu-user-static - - binfmt-support - - os: osx - rust: stable - env: TARGET=i686-apple-darwin - - os: linux - rust: stable - env: TARGET=i686-unknown-linux-gnu - addons: - apt: - packages: &i686_unknown_linux_gnu - # Cross compiler and cross compiled C libraries - - gcc-multilib - - os: linux - rust: stable - env: TARGET=i686-unknown-linux-musl - dist: trusty - sudo: required - addons: - apt: - packages: &musl_packages - - musl - - musl-dev - - musl-tools - - os: osx - rust: stable - env: TARGET=x86_64-apple-darwin - - os: linux - rust: stable - env: TARGET=x86_64-unknown-linux-gnu - - os: linux - rust: stable - env: TARGET=x86_64-unknown-linux-musl - dist: trusty - sudo: required - addons: - apt: - packages: *musl_packages - -before_install: - - export PATH="$PATH:$HOME/.cargo/bin" - -install: - - bash ci/install.sh - -script: echo "Publishing binaries to GitHub releases" - -before_deploy: - - bash ci/before_deploy.sh - -deploy: - provider: releases - api_key: - secure: iAANKduP2lubHQ4v/CzdoMGVscvj/gmNrKnFtDLAQJy0xGddKV8zz7DDzPIxA+noZgRQygkjVKMbnjykITi4FDBox7mahFlz6vBf8W+j+JR9nh477TiGKd+AGLvqCAgL4kcsCyWqoSm8fnJx9QRz0tzDqwh1n5X9zk5B9oaORa5/Lh2oZ6G5+sdQDiS0zBrlGzAi3k94SZq76HBkE4gEgYu/tNre6HwLtafVRT5QG6EFlgioxgSOn4YlURyerDb+WKgp2WoAMbNG9j01HhkmzDXhxOl56g9Ta++VC5LgIOmV3MFV33vu9gSaT9r0ktgI4dqqcntJsFT/27H9pW2d7lC5a14L4uUYp6EPayWfPtgVU0v5B5YHHvdS5pJwwUK70O/iSmyBsNSfqe1RSaNYeQooICJmtU0yimN533liQy90P01RZbZPoKoxYO30m6CqZMPUYxhuS/VwXu+tAaYeKw3J21/Dd8dTBvNF9EaokWamIFoLdTHdXGaxMZOsntBAQbRebQVrZpAFBLlss4VLuxAD9Ywrtq4z+INztWgGuG4Fn9NX0ObECoAFJrNcC9n3GFx7Do9SxOYaW+cIbx6oGy8X28pR7KVPVjaRDnOlVyOTNbeWMEuPgZDxjhIAk5ggLglr/ML2TyySYYEDvw5nrGeFD6Zt0SM9RQ5Pys8nW+0= - skip_cleanup: true - on: - repo: nikita2206/envpopulate - file_glob: true - file: ${PROJECT_NAME}-${TRAVIS_TAG}-${TARGET}.* - -#jobs: -# include: -# - stage: GitHub Release -# deploy: -# provider: releases -# api_key: -# secure: iAANKduP2lubHQ4v/CzdoMGVscvj/gmNrKnFtDLAQJy0xGddKV8zz7DDzPIxA+noZgRQygkjVKMbnjykITi4FDBox7mahFlz6vBf8W+j+JR9nh477TiGKd+AGLvqCAgL4kcsCyWqoSm8fnJx9QRz0tzDqwh1n5X9zk5B9oaORa5/Lh2oZ6G5+sdQDiS0zBrlGzAi3k94SZq76HBkE4gEgYu/tNre6HwLtafVRT5QG6EFlgioxgSOn4YlURyerDb+WKgp2WoAMbNG9j01HhkmzDXhxOl56g9Ta++VC5LgIOmV3MFV33vu9gSaT9r0ktgI4dqqcntJsFT/27H9pW2d7lC5a14L4uUYp6EPayWfPtgVU0v5B5YHHvdS5pJwwUK70O/iSmyBsNSfqe1RSaNYeQooICJmtU0yimN533liQy90P01RZbZPoKoxYO30m6CqZMPUYxhuS/VwXu+tAaYeKw3J21/Dd8dTBvNF9EaokWamIFoLdTHdXGaxMZOsntBAQbRebQVrZpAFBLlss4VLuxAD9Ywrtq4z+INztWgGuG4Fn9NX0ObECoAFJrNcC9n3GFx7Do9SxOYaW+cIbx6oGy8X28pR7KVPVjaRDnOlVyOTNbeWMEuPgZDxjhIAk5ggLglr/ML2TyySYYEDvw5nrGeFD6Zt0SM9RQ5Pys8nW+0= -# skip_cleanup: true -# on: -# repo: nikita2206/envpopulate -# file_glob: true -# file: ${PROJECT_NAME}-${TRAVIS_TAG}-${TARGET}.* diff --git a/Cargo.lock b/Cargo.lock index c28ce94..a6439bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,14 +1,137 @@ +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "atty" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bitflags" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "clap" +version = "2.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "envpopulate" version = "0.1.0" dependencies = [ + "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "text_io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "libc" +version = "0.2.48" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_syscall" +version = "0.1.51" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_termios" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "strsim" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "termion" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "text_io" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "textwrap" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-width" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "vec_map" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] +"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" +"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" +"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" +"checksum libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)" = "e962c7641008ac010fa60a7dfdc1712449f29c44ef2d4702394aea943ee75047" +"checksum redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)" = "423e376fffca3dfa06c9e9790a9ccd282fafb3cc6e6397d01dbf64f9bacc6b85" +"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" +"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" +"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum text_io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9658b61ebd1d2a40c276ba2335890b9eb6550b67458a6fbce2022e58c3350a50" +"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" +"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" +"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" +"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 0e26da5..9b65178 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ license = "MIT" [dependencies] text_io = "0.1.7" +clap = "2.32.0" diff --git a/src/main.rs b/src/main.rs index a95c6e3..12bad2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 ") + .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) {