Skip to content

Commit

Permalink
feat: Create .env file if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
schpet committed Sep 24, 2024
1 parent 98da14f commit b82c4dc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub fn read_env_vars(file_path: &str) -> Result<HashMap<String, String>, std::io
let contents = fs::read_to_string(path)?;
Ok(parse_env_content(&contents))
} else {
// Create an empty .env file if it doesn't exist
fs::write(path, "")?;
Ok(HashMap::new())
}
}
Expand Down
15 changes: 4 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,10 @@ fn main() {

if !new_vars.is_empty() {
should_print = false; // Don't print all vars when setting new ones
let mut env_vars = match read_env_vars(&cli.file) {
Ok(result) => result,
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
HashMap::new()
} else {
eprintln!("Error reading .env file: {}", e);
process::exit(1);
}
}
};
let mut env_vars = read_env_vars(&cli.file).unwrap_or_else(|e| {
eprintln!("Error reading .env file: {}", e);
process::exit(1);
});

env_vars.extend(new_vars);

Expand Down

0 comments on commit b82c4dc

Please sign in to comment.