zorath-env 0.3.6

Fast CLI for .env validation against JSON/YAML schemas. 14 types, secret detection, watch mode, remote schemas, export to shell/docker/k8s/json, health diagnostics, code scanning, auto-fix. CI-friendly. Language-agnostic single binary.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
mod config;
mod envfile;
mod presets;
mod remote;
mod schema;
mod secrets;
mod suggestions;
mod commands;

use clap::{Parser, Subcommand};
use clap_complete::Shell;
use config::Config;

#[derive(Parser, Debug)]
#[command(name="zenv", version, about="Validate .env files with a schema and generate docs.")]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand, Debug)]
enum Command {
    /// Validate .env against schema
    #[command(after_help = "\
Examples:
  zenv check                            Validate using defaults
  zenv check --schema custom.json       Use custom schema
  zenv check --detect-secrets true      Include secret detection
  zenv check --watch                    Watch for file changes
  zenv check --env .env.local           Validate specific env file
  zenv check --format json              JSON output for CI/CD

Security options for remote schemas:
  zenv check --schema https://... --verify-hash abc123...
  zenv check --schema https://... --ca-cert /path/to/ca.pem

Config file:
  Create .zenvrc in your project root to set defaults:
  {\"schema\": \"env.schema.json\", \"detect_secrets\": true}")]
    Check {
        /// Path to .env file (default: .env, or from .zenvrc)
        #[arg(long)]
        env: Option<String>,
        /// Path to schema file (default: env.schema.json, or from .zenvrc)
        #[arg(long)]
        schema: Option<String>,
        /// If set, missing .env is allowed (schema still validated against defaults/required rules)
        #[arg(long)]
        allow_missing_env: Option<bool>,
        /// Detect potential secrets in .env file (API keys, passwords, tokens)
        #[arg(long)]
        detect_secrets: Option<bool>,
        /// Skip cache when fetching remote schemas
        #[arg(long)]
        no_cache: Option<bool>,
        /// Watch for file changes and re-run validation
        #[arg(long, default_value_t = false)]
        watch: bool,
        /// Output format: text or json
        #[arg(long, default_value = "text")]
        format: String,
        /// Verify remote schema integrity with SHA-256 hash
        #[arg(long)]
        verify_hash: Option<String>,
        /// Custom CA certificate for enterprise TLS (PEM format)
        #[arg(long)]
        ca_cert: Option<String>,
    },

    /// Generate documentation from schema
    #[command(after_help = "\
Examples:
  zenv docs                             Generate markdown docs
  zenv docs --format json               Generate JSON output
  zenv docs --schema https://...        Use remote schema
  zenv docs --schema https://... --verify-hash abc123...")]
    Docs {
        /// Path to schema file (default: env.schema.json, or from .zenvrc)
        #[arg(long)]
        schema: Option<String>,
        /// Output format: markdown or json
        #[arg(long, default_value = "markdown")]
        format: String,
        /// Skip cache when fetching remote schemas
        #[arg(long)]
        no_cache: Option<bool>,
        /// Verify remote schema integrity with SHA-256 hash
        #[arg(long)]
        verify_hash: Option<String>,
        /// Custom CA certificate for enterprise TLS (PEM format)
        #[arg(long)]
        ca_cert: Option<String>,
    },

    /// Create a starter schema from .env.example or preset
    #[command(after_help = "\
Examples:
  zenv init                             Create schema from .env.example
  zenv init --example .env              Use .env as source
  zenv init --preset nextjs             Use Next.js preset
  zenv init --preset rails              Use Rails preset
  zenv init --list-presets              Show available presets

Available presets:
  nextjs, rails, django, fastapi, express, laravel")]
    Init {
        /// Source .env file to infer types from
        #[arg(long, default_value = ".env.example")]
        example: String,
        /// Path to schema file (default: env.schema.json, or from .zenvrc)
        #[arg(long)]
        schema: Option<String>,
        /// Use a framework preset (nextjs, rails, django, fastapi, express, laravel)
        #[arg(long)]
        preset: Option<String>,
        /// List available presets
        #[arg(long, default_value_t = false)]
        list_presets: bool,
    },

    /// Show version and optionally check for updates
    #[command(after_help = "\
Examples:
  zenv version                          Show current version
  zenv version --check-update           Check for newer version")]
    Version {
        /// Check crates.io for newer version
        #[arg(long, default_value_t = false)]
        check_update: bool,
    },

    /// Generate shell completions
    #[command(after_help = "\
Examples:
  zenv completions bash                 Generate bash completions
  zenv completions zsh                  Generate zsh completions
  zenv completions fish                 Generate fish completions
  zenv completions powershell           Generate PowerShell completions

Installation:
  bash:  zenv completions bash >> ~/.bashrc
  zsh:   zenv completions zsh >> ~/.zshrc
  fish:  zenv completions fish > ~/.config/fish/completions/zenv.fish")]
    Completions {
        /// Shell to generate completions for (bash, zsh, fish, powershell)
        #[arg(value_enum)]
        shell: Shell,
    },

    /// Generate .env.example from schema
    #[command(after_help = "\
Examples:
  zenv example                          Print to stdout
  zenv example --output .env.example    Write to file
  zenv example --include-defaults       Include schema defaults
  zenv example --schema https://... --verify-hash abc123...")]
    Example {
        /// Path to schema file (default: env.schema.json, or from .zenvrc)
        #[arg(long)]
        schema: Option<String>,
        /// Output file path (defaults to stdout)
        #[arg(long)]
        output: Option<String>,
        /// Include default values in output
        #[arg(long, default_value_t = false)]
        include_defaults: bool,
        /// Skip cache when fetching remote schemas
        #[arg(long)]
        no_cache: Option<bool>,
        /// Verify remote schema integrity with SHA-256 hash
        #[arg(long)]
        verify_hash: Option<String>,
        /// Custom CA certificate for enterprise TLS (PEM format)
        #[arg(long)]
        ca_cert: Option<String>,
    },

    /// Compare two .env files
    #[command(after_help = "\
Examples:
  zenv diff .env.local .env.prod        Compare two env files
  zenv diff .env.dev .env --schema s.json   With schema validation
  zenv diff .env.a .env.b --format json     JSON output for CI")]
    Diff {
        /// First .env file
        env_a: String,
        /// Second .env file
        env_b: String,
        /// Optional schema to check compliance
        #[arg(long)]
        schema: Option<String>,
        /// Output format: text or json
        #[arg(long, default_value = "text")]
        format: String,
        /// Skip cache when fetching remote schemas
        #[arg(long)]
        no_cache: Option<bool>,
        /// Verify remote schema integrity with SHA-256 hash
        #[arg(long)]
        verify_hash: Option<String>,
        /// Custom CA certificate for enterprise TLS (PEM format)
        #[arg(long)]
        ca_cert: Option<String>,
    },

    /// Auto-fix common .env issues
    #[command(after_help = "\
Examples:
  zenv fix --dry-run                    Preview fixes without changing files
  zenv fix                              Apply fixes (creates .env.backup)
  zenv fix --remove-unknown             Also remove keys not in schema

What it fixes:
  - Adds missing required variables (with schema defaults)
  - Removes unknown keys (with --remove-unknown)

What it reports but doesn't fix:
  - Invalid types (can't guess correct value)
  - Invalid enum values (needs human choice)")]
    Fix {
        /// Path to .env file (default: .env, or from .zenvrc)
        #[arg(long)]
        env: Option<String>,
        /// Path to schema file (default: env.schema.json, or from .zenvrc)
        #[arg(long)]
        schema: Option<String>,
        /// Remove keys not defined in schema
        #[arg(long, default_value_t = false)]
        remove_unknown: bool,
        /// Preview changes without modifying files
        #[arg(long, default_value_t = false)]
        dry_run: bool,
        /// Skip cache when fetching remote schemas
        #[arg(long)]
        no_cache: Option<bool>,
        /// Verify remote schema integrity with SHA-256 hash
        #[arg(long)]
        verify_hash: Option<String>,
        /// Custom CA certificate for enterprise TLS (PEM format)
        #[arg(long)]
        ca_cert: Option<String>,
    },

    /// Scan source code for environment variable usage
    #[command(after_help = "\
Examples:
  zenv scan                             Scan current directory
  zenv scan --path ./src                Scan specific directory
  zenv scan --show-unused               Show vars in schema but not in code
  zenv scan --show-paths                Show file:line for all found vars
  zenv scan --format json               JSON output for CI

Supported languages:
  JavaScript/TypeScript, Python, Go, Rust, PHP, Ruby, Java, C#, Kotlin")]
    Scan {
        /// Directory to scan
        #[arg(long, default_value = ".")]
        path: String,
        /// Path to schema file (default: env.schema.json, or from .zenvrc)
        #[arg(long)]
        schema: Option<String>,
        /// Show variables in schema but not found in code
        #[arg(long, default_value_t = false)]
        show_unused: bool,
        /// Show file:line paths for all found variables
        #[arg(long, default_value_t = false)]
        show_paths: bool,
        /// Output format: text or json
        #[arg(long, default_value = "text")]
        format: String,
        /// Skip cache when fetching remote schemas
        #[arg(long)]
        no_cache: Option<bool>,
        /// Verify remote schema integrity with SHA-256 hash
        #[arg(long)]
        verify_hash: Option<String>,
        /// Custom CA certificate for enterprise TLS (PEM format)
        #[arg(long)]
        ca_cert: Option<String>,
    },

    /// Manage remote schema cache
    #[command(after_help = "\
Examples:
  zenv cache list                       List cached schemas
  zenv cache clear                      Clear all cached schemas
  zenv cache clear https://...          Clear specific URL
  zenv cache path                       Show cache directory")]
    Cache {
        #[command(subcommand)]
        action: CacheAction,
    },

    /// Export .env to various formats
    #[command(after_help = "\
Examples:
  zenv export .env --format shell       Shell script (export FOO=\"bar\")
  zenv export .env --format docker      Dockerfile (ENV FOO=bar)
  zenv export .env --format k8s         Kubernetes ConfigMap YAML
  zenv export .env --format json        JSON object
  zenv export .env --format systemd     systemd Environment directives
  zenv export .env --format dotenv      Standard .env format

  zenv export .env --schema s.json      Only export vars defined in schema
  zenv export .env -f shell -o setup.sh Write to file

Aliases:
  shell: bash, sh
  docker: dockerfile
  k8s: kubernetes, configmap
  systemd: service
  dotenv: env")]
    Export {
        /// Path to .env file to export
        env: String,
        /// Output format (shell, docker, k8s, json, systemd, dotenv)
        #[arg(short = 'f', long, default_value = "shell")]
        format: String,
        /// Optional schema to filter variables
        #[arg(long)]
        schema: Option<String>,
        /// Output file path (defaults to stdout)
        #[arg(short = 'o', long)]
        output: Option<String>,
        /// Skip cache when fetching remote schemas
        #[arg(long)]
        no_cache: Option<bool>,
        /// Verify remote schema integrity with SHA-256 hash
        #[arg(long)]
        verify_hash: Option<String>,
        /// Custom CA certificate for enterprise TLS (PEM format)
        #[arg(long)]
        ca_cert: Option<String>,
    },

    /// Run health check and diagnostics
    #[command(after_help = "\
Examples:
  zenv doctor                           Run full health check

Checks:
  - Schema file exists and is valid
  - .env file exists and parses correctly
  - Config file (.zenvrc) is valid JSON
  - Remote schema cache is accessible
  - Validation passes (if schema and env exist)

Each check shows:
  [OK]    - No issues
  [WARN]  - Non-critical issue
  [ERROR] - Critical issue that needs attention")]
    Doctor,
}

#[derive(Subcommand, Debug)]
enum CacheAction {
    /// List cached remote schemas
    List,
    /// Clear cached schemas (all or specific URL)
    Clear {
        /// URL to clear from cache (omit to clear all)
        url: Option<String>,
    },
    /// Print cache directory path
    Path,
}

fn main() {
    let cli = Cli::parse();

    // Load config from .zenvrc (if present)
    let config = Config::load().unwrap_or_default();

    let result = match cli.command {
        Command::Check { env, schema, allow_missing_env, detect_secrets, no_cache, watch, format, verify_hash, ca_cert } => {
            // CLI args override config, config overrides defaults
            let env = env.unwrap_or_else(|| config.env_or(".env"));
            let schema = schema.unwrap_or_else(|| config.schema_or("env.schema.json"));
            let allow_missing_env = allow_missing_env.unwrap_or_else(|| config.allow_missing_env_or(true));
            let detect_secrets = detect_secrets.unwrap_or_else(|| config.detect_secrets_or(false));
            let no_cache = no_cache.unwrap_or_else(|| config.no_cache_or(false));
            let verify_hash = verify_hash.or_else(|| config.verify_hash());
            let ca_cert = ca_cert.or_else(|| config.ca_cert());
            commands::check::run(&env, &schema, allow_missing_env, detect_secrets, no_cache, watch, &format, verify_hash.as_deref(), ca_cert.as_deref())
        }
        Command::Docs { schema, format, no_cache, verify_hash, ca_cert } => {
            let schema = schema.unwrap_or_else(|| config.schema_or("env.schema.json"));
            let no_cache = no_cache.unwrap_or_else(|| config.no_cache_or(false));
            let verify_hash = verify_hash.or_else(|| config.verify_hash());
            let ca_cert = ca_cert.or_else(|| config.ca_cert());
            commands::docs::run(&schema, &format, no_cache, verify_hash.as_deref(), ca_cert.as_deref())
        }
        Command::Init { example, schema, preset, list_presets } => {
            if list_presets {
                println!("Available presets:");
                for name in presets::list_presets() {
                    println!("  {}", name);
                }
                Ok(())
            } else {
                let schema = schema.unwrap_or_else(|| config.schema_or("env.schema.json"));
                commands::init::run(&example, &schema, preset.as_deref())
            }
        }
        Command::Version { check_update } => commands::version::run(check_update),
        Command::Completions { shell } => commands::completions::run(shell),
        Command::Example { schema, output, include_defaults, no_cache, verify_hash, ca_cert } => {
            let schema = schema.unwrap_or_else(|| config.schema_or("env.schema.json"));
            let no_cache = no_cache.unwrap_or_else(|| config.no_cache_or(false));
            let verify_hash = verify_hash.or_else(|| config.verify_hash());
            let ca_cert = ca_cert.or_else(|| config.ca_cert());
            commands::example::run(&schema, output.as_deref(), include_defaults, no_cache, verify_hash.as_deref(), ca_cert.as_deref())
        }
        Command::Diff { env_a, env_b, schema, format, no_cache, verify_hash, ca_cert } => {
            // For diff, schema is optional so we don't apply config default
            let no_cache = no_cache.unwrap_or_else(|| config.no_cache_or(false));
            let verify_hash = verify_hash.or_else(|| config.verify_hash());
            let ca_cert = ca_cert.or_else(|| config.ca_cert());
            commands::diff::run(&env_a, &env_b, schema.as_deref(), &format, no_cache, verify_hash.as_deref(), ca_cert.as_deref())
        }
        Command::Fix { env, schema, remove_unknown, dry_run, no_cache, verify_hash, ca_cert } => {
            let env = env.unwrap_or_else(|| config.env_or(".env"));
            let schema = schema.unwrap_or_else(|| config.schema_or("env.schema.json"));
            let no_cache = no_cache.unwrap_or_else(|| config.no_cache_or(false));
            let verify_hash = verify_hash.or_else(|| config.verify_hash());
            let ca_cert = ca_cert.or_else(|| config.ca_cert());
            commands::fix::run(&env, &schema, remove_unknown, dry_run, no_cache, verify_hash.as_deref(), ca_cert.as_deref())
        }
        Command::Scan { path, schema, show_unused, show_paths, format, no_cache, verify_hash, ca_cert } => {
            let schema = schema.unwrap_or_else(|| config.schema_or("env.schema.json"));
            let no_cache = no_cache.unwrap_or_else(|| config.no_cache_or(false));
            let verify_hash = verify_hash.or_else(|| config.verify_hash());
            let ca_cert = ca_cert.or_else(|| config.ca_cert());
            commands::scan::run(&path, &schema, show_unused, show_paths, &format, no_cache, verify_hash.as_deref(), ca_cert.as_deref())
        }
        Command::Cache { action } => match action {
            CacheAction::List => commands::cache::run_list(),
            CacheAction::Clear { url } => commands::cache::run_clear(url.as_deref()),
            CacheAction::Path => commands::cache::run_path(),
        },
        Command::Export { env, format, schema, output, no_cache, verify_hash, ca_cert } => {
            let no_cache = no_cache.unwrap_or_else(|| config.no_cache_or(false));
            let verify_hash = verify_hash.or_else(|| config.verify_hash());
            let ca_cert = ca_cert.or_else(|| config.ca_cert());
            commands::export::run(&env, schema.as_deref(), &format, output.as_deref(), no_cache, verify_hash.as_deref(), ca_cert.as_deref())
        }
        Command::Doctor => commands::doctor::run(),
    };

    if let Err(e) = result {
        eprintln!("zenv error: {e}");
        std::process::exit(1);
    }
}