Skip to content

Commit 217ec2c

Browse files
committed
feat(cli): support multiple --env-file arg
1 parent 69e1d7a commit 217ec2c

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

cli/args/flags.rs

+35-10
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ pub struct Flags {
613613
pub internal: InternalFlags,
614614
pub ignore: Vec<String>,
615615
pub import_map_path: Option<String>,
616-
pub env_file: Option<String>,
616+
pub env_file: Option<Vec<String>>,
617617
pub inspect_brk: Option<SocketAddr>,
618618
pub inspect_wait: Option<SocketAddr>,
619619
pub inspect: Option<SocketAddr>,
@@ -3764,6 +3764,7 @@ fn env_file_arg() -> Arg {
37643764
.default_missing_value(".env")
37653765
.require_equals(true)
37663766
.num_args(0..=1)
3767+
.action(ArgAction::Append)
37673768
}
37683769

37693770
fn reload_arg() -> Arg {
@@ -5470,7 +5471,8 @@ fn import_map_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
54705471
}
54715472

54725473
fn env_file_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
5473-
flags.env_file = matches.remove_one::<String>("env-file");
5474+
flags.env_file = matches.get_many::<String>("env-file")
5475+
.map(|values| values.cloned().collect());
54745476
}
54755477

54765478
fn reload_arg_parse(
@@ -7381,7 +7383,7 @@ mod tests {
73817383
allow_all: true,
73827384
..Default::default()
73837385
},
7384-
env_file: Some(".example.env".to_owned()),
7386+
env_file: Some(vec![".example.env".to_owned()]),
73857387
..Flags::default()
73867388
}
73877389
);
@@ -7475,7 +7477,7 @@ mod tests {
74757477
allow_all: true,
74767478
..Default::default()
74777479
},
7478-
env_file: Some(".example.env".to_owned()),
7480+
env_file: Some(vec![".example.env".to_owned()]),
74797481
unsafely_ignore_certificate_errors: Some(vec![]),
74807482
..Flags::default()
74817483
}
@@ -8123,7 +8125,7 @@ mod tests {
81238125
subcommand: DenoSubcommand::Run(RunFlags::new_default(
81248126
"script.ts".to_string(),
81258127
)),
8126-
env_file: Some(".env".to_owned()),
8128+
env_file: Some(vec![".env".to_owned()]),
81278129
code_cache_enabled: true,
81288130
..Flags::default()
81298131
}
@@ -8139,7 +8141,7 @@ mod tests {
81398141
subcommand: DenoSubcommand::Run(RunFlags::new_default(
81408142
"script.ts".to_string(),
81418143
)),
8142-
env_file: Some(".env".to_owned()),
8144+
env_file: Some(vec![".env".to_owned()]),
81438145
code_cache_enabled: true,
81448146
..Flags::default()
81458147
}
@@ -8172,7 +8174,7 @@ mod tests {
81728174
subcommand: DenoSubcommand::Run(RunFlags::new_default(
81738175
"script.ts".to_string(),
81748176
)),
8175-
env_file: Some(".another_env".to_owned()),
8177+
env_file: Some(vec![".another_env".to_owned()]),
81768178
code_cache_enabled: true,
81778179
..Flags::default()
81788180
}
@@ -8193,13 +8195,36 @@ mod tests {
81938195
subcommand: DenoSubcommand::Run(RunFlags::new_default(
81948196
"script.ts".to_string(),
81958197
)),
8196-
env_file: Some(".another_env".to_owned()),
8198+
env_file: Some(vec![".another_env".to_owned()]),
8199+
code_cache_enabled: true,
8200+
..Flags::default()
8201+
}
8202+
);
8203+
}
8204+
8205+
#[test]
8206+
fn run_multiple_env_file_defined() {
8207+
let r = flags_from_vec(svec![
8208+
"deno",
8209+
"run",
8210+
"--env-file",
8211+
"--env-file=.two_env",
8212+
"script.ts"
8213+
]);
8214+
assert_eq!(
8215+
r.unwrap(),
8216+
Flags {
8217+
subcommand: DenoSubcommand::Run(RunFlags::new_default(
8218+
"script.ts".to_string(),
8219+
)),
8220+
env_file: Some(vec![".env".to_owned(), ".two_env".to_owned()]),
81978221
code_cache_enabled: true,
81988222
..Flags::default()
81998223
}
82008224
);
82018225
}
82028226

8227+
82038228
#[test]
82048229
fn cache_multiple() {
82058230
let r =
@@ -8336,7 +8361,7 @@ mod tests {
83368361
allow_read: Some(vec![]),
83378362
..Default::default()
83388363
},
8339-
env_file: Some(".example.env".to_owned()),
8364+
env_file: Some(vec![".example.env".to_owned()]),
83408365
..Flags::default()
83418366
}
83428367
);
@@ -10011,7 +10036,7 @@ mod tests {
1001110036
unsafely_ignore_certificate_errors: Some(vec![]),
1001210037
v8_flags: svec!["--help", "--random-seed=1"],
1001310038
seed: Some(1),
10014-
env_file: Some(".example.env".to_owned()),
10039+
env_file: Some(vec![".example.env".to_owned()]),
1001510040
..Flags::default()
1001610041
}
1001710042
);

cli/args/mod.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ impl CliOptions {
11251125
}
11261126
}
11271127

1128-
pub fn env_file_name(&self) -> Option<&String> {
1128+
pub fn env_file_name(&self) -> Option<&Vec<String>> {
11291129
self.flags.env_file.as_ref()
11301130
}
11311131

@@ -1871,19 +1871,22 @@ pub fn config_to_deno_graph_workspace_member(
18711871
})
18721872
}
18731873

1874-
fn load_env_variables_from_env_file(filename: Option<&String>) {
1875-
let Some(env_file_name) = filename else {
1874+
fn load_env_variables_from_env_file(filename: Option<&Vec<String>>) {
1875+
let Some(env_file_names) = filename else {
18761876
return;
18771877
};
1878-
match from_filename(env_file_name) {
1879-
Ok(_) => (),
1880-
Err(error) => {
1881-
match error {
1878+
1879+
for env_file_name in env_file_names.iter().rev() {
1880+
match from_filename(env_file_name) {
1881+
Ok(_) => (),
1882+
Err(error) => {
1883+
match error {
18821884
dotenvy::Error::LineParse(line, index)=> log::info!("{} Parsing failed within the specified environment file: {} at index: {} of the value: {}",colors::yellow("Warning"), env_file_name, index, line),
18831885
dotenvy::Error::Io(_)=> log::info!("{} The `--env-file` flag was used, but the environment file specified '{}' was not found.",colors::yellow("Warning"),env_file_name),
18841886
dotenvy::Error::EnvVar(_)=> log::info!("{} One or more of the environment variables isn't present or not unicode within the specified environment file: {}",colors::yellow("Warning"),env_file_name),
18851887
_ => log::info!("{} Unknown failure occurred with the specified environment file: {}", colors::yellow("Warning"), env_file_name),
18861888
}
1889+
}
18871890
}
18881891
}
18891892
}

cli/standalone/binary.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,15 @@ impl<'a> DenoCompileBinaryWriter<'a> {
565565
};
566566

567567
let env_vars_from_env_file = match cli_options.env_file_name() {
568-
Some(env_filename) => {
569-
log::info!("{} Environment variables from the file \"{}\" were embedded in the generated executable file", crate::colors::yellow("Warning"), env_filename);
570-
get_file_env_vars(env_filename.to_string())?
568+
Some(env_filenames) => {
569+
let mut aggregated_env_vars = IndexMap::new();
570+
for env_filename in env_filenames.iter().rev() {
571+
log::info!("{} Environment variables from the file \"{}\" were embedded in the generated executable file", crate::colors::yellow("Warning"), env_filename);
572+
573+
let env_vars = get_file_env_vars(env_filename.to_string())?;
574+
aggregated_env_vars.extend(env_vars);
575+
}
576+
aggregated_env_vars
571577
}
572578
None => Default::default(),
573579
};

0 commit comments

Comments
 (0)