Skip to content

Latest commit

 

History

History
163 lines (124 loc) · 3.52 KB

File metadata and controls

163 lines (124 loc) · 3.52 KB

env-diff

A zero-dependency Python tool for comparing, validating, and managing .env files. Diff environments, compare servers, validate against schemas, and generate .env.example files.

Features

  • Diff two .env files (show added, removed, changed keys)
  • Compare environment variables between servers via SSH
  • Export current environment to a file
  • Validate .env against a JSON schema (required keys, types, allowed values, patterns)
  • Generate .env.example from .env (strips sensitive values automatically)
  • Merge .env files with conflict resolution strategies

Installation

Copy env_diff.py into your project or add it to your PATH. No dependencies required (Python 3.7+).

chmod +x env_diff.py

Usage

Diff two .env files

python env_diff.py diff .env.staging .env.production
--- Removed (in .env.staging but not .env.production) ---
  - DEBUG=***

+++ Added (in .env.production but not .env.staging) ---
  + SENTRY_DSN=***

~~~ Changed ---
  ~ DATABASE_URL (values differ)

Summary: 1 added, 1 removed, 1 changed, 8 unchanged

Show actual values:

python env_diff.py diff .env.staging .env.production --show-values

Compare servers via SSH

python env_diff.py compare user@staging-server user@prod-server
python env_diff.py compare server1 server2 --filter "DATABASE,REDIS,API"

Export current environment

python env_diff.py export -o env_snapshot.txt
python env_diff.py export --filter "NODE,npm,PATH" -o node_env.txt

Validate against schema

python env_diff.py validate .env schema.json

schema.json:

{
  "required": ["DATABASE_URL", "API_KEY", "NODE_ENV"],
  "properties": {
    "DATABASE_URL": {
      "type": "url",
      "required": true
    },
    "API_KEY": {
      "type": "string",
      "min_length": 20
    },
    "NODE_ENV": {
      "type": "string",
      "allowed": ["development", "staging", "production"]
    },
    "PORT": {
      "type": "integer"
    },
    "DEBUG": {
      "type": "boolean"
    },
    "ADMIN_EMAIL": {
      "type": "email"
    },
    "WEBHOOK_URL": {
      "type": "url",
      "pattern": "^https://"
    }
  },
  "allow_extra": true
}
VALIDATION PASSED
0 errors, 0 warnings

Generate .env.example

python env_diff.py example .env
python env_diff.py example .env -o .env.example
python env_diff.py example .env --keep-values  # keep non-sensitive values

Smart placeholders:

DATABASE_URL=https://example.com
API_KEY=your-secret-here
PORT=3000
DEBUG=false
ADMIN_EMAIL=user@example.com

Merge .env files

# Overlay wins on conflicts (default)
python env_diff.py merge .env.defaults .env.local -o .env

# Base wins on conflicts
python env_diff.py merge .env.defaults .env.local -s base -o .env

# Ask interactively on each conflict
python env_diff.py merge .env.defaults .env.local -s ask -o .env

Schema Format

The validation schema is a JSON file with these fields:

Field Description
required Array of required key names
properties Object mapping key names to validation rules
allow_extra Boolean, whether to warn on unknown keys (default: true)

Property rules

Rule Description
type Expected type: string, integer, number, boolean, url, email
required Boolean, whether this key is required
allowed Array of allowed values
pattern Regex pattern the value must match
min_length Minimum value length

License

MIT