| Crates.io | auto-env-generator |
| lib.rs | auto-env-generator |
| version | 0.1.0 |
| created_at | 2025-08-11 06:40:47.841139+00 |
| updated_at | 2025-08-11 06:40:47.841139+00 |
| description | Fast, parallel Rust tool for automatically scanning codebases and generating .env files based on detected environment variable usage |
| homepage | https://github.com/darkobyte/auto-env-generator |
| repository | https://github.com/darkobyte/auto-env-generator |
| max_upload_size | |
| id | 1789687 |
| size | 111,496 |
A fast, parallel Rust tool for automatically scanning your codebase and generating .env files based on detected environment variable usage. Uses efficient pattern matching with Aho-Corasick and memchr for sub-second performance even on large projects.
std::env::var(), env::var(), and dotenv::var() calls.env filesInstall the CLI tool:
cargo install auto-env-generator
Generate a .env file for your project:
# Scan current directory
autoenv generate
# Scan specific directory
autoenv generate ./my-rust-project
# Generate .env.example instead
autoenv generate -o .env.example
# Scan and list variables without generating file
autoenv scan
# Show verbose output
autoenv generate --verbose
Add to your Cargo.toml:
[dependencies]
auto-env-generator = "0.1"
Basic usage:
use auto_env_generator::generate_env_file;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Generate .env file for current directory
generate_env_file(".")?;
// Or scan a specific directory
generate_env_file("./my-project")?;
Ok(())
}
Advanced usage with configuration:
use auto_env_generator::{generate_env_file_with_config, Config};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = Config {
output: Some(".env.example".to_string()),
merge_existing: Some(false),
ignore: Some(vec!["DEBUG".to_string(), "TEST_MODE".to_string()]),
};
generate_env_file_with_config("./my-project", config)?;
Ok(())
}
The tool efficiently scans for these patterns:
// Standard library calls
let db_url = std::env::var("DATABASE_URL").unwrap();
let api_key = env::var("API_KEY").unwrap();
// dotenv crate calls
let secret = dotenv::var("SECRET_KEY").unwrap();
// With _os variants
let path = std::env::var_os("PATH").unwrap();
let home = env::var_os("HOME").unwrap();
let config = dotenv::var_os("CONFIG_PATH").unwrap();
// Various formatting styles
let var1 = std::env::var("VAR_1").unwrap();
let var2=env::var("VAR_2").ok();
let var3 = dotenv::var( "VAR_3" ).unwrap_or_default();
// Multi-line calls
let var4 = std::env::var(
"MULTILINE_VAR"
).unwrap();
Create an autoenv.toml file in your project root:
# Output file name (default: ".env")
output = ".env.example"
# Whether to merge with existing file without overwriting values (default: true)
merge_existing = true
# List of variable names to ignore
ignore = [
"HOME",
"PATH",
"USER",
"DEBUG",
"TEST_MODE"
]
Generate a sample config file:
autoenv init-config
generateGenerate a .env file by scanning Rust source files:
autoenv generate [DIRECTORY] [OPTIONS]
Options:
-o, --output <FILE> Output file name (default: .env)
-c, --config <CONFIG> Configuration file path
--no-merge Don't merge with existing file (overwrite instead)
--ignore <VARIABLE> Variables to ignore (can be used multiple times)
-v, --verbose Verbose output
Examples:
# Basic usage
autoenv generate
# Generate .env.example file
autoenv generate -o .env.example
# Ignore specific variables
autoenv generate --ignore DEBUG --ignore TEST_MODE
# Use custom config file
autoenv generate -c custom-config.toml
# Scan different directory with verbose output
autoenv generate ./backend --verbose
scanList found environment variables without generating a file:
autoenv scan [DIRECTORY] [OPTIONS]
Options:
-c, --config <CONFIG> Configuration file path
--ignore <VARIABLE> Variables to ignore
--show-locations Show file locations where variables were found
configShow current configuration:
autoenv config [OPTIONS]
Options:
-c, --config <CONFIG> Configuration file path
init-configCreate a sample configuration file:
autoenv init-config [FILE]
use auto_env_generator::*;
// Generate .env file with default settings
generate_env_file(path: &str) -> Result<()>
// Generate with custom configuration
generate_env_file_with_config(path: &str, config: Config) -> Result<()>
// Generate to custom output path
generate_env_file_to(scan_path: &str, output_path: &str) -> Result<()>
// Just scan and return found variables
scan_for_env_vars(path: &str) -> Result<HashSet<String>>
use auto_env_generator::{EnvScanner, Config};
// Create scanner with custom configuration
let config = Config {
output: Some(".env.production".to_string()),
merge_existing: Some(true),
ignore: Some(vec!["DEBUG".to_string()]),
};
let scanner = EnvScanner::with_config(config)?;
// Scan directory
let variables = scanner.scan_directory("./src")?;
// Generate file
scanner.generate_env_file(&variables, ".env.production")?;
// Load configuration from file
let config = EnvScanner::load_config("autoenv.toml")?;
Auto Environment Generator is designed for speed:
On a typical Rust project (500 files, ~50,000 lines):
Run benchmarks yourself:
cargo bench
.rs files, skipping target/ and hidden directories.env files.env fileUse in GitHub Actions to ensure your .env.example stays up-to-date:
name: Update .env.example
on: [push, pull_request]
jobs:
update-env:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Install auto-env-generator
run: cargo install auto-env-generator
- name: Generate .env.example
run: autoenv generate -o .env.example
- name: Check for changes
run: git diff --exit-code .env.example
Add to .pre-commit-config.yaml:
repos:
- repo: local
hooks:
- id: auto-env-generator
name: Update .env.example
entry: autoenv generate -o .env.example
language: system
files: '\.rs$'
pass_filenames: false
Use in build.rs:
fn main() {
auto_env_generator::generate_env_file(".").unwrap();
println!("cargo:rerun-if-changed=src/");
}
For a typical web application:
// src/main.rs
use std::env;
#[tokio::main]
async fn main() {
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let redis_url = env::var("REDIS_URL").unwrap_or_default();
let jwt_secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set");
let port = env::var("PORT").unwrap_or_else(|_| "3000".to_string());
// ... rest of your app
}
Running autoenv generate produces:
# Auto-generated environment variables
# Add your values below
DATABASE_URL=
JWT_SECRET=
PORT=
REDIS_URL=
Create autoenv.toml:
output = ".env.example"
merge_existing = true
ignore = ["HOME", "PATH", "USER"]
Running autoenv generate will ignore system variables and create .env.example.
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
git clone https://github.com/yourusername/auto-env-generator
cd auto-env-generator
cargo build
cargo test
cargo bench
# Unit tests
cargo test
# Integration tests
cargo test --test integration_tests
# Benchmarks
cargo bench
# Test CLI
cargo run --bin autoenv -- generate --help
See CHANGELOG.md for release history.
Licensed under the MIT License. See LICENSE for details.
Auto Environment Generator provides a robust, fast, and reliable solution that integrates seamlessly into any Rust workflow.