dotenvor 0.2.0

Small, fast `.env` parser and loader for Rust
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
436
437
438
use std::collections::BTreeMap;
use std::env;
use std::ffi::{OsStr, OsString};
#[cfg(unix)]
use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::process::{self, Command};

use dotenvor::{EnvLoader, Error, KeyParsingMode, SubstitutionMode, TargetEnv};

const DEFAULT_FILE: &str = ".env";

const HELP: &str = "\
dotenv - run commands with variables loaded from dotenv files

Usage:
  dotenv run [OPTIONS] -- COMMAND [ARGS...]
  dotenv run [OPTIONS] COMMAND [ARGS...]
  dotenv --help
  dotenv --version

Commands:
  run       Load dotenv files and execute a command
";

const RUN_HELP: &str = "\
dotenv run - load dotenv files and execute a command

Usage:
  dotenv run [OPTIONS] -- COMMAND [ARGS...]
  dotenv run [OPTIONS] COMMAND [ARGS...]

Options:
  -f, --file <PATH>       Dotenv file path. Repeat to load multiple files.
                          Defaults to .env.
  -i, --ignore            Ignore missing dotenv files.
      --ignore-missing    Alias for --ignore.
  -o, --override          Override existing environment variables.
      --overload          Alias for --override.
  -u, --search-upward     Search parent directories for relative dotenv files.
      --expand            Expand variable placeholders in values.
      --permissive-keys   Accept permissive key syntax.
  -v, --verbose           Print loader diagnostics to stderr.
  -q, --quiet             Suppress loader diagnostics.
  -h, --help              Show this help text.
";

#[derive(Debug, Clone, PartialEq, Eq)]
enum RunCommand {
    Help,
    Execute(RunOptions),
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct RunOptions {
    files: Vec<PathBuf>,
    required: bool,
    override_existing: bool,
    search_upward: bool,
    substitution_mode: SubstitutionMode,
    key_parsing_mode: KeyParsingMode,
    verbose: bool,
    quiet: bool,
    command: OsString,
    args: Vec<OsString>,
}

impl Default for RunOptions {
    fn default() -> Self {
        Self {
            files: Vec::new(),
            required: true,
            override_existing: false,
            search_upward: false,
            substitution_mode: SubstitutionMode::Disabled,
            key_parsing_mode: KeyParsingMode::Strict,
            verbose: false,
            quiet: false,
            command: OsString::new(),
            args: Vec::new(),
        }
    }
}

fn main() {
    process::exit(run(env::args_os()));
}

fn run(args: impl IntoIterator<Item = OsString>) -> i32 {
    let mut args = args.into_iter();
    let _bin = args.next();

    let Some(subcommand) = args.next() else {
        print_help();
        return 0;
    };

    let subcommand = subcommand.to_string_lossy();
    match subcommand.as_ref() {
        "-h" | "--help" | "help" => {
            print_help();
            0
        }
        "-V" | "--version" | "version" => {
            print_version();
            0
        }
        "run" => match parse_run_options(args.collect()) {
            Ok(RunCommand::Help) => {
                print_run_help();
                0
            }
            Ok(RunCommand::Execute(options)) => match execute_run(options) {
                Ok(code) => code,
                Err(err) => {
                    eprintln!("dotenv: {err}");
                    1
                }
            },
            Err(err) => {
                eprintln!("dotenv: {err}");
                eprintln!("Try `dotenv run --help`.");
                1
            }
        },
        unknown => {
            eprintln!("dotenv: unknown subcommand `{unknown}`");
            eprintln!("Try `dotenv --help`.");
            1
        }
    }
}

fn parse_run_options(args: Vec<OsString>) -> Result<RunCommand, String> {
    let mut options = RunOptions::default();
    let mut index = 0usize;
    while index < args.len() {
        if let Some(value) = split_long_option_value(&args[index], "--file=") {
            parse_file_value(&value, &mut options.files)?;
            index += 1;
            continue;
        }

        let token = args[index].to_string_lossy();
        match token.as_ref() {
            "--" => {
                index += 1;
                break;
            }
            "-h" | "--help" => return Ok(RunCommand::Help),
            "-f" | "--file" => {
                index += 1;
                let Some(value) = args.get(index) else {
                    return Err("missing value for `-f/--file`".to_owned());
                };
                parse_file_value(value, &mut options.files)?;
                index += 1;
            }
            "-i" | "--ignore" | "--ignore-missing" => {
                options.required = false;
                index += 1;
            }
            "-o" | "--override" | "--overload" => {
                options.override_existing = true;
                index += 1;
            }
            "-u" | "--search-upward" => {
                options.search_upward = true;
                index += 1;
            }
            "--expand" => {
                options.substitution_mode = SubstitutionMode::Expand;
                index += 1;
            }
            "--permissive-keys" => {
                options.key_parsing_mode = KeyParsingMode::Permissive;
                index += 1;
            }
            "-v" | "--verbose" => {
                options.verbose = true;
                index += 1;
            }
            "-q" | "--quiet" => {
                options.quiet = true;
                index += 1;
            }
            unknown if unknown.starts_with('-') => {
                return Err(format!("unknown option `{unknown}`"));
            }
            _ => break,
        }
    }

    let remaining = &args[index..];
    let Some((command, command_args)) = remaining.split_first() else {
        return Err("missing command after `run`".to_owned());
    };

    if options.files.is_empty() {
        options.files.push(PathBuf::from(DEFAULT_FILE));
    }

    options.command = command.clone();
    options.args = command_args.to_vec();
    Ok(RunCommand::Execute(options))
}

fn split_long_option_value(raw: &OsString, prefix: &str) -> Option<OsString> {
    let raw_bytes = raw.as_encoded_bytes();
    let prefix_bytes = prefix.as_bytes();
    if !raw_bytes.starts_with(prefix_bytes) {
        return None;
    }

    // SAFETY: `raw_bytes` came from `raw`, and `prefix` is ASCII, so the slice
    // starts at a valid boundary in Rust's platform string encoding.
    Some(unsafe {
        OsString::from_encoded_bytes_unchecked(raw_bytes[prefix_bytes.len()..].to_vec())
    })
}

fn parse_file_value(raw: &OsStr, files: &mut Vec<PathBuf>) -> Result<(), String> {
    if raw.as_encoded_bytes().is_empty() {
        return Err("`-f/--file` requires a path".to_owned());
    }

    files.push(PathBuf::from(raw));
    Ok(())
}

fn execute_run(options: RunOptions) -> Result<i32, String> {
    let entries = load_entries(&options).map_err(format_loader_error)?;
    let mut command = Command::new(&options.command);
    command.args(&options.args);

    for entry in entries {
        if !options.override_existing && env::var_os(&entry.key).is_some() {
            continue;
        }
        command.env(entry.key, entry.value);
    }

    execute_command(command, &options.command)
}

fn load_entries(options: &RunOptions) -> Result<Vec<dotenvor::Entry>, Error> {
    let target = if options.substitution_mode == SubstitutionMode::Expand {
        TargetEnv::from_memory(snapshot_process_env()?)
    } else {
        TargetEnv::memory()
    };

    let loader = EnvLoader::new()
        .paths(&options.files)
        .required(options.required)
        .override_existing(options.override_existing)
        .search_upward(options.search_upward)
        .substitution_mode(options.substitution_mode)
        .key_parsing_mode(options.key_parsing_mode)
        .verbose(options.verbose)
        .quiet(options.quiet)
        .target(target);
    loader.parse_only()
}

fn snapshot_process_env() -> Result<BTreeMap<String, String>, Error> {
    let mut snapshot = BTreeMap::new();
    for (key, value) in env::vars_os() {
        let key = key.into_string().map_err(|_| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "process environment contains a non-UTF-8 variable name; `dotenv run --expand` requires UTF-8 environment data",
            )
        })?;
        let value = value.into_string().map_err(|_| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!(
                    "process environment variable `{key}` value is not valid UTF-8; `dotenv run --expand` requires UTF-8 environment data"
                ),
            )
        })?;
        snapshot.insert(key, value);
    }
    Ok(snapshot)
}

#[cfg(unix)]
fn execute_command(mut command: Command, program: &OsString) -> Result<i32, String> {
    let err = command.exec();
    Err(format!(
        "failed to execute `{}`: {err}",
        program.to_string_lossy()
    ))
}

#[cfg(not(unix))]
fn execute_command(mut command: Command, program: &OsString) -> Result<i32, String> {
    let status = command
        .status()
        .map_err(|err| format!("failed to execute `{}`: {err}", program.to_string_lossy()))?;
    Ok(status.code().unwrap_or(1))
}

fn format_loader_error(err: Error) -> String {
    match err {
        Error::Io(io_err) => format!("I/O error: {io_err}"),
        Error::Parse(parse_err) => parse_err.to_string(),
        Error::InvalidEncoding(utf8_err) => format!("invalid UTF-8 input: {utf8_err}"),
    }
}

fn print_help() {
    println!("{HELP}");
}

fn print_run_help() {
    println!("{RUN_HELP}");
}

fn print_version() {
    println!("dotenv {}", env!("CARGO_PKG_VERSION"));
}

#[cfg(test)]
mod tests {
    use super::{RunCommand, RunOptions, parse_run_options};
    use dotenvor::{KeyParsingMode, SubstitutionMode};
    use std::ffi::OsString;
    use std::path::PathBuf;

    #[test]
    fn parse_run_uses_defaults() {
        let parsed = parse_run_options(vec![OsString::from("printenv"), OsString::from("FOO")])
            .expect("parse should succeed");
        let RunCommand::Execute(options) = parsed else {
            panic!("expected execute");
        };

        assert_eq!(options.files, vec![PathBuf::from(".env")]);
        assert!(options.required);
        assert!(!options.override_existing);
        assert!(!options.search_upward);
        assert_eq!(options.substitution_mode, SubstitutionMode::Disabled);
        assert_eq!(options.key_parsing_mode, KeyParsingMode::Strict);
        assert_eq!(options.command, OsString::from("printenv"));
        assert_eq!(options.args, vec![OsString::from("FOO")]);
    }

    #[test]
    fn parse_run_supports_repeated_files() {
        let parsed = parse_run_options(vec![
            OsString::from("-f"),
            OsString::from(".env.local"),
            OsString::from("--file"),
            OsString::from("custom.env"),
            OsString::from("--file=inline.env"),
            OsString::from("--"),
            OsString::from("printenv"),
            OsString::from("FOO"),
        ])
        .expect("parse should succeed");
        let RunCommand::Execute(options) = parsed else {
            panic!("expected execute");
        };

        assert_eq!(
            options.files,
            vec![
                PathBuf::from(".env.local"),
                PathBuf::from("custom.env"),
                PathBuf::from("inline.env"),
            ]
        );
    }

    #[test]
    fn parse_run_reports_missing_file_value() {
        let err = parse_run_options(vec![OsString::from("-f")]).expect_err("parse should fail");
        assert_eq!(err, "missing value for `-f/--file`");
    }

    #[test]
    fn parse_run_rejects_empty_file_path() {
        let err = parse_run_options(vec![
            OsString::from("-f"),
            OsString::from(""),
            OsString::from("printenv"),
            OsString::from("FOO"),
        ])
        .expect_err("parse should fail");
        assert_eq!(err, "`-f/--file` requires a path");
    }

    #[cfg(unix)]
    #[test]
    fn parse_run_preserves_non_utf8_file_paths() {
        use std::os::unix::ffi::OsStringExt;

        let direct = OsString::from_vec(vec![0xFF, b'.', b'e', b'n', b'v']);
        let mut inline = b"--file=".to_vec();
        inline.extend_from_slice(&[0xFE, b'.', b'e', b'n', b'v']);

        let parsed = parse_run_options(vec![
            OsString::from("-f"),
            direct.clone(),
            OsString::from_vec(inline),
            OsString::from("printenv"),
            OsString::from("FOO"),
        ])
        .expect("parse should succeed");
        let RunCommand::Execute(options) = parsed else {
            panic!("expected execute");
        };

        assert_eq!(
            options.files,
            vec![
                PathBuf::from(direct),
                PathBuf::from(OsString::from_vec(vec![0xFE, b'.', b'e', b'n', b'v'])),
            ]
        );
    }

    #[test]
    fn parse_run_help_short_circuits() {
        let parsed = parse_run_options(vec![OsString::from("--help")]).expect("parse should work");
        assert_eq!(parsed, RunCommand::Help);
    }

    #[test]
    fn run_options_default_matches_expected_behavior() {
        let options = RunOptions::default();
        assert!(options.required);
        assert!(!options.override_existing);
        assert!(!options.search_upward);
    }
}