Skip to content

Commit

Permalink
Merge branch 'release/0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmmn378 committed Jan 16, 2024
2 parents 0d1630e + 51f93b4 commit 80b3e32
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 19 deletions.
5 changes: 0 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
HELLO=string
TAYLOR=string
AGE=int
SCORE=float
ACTIVE=bool
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/io_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn read_dot_env(path: &str) -> Result<IndexMap<String, String>, 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));
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 14 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use io_manager::generate_dot_env_file;

fn generate(val: &clap::ArgMatches) {
let path = val.get_one::<String>("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() {
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
HELLO=ADELE
TAYLOR="SWIFT"
AGE=25
SCORE=99.9 # Comment
SCORE=99.9 # Comment
# Some Comment
ACTIVE=true
ACTIVE =true

#

0 comments on commit 80b3e32

Please sign in to comment.