env-setup — Environment Variable Manager
Scan your codebase for all referenced environment variables, generate .env.example, validate your current .env, and ensure secrets aren't committed.
Steps
1. Scan Codebase for Environment Variables
Search for env var references across all common patterns:
CODEBLOCK0
Windows PowerShell alternative:
CODEBLOCK1
2. Extract Variable Names
Parse grep output to extract unique variable names:
- -
process.env.DATABASE_URL → INLINECODE3 - INLINECODE4 →
SECRET_KEY (default: default) - INLINECODE7 → INLINECODE8
- INLINECODE9 → INLINECODE10
Deduplicate and sort alphabetically. Note which file and line each var is referenced in.
3. Classify Variables
Categorize each variable:
| Category | Pattern | Examples |
|---|
| 🔴 Secrets | INLINECODE11 , *SECRET*, *TOKEN*, *PASSWORD*, INLINECODE15 | INLINECODE16 , INLINECODE17 |
| 🟡 Service URLs |
*URL*,
*HOST*,
*ENDPOINT*,
*URI* |
DATABASE_URL,
REDIS_HOST |
| 🟢 Configuration |
*PORT*,
*ENV*,
*MODE*,
*LEVEL*,
*DEBUG* |
PORT,
NODE_ENV,
LOG_LEVEL |
| ⚪ Other | Everything else |
APP_NAME,
MAX_RETRIES |
4. Generate .env.example
Create .env.example with descriptions, categories, and safe defaults:
CODEBLOCK2
Rules:
- - Secrets get placeholder values (
change-me, your-xxx-here) - Config vars get sensible defaults
- Group by category with comment headers
- Add
🔴 SECRET warning on sensitive vars
5. Validate Current .env
If .env exists, compare against discovered variables:
CODEBLOCK3
6. Ensure .gitignore Safety
Check that .env is in .gitignore:
CODEBLOCK4
If not found, offer to add:
CODEBLOCK5
Also check git history for accidentally committed .env files:
CODEBLOCK6
If found, warn the user that secrets may be in git history and suggest git filter-branch or BFG Repo-Cleaner.
7. Output Summary
CODEBLOCK7
Edge Cases
- - Framework-specific env: Next.js uses
NEXT_PUBLIC_* (client-exposed); flag these distinctly - Docker env: Check
docker-compose.yml environment: section too - Multiple .env files:
.env.development, .env.production, .env.test — validate all - No .env exists: Generate both
.env.example and a starter INLINECODE51 - Interpolated vars:
${VAR:-default} in shell scripts — extract INLINECODE53
Error Handling
| Error | Resolution |
|---|
| No env vars found | Project may not use env vars — confirm with user |
| .env has syntax errors |
Flag lines that don't match
KEY=value pattern |
| Binary files in scan | Exclude with
--binary-files=without-match |
| Permission denied on .env | Check file permissions; may need elevated access |
Built by Clawb (SOVEREIGN) — more skills at [coming soon]
env-setup — 环境变量管理器
扫描你的代码库中所有引用的环境变量,生成 .env.example 文件,验证当前的 .env 文件,并确保机密信息不会被提交。
步骤
1. 扫描代码库中的环境变量
在所有常见模式中搜索环境变量引用:
bash
Node.js / JavaScript / TypeScript
grep -rn process\.env\.\w\+ --include=
.js --include=.ts --include=
.jsx --include=.tsx . | grep -v node_modules | grep -v dist
Python
grep -rn os\.environ\|os\.getenv\|environ\.get --include=*.py . | grep -v
pycache | grep -v .venv
Rust
grep -rn env::var\|env::var_os\|dotenv --include=*.rs . | grep -v target
Go
grep -rn os\.Getenv\|os\.LookupEnv\|viper\. --include=*.go . | grep -v vendor
Docker / docker-compose
grep -rn \${.
} --include=.yml --include=
.yaml docker-compose 2>/dev/null
配置文件中的通用 .env 引用
grep -rn env\. --include=
.toml --include=.yaml --include=*.yml . 2>/dev/null
Windows PowerShell 替代方案:
powershell
Get-ChildItem -Recurse -Include .js,.ts,.jsx,.tsx -Exclude node_modules,dist | Select-String process\.env\.\w+
Get-ChildItem -Recurse -Include *.py -Exclude pycache,.venv | Select-String os\.environ|os\.getenv
2. 提取变量名称
解析 grep 输出以提取唯一的变量名称:
- - process.env.DATABASEURL → DATABASEURL
- os.environ.get(SECRETKEY, default) → SECRETKEY(默认值:default)
- os.getenv(APIKEY) → APIKEY
- env::var(RUSTLOG) → RUSTLOG
去重并按字母顺序排序。记录每个变量被引用的文件和行号。
3. 分类变量
将每个变量分类:
| 类别 | 模式 | 示例 |
|---|
| 🔴 机密信息 | KEY、SECRET、TOKEN、PASSWORD、CREDENTIAL | APIKEY、JWTSECRET |
| 🟡 服务 URL |
URL、
HOST、
ENDPOINT、
URI | DATABASE
URL、REDISHOST |
| 🟢 配置 |
PORT、
ENV、
MODE、
LEVEL、
DEBUG | PORT、NODE
ENV、LOGLEVEL |
| ⚪ 其他 | 其他所有内容 | APP
NAME、MAXRETRIES |
4. 生成 .env.example
创建包含描述、分类和安全默认值的 .env.example 文件:
env
============================================
环境配置
由 env-setup 技能生成
============================================
--- 应用配置 ---
NODE_ENV=development
PORT=3000
LOG_LEVEL=info
--- 数据库 ---
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
--- 身份验证(🔴 机密信息 — 切勿提交真实值) ---
JWT_SECRET=change-me-in-production
API_KEY=your-api-key-here
--- 外部服务 ---
REDIS_URL=redis://localhost:6379
规则:
- - 机密信息使用占位符值(change-me、your-xxx-here)
- 配置变量使用合理的默认值
- 按类别分组,并添加注释标题
- 在敏感变量上添加 🔴 机密信息 警告
5. 验证当前 .env 文件
如果 .env 文件存在,则与发现的变量进行比较:
markdown
.env 验证报告
❌ 缺失(代码需要但 .env 中没有)
- - STRIPESECRETKEY — 在 src/billing.ts:14 中引用
- SMTP_PASSWORD — 在 src/email.ts:8 中引用
⚠️ 未使用(.env 中存在但代码中未引用)
- - OLDAPIENDPOINT — 可以安全删除
✅ 存在且被引用
- - DATABASEURL ✓
- PORT ✓
- NODEENV ✓
6. 确保 .gitignore 安全
检查 .env 是否在 .gitignore 中:
bash
grep -q ^\.env$\|^\.env\.\* .gitignore 2>/dev/null
如果未找到,提供添加建议:
环境文件
.env
.env.local
.env.*.local
同时检查 git 历史中是否有意外提交的 .env 文件:
bash
git log --all --diff-filter=A -- .env .env.local .env.production 2>/dev/null
如果找到,警告用户机密信息可能存在于 git 历史中,并建议使用 git filter-branch 或 BFG Repo-Cleaner。
7. 输出摘要
markdown
环境变量报告
4 |
| ❌ .env 中缺失 | 2 |
| ⚠️ .env 中未使用 | 1 |
| ✅ 正确配置 | 12 |
| .gitignore 保护 | ✅ |
边界情况
- - 框架特定的环境变量:Next.js 使用 NEXTPUBLIC*(客户端暴露);需明确标记
- Docker 环境变量:同时检查 docker-compose.yml 的 environment: 部分
- 多个 .env 文件:.env.development、.env.production、.env.test — 全部验证
- 没有 .env 文件:同时生成 .env.example 和初始 .env 文件
- 插值变量:Shell 脚本中的 ${VAR:-default} — 提取 VAR
错误处理
| 错误 | 解决方案 |
|---|
| 未找到环境变量 | 项目可能未使用环境变量 — 与用户确认 |
| .env 文件存在语法错误 |
标记不符合 KEY=value 模式的行 |
| 扫描中包含二进制文件 | 使用 --binary-files=without-match 排除 |
| .env 文件权限拒绝 | 检查文件权限;可能需要提升访问权限 |
由 Clawb (SOVEREIGN) 构建 — 更多技能即将推出