zorath-env 0.3.9

Fast CLI for .env validation against JSON/YAML schemas. 14 types, secret detection, watch mode, remote schemas, 7 export formats, CI templates, health diagnostics, code scanning, auto-fix. Language-agnostic single binary.
Documentation
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
use std::path::Path;

use crate::envfile;
use crate::errors::CliError;
use crate::presets;
use crate::schema::{self, Schema, VarSpec, VarType};

#[doc(hidden)]
pub fn run(example_path: &str, schema_path: &str, preset: Option<&str>) -> Result<(), CliError> {
    run_with_options(example_path, schema_path, preset, false)
}

#[doc(hidden)]
pub fn run_with_options(example_path: &str, schema_path: &str, preset: Option<&str>, list_presets: bool) -> Result<(), CliError> {
    if list_presets {
        println!("Available presets:");
        for name in presets::AVAILABLE_PRESETS {
            println!("  {}", name);
        }
        return Ok(());
    }

    // Start with preset schema if provided
    let mut schema_map: Schema = if let Some(preset_name) = preset {
        match presets::get_preset(preset_name) {
            Some(preset_schema) => {
                println!("zenv: using '{}' preset", preset_name);
                preset_schema
            }
            None => {
                return Err(CliError::Input(format!(
                    "unknown preset '{}'. Available: {}",
                    preset_name,
                    presets::AVAILABLE_PRESETS.join(", ")
                )));
            }
        }
    } else {
        Schema::new()
    };

    // If example file exists, merge inferred types (inferred takes precedence for existing keys)
    if Path::new(example_path).exists() {
        let env = envfile::parse_env_file(example_path).map_err(|e| CliError::Input(e.to_string()))?;

        for (k, v) in env {
            // Only add if not already in preset
            schema_map.entry(k.clone()).or_insert_with(|| {
                let inferred = infer_type(&v);
                let description = infer_description(&k, &inferred);

                VarSpec {
                    var_type: inferred,
                    required: true,
                    description: Some(description),
                    values: None,
                    default: None,
                    validate: None,
                    secret: None,
                    ..Default::default()
                }
            });
        }
    } else if preset.is_none() {
        // No preset and no example file - provide helpful suggestion
        return Err(CliError::Input(format!(
            "example file not found: {example_path}\n\n\
            To create a schema, either:\n  \
            1. Create a {} file with your environment variables\n  \
            2. Use a framework preset: zenv init --preset <name>\n\n\
            Available presets: nextjs, rails, django, fastapi, express, laravel\n\
            List presets with: zenv init --list-presets",
            example_path
        )));
    }

    if Path::new(schema_path).exists() {
        eprintln!("warning: overwriting existing {schema_path}");
    }

    schema::save_schema(schema_path, &schema_map).map_err(|e| CliError::Schema(e.to_string()))?;
    println!("zenv: wrote schema to {schema_path} ({} variables)", schema_map.len());
    Ok(())
}

fn infer_type(v: &str) -> VarType {
    let lv = v.trim().to_lowercase();

    if lv == "true" || lv == "false" || lv == "1" || lv == "0" || lv == "yes" || lv == "no" {
        return VarType::Bool;
    }
    if v.parse::<i64>().is_ok() {
        return VarType::Int;
    }
    if v.parse::<f64>().is_ok() {
        return VarType::Float;
    }
    if url::Url::parse(v).is_ok() {
        return VarType::Url;
    }
    VarType::String
}

/// Infer a description from the variable name and type
fn infer_description(key: &str, var_type: &VarType) -> String {
    let lower_key = key.to_lowercase();

    // Database-related
    if lower_key.contains("database") || lower_key.contains("db_") || lower_key == "db" {
        return "Database connection string".to_string();
    }
    if lower_key.contains("redis") {
        return "Redis connection URL".to_string();
    }
    if lower_key.contains("mongo") {
        return "MongoDB connection URL".to_string();
    }

    // URL patterns
    if lower_key.ends_with("_url") || lower_key.ends_with("_uri") {
        let service = extract_service_name(key);
        return format!("{} endpoint URL", service);
    }
    if lower_key.ends_with("_endpoint") {
        let service = extract_service_name(key);
        return format!("{} API endpoint", service);
    }

    // Authentication
    if lower_key.contains("api_key") || lower_key.contains("apikey") {
        let service = extract_service_name(key);
        return format!("{} API key", service);
    }
    if lower_key.contains("secret_key") || lower_key.contains("secretkey") {
        let service = extract_service_name(key);
        return format!("{} secret key", service);
    }
    if lower_key.contains("access_key") {
        let service = extract_service_name(key);
        return format!("{} access key", service);
    }
    if lower_key.contains("_token") || lower_key.ends_with("token") {
        let service = extract_service_name(key);
        return format!("{} authentication token", service);
    }
    if lower_key.contains("password") || lower_key.contains("passwd") {
        return "Password credential".to_string();
    }
    if lower_key.contains("_secret") || lower_key.ends_with("secret") {
        let service = extract_service_name(key);
        return format!("{} secret", service);
    }

    // Environment
    if lower_key == "node_env" || lower_key == "app_env" || lower_key == "environment" || lower_key == "env" {
        return "Application environment (e.g., development, staging, production)".to_string();
    }

    // Common patterns
    if lower_key.contains("port") {
        return "Port number for network service".to_string();
    }
    if lower_key.contains("host") && !lower_key.contains("_url") {
        return "Hostname or IP address".to_string();
    }
    if lower_key.contains("timeout") {
        return "Timeout duration in milliseconds".to_string();
    }
    if lower_key.contains("max_") || lower_key.contains("_max") {
        return format!("Maximum {} limit", key.replace('_', " ").to_lowercase());
    }
    if lower_key.contains("min_") || lower_key.contains("_min") {
        return format!("Minimum {} threshold", key.replace('_', " ").to_lowercase());
    }
    if lower_key.contains("enable") || lower_key.contains("disable") {
        return format!("Toggle for {}", key.replace('_', " ").to_lowercase());
    }
    if lower_key.starts_with("debug") {
        return "Enable debug mode".to_string();
    }
    if lower_key.contains("log_level") || lower_key == "loglevel" {
        return "Logging verbosity level".to_string();
    }
    if lower_key.contains("path") || lower_key.contains("dir") || lower_key.contains("directory") {
        return "File system path".to_string();
    }

    // Type-based fallback
    match var_type {
        VarType::Url => format!("{} URL", humanize_key(key)),
        VarType::Int => format!("{} (integer)", humanize_key(key)),
        VarType::Float => format!("{} (decimal)", humanize_key(key)),
        VarType::Bool => format!("{} flag", humanize_key(key)),
        _ => format!("{} configuration", humanize_key(key)),
    }
}

/// Extract service name from a key like STRIPE_API_KEY -> Stripe
fn extract_service_name(key: &str) -> String {
    let parts: Vec<&str> = key.split('_').collect();
    if parts.is_empty() {
        return key.to_string();
    }

    // Common suffixes to strip
    let suffixes = ["API", "KEY", "SECRET", "TOKEN", "URL", "URI", "ENDPOINT", "HOST", "PORT"];

    // Find the first part that's not a common suffix
    for part in &parts {
        let upper = part.to_uppercase();
        if !suffixes.contains(&upper.as_str()) && !part.is_empty() {
            // Capitalize first letter, lowercase rest
            let mut chars = part.chars();
            return match chars.next() {
                None => String::new(),
                Some(first) => first.to_uppercase().collect::<String>() + &chars.as_str().to_lowercase(),
            };
        }
    }

    humanize_key(key)
}

/// Convert KEY_NAME to "Key name"
fn humanize_key(key: &str) -> String {
    let result = key
        .split('_')
        .filter(|s| !s.is_empty())
        .map(|s| {
            let mut chars = s.chars();
            match chars.next() {
                None => String::new(),
                Some(first) => first.to_uppercase().collect::<String>() + &chars.as_str().to_lowercase(),
            }
        })
        .collect::<Vec<String>>()
        .join(" ");

    if result.is_empty() {
        key.to_string()
    } else {
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Bool inference tests
    #[test]
    fn test_infer_bool_true() {
        assert!(matches!(infer_type("true"), VarType::Bool));
    }

    #[test]
    fn test_infer_bool_false() {
        assert!(matches!(infer_type("false"), VarType::Bool));
    }

    #[test]
    fn test_infer_bool_one() {
        assert!(matches!(infer_type("1"), VarType::Bool));
    }

    #[test]
    fn test_infer_bool_zero() {
        assert!(matches!(infer_type("0"), VarType::Bool));
    }

    #[test]
    fn test_infer_bool_yes() {
        assert!(matches!(infer_type("yes"), VarType::Bool));
    }

    #[test]
    fn test_infer_bool_no() {
        assert!(matches!(infer_type("no"), VarType::Bool));
    }

    #[test]
    fn test_infer_bool_case_insensitive() {
        assert!(matches!(infer_type("TRUE"), VarType::Bool));
        assert!(matches!(infer_type("False"), VarType::Bool));
        assert!(matches!(infer_type("YES"), VarType::Bool));
    }

    #[test]
    fn test_infer_bool_with_whitespace() {
        assert!(matches!(infer_type("  true  "), VarType::Bool));
    }

    // Int inference tests
    #[test]
    fn test_infer_int_positive() {
        assert!(matches!(infer_type("42"), VarType::Int));
    }

    #[test]
    fn test_infer_int_negative() {
        assert!(matches!(infer_type("-42"), VarType::Int));
    }

    #[test]
    fn test_infer_int_large() {
        assert!(matches!(infer_type("9999999999"), VarType::Int));
    }

    #[test]
    fn test_infer_int_port() {
        assert!(matches!(infer_type("3000"), VarType::Int));
    }

    // Float inference tests
    #[test]
    fn test_infer_float_decimal() {
        assert!(matches!(infer_type("3.14"), VarType::Float));
    }

    #[test]
    fn test_infer_float_negative() {
        assert!(matches!(infer_type("-0.5"), VarType::Float));
    }

    #[test]
    fn test_infer_float_scientific() {
        assert!(matches!(infer_type("1.5e10"), VarType::Float));
    }

    // URL inference tests
    #[test]
    fn test_infer_url_https() {
        assert!(matches!(infer_type("https://example.com"), VarType::Url));
    }

    #[test]
    fn test_infer_url_http() {
        assert!(matches!(infer_type("http://localhost:3000"), VarType::Url));
    }

    #[test]
    fn test_infer_url_postgres() {
        assert!(matches!(infer_type("postgres://user:pass@host/db"), VarType::Url));
    }

    #[test]
    fn test_infer_url_redis() {
        assert!(matches!(infer_type("redis://localhost:6379"), VarType::Url));
    }

    // String inference tests (default)
    #[test]
    fn test_infer_string_plain_text() {
        assert!(matches!(infer_type("hello world"), VarType::String));
    }

    #[test]
    fn test_infer_string_env_name() {
        assert!(matches!(infer_type("development"), VarType::String));
    }

    #[test]
    fn test_infer_string_api_key() {
        assert!(matches!(infer_type("sk_test_abc123xyz"), VarType::String));
    }

    #[test]
    fn test_infer_string_path() {
        assert!(matches!(infer_type("/var/log/app.log"), VarType::String));
    }

    #[test]
    fn test_infer_string_empty() {
        assert!(matches!(infer_type(""), VarType::String));
    }

    // Edge cases - priority order verification
    #[test]
    fn test_bool_takes_priority_over_int() {
        // "1" and "0" should be Bool, not Int
        assert!(matches!(infer_type("1"), VarType::Bool));
        assert!(matches!(infer_type("0"), VarType::Bool));
    }

    #[test]
    fn test_int_takes_priority_over_float() {
        // "42" parses as both i64 and f64, but should be Int
        assert!(matches!(infer_type("42"), VarType::Int));
    }

    // Description inference tests
    #[test]
    fn test_infer_description_database_url() {
        let desc = infer_description("DATABASE_URL", &VarType::Url);
        assert_eq!(desc, "Database connection string");
    }

    #[test]
    fn test_infer_description_api_key() {
        let desc = infer_description("STRIPE_API_KEY", &VarType::String);
        assert_eq!(desc, "Stripe API key");
    }

    #[test]
    fn test_infer_description_port() {
        let desc = infer_description("PORT", &VarType::Int);
        assert_eq!(desc, "Port number for network service");
    }

    #[test]
    fn test_infer_description_node_env() {
        let desc = infer_description("NODE_ENV", &VarType::String);
        assert_eq!(desc, "Application environment (e.g., development, staging, production)");
    }

    #[test]
    fn test_infer_description_generic_url() {
        let desc = infer_description("WEBHOOK_URL", &VarType::Url);
        assert_eq!(desc, "Webhook endpoint URL");
    }

    #[test]
    fn test_humanize_key() {
        assert_eq!(humanize_key("DATABASE_URL"), "Database Url");
        assert_eq!(humanize_key("API_KEY"), "Api Key");
        assert_eq!(humanize_key("PORT"), "Port");
    }

    #[test]
    fn test_extract_service_name() {
        assert_eq!(extract_service_name("STRIPE_API_KEY"), "Stripe");
        assert_eq!(extract_service_name("AWS_SECRET_KEY"), "Aws");
        assert_eq!(extract_service_name("GITHUB_TOKEN"), "Github");
    }
}