Auto Environment Generator
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.
Features
- ๐ Blazing Fast: Parallel scanning with Rayon + efficient pattern matching
- ๐ Smart Detection: Finds
std::env::var(),env::var(), anddotenv::var()calls - ๐ก๏ธ Safe Merging: Preserves existing values when merging with existing
.envfiles - โ๏ธ Configurable: TOML configuration with ignore lists and custom output paths
- ๐ฆ Library + CLI: Use as a library or standalone command-line tool
- ๐ฏ Zero Dependencies: Minimal runtime dependencies, maximum performance
- ๐งช Well Tested: Comprehensive test suite with benchmarks
Quick Start
CLI Usage
Install the CLI tool:
Generate a .env file for your project:
# Scan current directory
# Scan specific directory
# Generate .env.example instead
# Scan and list variables without generating file
# Show verbose output
Library Usage
Add to your Cargo.toml:
[]
= "0.1"
Basic usage:
use generate_env_file;
Advanced usage with configuration:
use ;
What It Detects
The tool efficiently scans for these patterns:
// Standard library calls
let db_url = var.unwrap;
let api_key = var.unwrap;
// dotenv crate calls
let secret = var.unwrap;
// With _os variants
let path = var_os.unwrap;
let home = var_os.unwrap;
let config = var_os.unwrap;
// Various formatting styles
let var1 = var.unwrap;
let var2=var.ok;
let var3 = var.unwrap_or_default;
// Multi-line calls
let var4 = var.unwrap;
Configuration
Create an autoenv.toml file in your project root:
# Output file name (default: ".env")
= ".env.example"
# Whether to merge with existing file without overwriting values (default: true)
= true
# List of variable names to ignore
= [
"HOME",
"PATH",
"USER",
"DEBUG",
"TEST_MODE"
]
Generate a sample config file:
CLI Commands
generate
Generate a .env file by scanning Rust source files:
)
Examples:
# Basic usage
# Generate .env.example file
# Ignore specific variables
# Use custom config file
# Scan different directory with verbose output
scan
List found environment variables without generating a file:
config
Show current configuration:
init-config
Create a sample configuration file:
Library API
Core Functions
use *;
// Generate .env file with default settings
generate_env_file // Generate with custom configuration
generate_env_file_with_config // Generate to custom output path
generate_env_file_to // Just scan and return found variables
scan_for_env_vars
Advanced Usage
use ;
// Create scanner with custom configuration
let config = Config ;
let scanner = with_config?;
// Scan directory
let variables = scanner.scan_directory?;
// Generate file
scanner.generate_env_file?;
// Load configuration from file
let config = load_config?;
Performance
Auto Environment Generator is designed for speed:
- Parallel Processing: Uses Rayon for parallel file processing
- Efficient Pattern Matching: Aho-Corasick algorithm for fast pattern detection
- Zero-Copy Reading: BufReader with minimal allocations
- Smart Filtering: Skips target directories and non-Rust files
Benchmarks
On a typical Rust project (500 files, ~50,000 lines):
- Scan Time: ~100-300ms
- Memory Usage: <50MB peak
- Throughput: >1000 files/second
Run benchmarks yourself:
How It Works
- File Discovery: Recursively finds all
.rsfiles, skippingtarget/and hidden directories - Parallel Scanning: Uses Rayon to process files in parallel
- Pattern Matching: Aho-Corasick automaton quickly finds potential env var calls
- Extraction: Regex extracts variable names from string literals
- Deduplication: HashSet ensures no duplicate variables
- Merge Logic: Intelligently merges with existing
.envfiles - Output: Generates sorted, commented
.envfile
Integration
CI/CD
Use in GitHub Actions to ensure your .env.example stays up-to-date:
name: Update .env.example
on:
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
Pre-commit Hook
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
Build Scripts
Use in build.rs:
Examples
Basic Web Server
For a typical web application:
// src/main.rs
use env;
async
Running autoenv generate produces:
# Auto-generated environment variables
# Add your values below
DATABASE_URL=
JWT_SECRET=
PORT=
REDIS_URL=
With Configuration
Create autoenv.toml:
= ".env.example"
= true
= ["HOME", "PATH", "USER"]
Running autoenv generate will ignore system variables and create .env.example.
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
Development Setup
Running Tests
# Unit tests
# Integration tests
# Benchmarks
# Test CLI
Changelog
See CHANGELOG.md for release history.
License
Licensed under the MIT License. See LICENSE for details.
Alternatives
- Manual scanning: Grep-based approaches are slower and less accurate
- IDE extensions: Limited to specific editors and don't integrate with CI/CD
- Custom scripts: Brittle and require maintenance
Auto Environment Generator provides a robust, fast, and reliable solution that integrates seamlessly into any Rust workflow.