From 458ccc73c11530a4d4a74da9ab71aca4ae152812 Mon Sep 17 00:00:00 2001 From: Mamun Date: Wed, 17 Jan 2024 00:20:46 +0600 Subject: [PATCH 1/2] Added dry-run and key trimmer --- .env.example | 5 ----- src/io_manager.rs | 13 +++++++++---- src/main.rs | 20 ++++++++++++++------ test.env | 4 ++-- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/.env.example b/.env.example index 8e67e89..e69de29 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +0,0 @@ -HELLO=string -TAYLOR=string -AGE=int -SCORE=float -ACTIVE=bool \ No newline at end of file diff --git a/src/io_manager.rs b/src/io_manager.rs index 51ed9c4..f6915f0 100644 --- a/src/io_manager.rs +++ b/src/io_manager.rs @@ -36,7 +36,7 @@ fn read_dot_env(path: &str) -> Result, Error> { continue; } let mut split = line.split('='); - let key = split.next().unwrap(); + let key = split.next().unwrap().trim(); let val = split.next().unwrap(); env.insert(key.to_string(), clean_text(val)); } @@ -86,10 +86,15 @@ fn remove_comments(text: &str) -> String { text } -pub fn generate_dot_env_file(path: &str) -> Result<(), Error> { +pub fn generate_dot_env_file(dry_run: bool, path: &str) -> Result<(), Error> { let env = read_dot_env(path)?; - let env_string = generate_dot_env_string(env); + let mut env_string = generate_dot_env_string(env); + env_string.push_str("\n"); let mut file = File::create(".env.example")?; + if dry_run { + println!("{}", env_string.strip_suffix("\n").unwrap()); + return Ok(()); + } file.write_all(env_string.as_bytes())?; Ok(()) } @@ -141,7 +146,7 @@ mod tests { #[test] fn test_generate_dot_env_file() { - generate_dot_env_file("test.env").unwrap(); + generate_dot_env_file(false, "test.env").unwrap(); let mut file = File::open(".env.example").unwrap(); let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); diff --git a/src/main.rs b/src/main.rs index 051cb03..c3d03dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,15 @@ use io_manager::generate_dot_env_file; fn generate(val: &clap::ArgMatches) { let path = val.get_one::("path").unwrap(); - generate_dot_env_file(path).unwrap(); - println!( - "{} {}", - "✨ Successfully generated".green(), - ".env.example".bold() - ); + let dry_run: bool = val.get_flag("dry-run"); + generate_dot_env_file(dry_run, &path).unwrap(); + if !dry_run { + println!( + "{} {}", + "✨ Successfully generated".green(), + ".env.example".bold() + ); + } } fn main() { @@ -23,6 +26,11 @@ fn main() { .help("Path to the .env file to generate") .required(true) .action(ArgAction::Set), + ) + .arg( + clap::Arg::new("dry-run") + .long("dry-run") + .action(ArgAction::SetTrue), ); cmd = cmd.subcommand(sub_generate); let matches = cmd.get_matches(); diff --git a/test.env b/test.env index 9009e83..5667fe0 100644 --- a/test.env +++ b/test.env @@ -2,8 +2,8 @@ HELLO=ADELE TAYLOR="SWIFT" AGE=25 -SCORE=99.9 # Comment + SCORE=99.9 # Comment # Some Comment -ACTIVE=true +ACTIVE =true # From 51f93b484d70367b4eed4968f48b3c37d13a38af Mon Sep 17 00:00:00 2001 From: Mamun Date: Wed, 17 Jan 2024 00:22:20 +0600 Subject: [PATCH 2/2] Bumped version to 0.3.0 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7941df6..8eeb4d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -108,7 +108,7 @@ dependencies = [ [[package]] name = "envy" -version = "0.2.0" +version = "0.3.0" dependencies = [ "clap", "colored", diff --git a/Cargo.toml b/Cargo.toml index 5ac42cc..196fb8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "envy" -version = "0.2.0" +version = "0.3.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html