From fb6348500ffc827b3aaca3e8299e1a3964be43c1 Mon Sep 17 00:00:00 2001 From: HasanAlrimawi <141642411+HasanAlrimawi@users.noreply.github.com> Date: Wed, 10 Jul 2024 00:33:41 +0300 Subject: feat(compile): support --env (#24166) Supported the use of --env flag with the compile subcommand, so that the generated executable/binary file can access the passed env file. --- cli/standalone/binary.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'cli/standalone') diff --git a/cli/standalone/binary.rs b/cli/standalone/binary.rs index c9371d853..4d6b32b7d 100644 --- a/cli/standalone/binary.rs +++ b/cli/standalone/binary.rs @@ -2,6 +2,7 @@ use std::borrow::Cow; use std::collections::BTreeMap; +use std::collections::HashMap; use std::collections::VecDeque; use std::env::current_exe; use std::ffi::OsString; @@ -96,6 +97,7 @@ pub struct Metadata { pub ca_stores: Option>, pub ca_data: Option>, pub unsafely_ignore_certificate_errors: Option>, + pub env_vars_from_env_file: HashMap, pub workspace_resolver: SerializedWorkspaceResolver, pub entrypoint_key: String, pub node_modules: Option, @@ -584,6 +586,14 @@ impl<'a> DenoCompileBinaryWriter<'a> { } }; + let env_vars_from_env_file = match cli_options.env_file_name() { + Some(env_filename) => { + log::info!("{} Environment variables from the file \"{}\" were embedded in the generated executable file", crate::colors::yellow("Warning"), env_filename); + get_file_env_vars(env_filename.to_string())? + } + None => Default::default(), + }; + let metadata = Metadata { argv: compile_flags.args.clone(), seed: cli_options.seed(), @@ -596,6 +606,7 @@ impl<'a> DenoCompileBinaryWriter<'a> { log_level: cli_options.log_level(), ca_stores: cli_options.ca_stores().clone(), ca_data, + env_vars_from_env_file, entrypoint_key: root_dir_url.specifier_key(entrypoint).into_owned(), workspace_resolver: SerializedWorkspaceResolver { import_map: self.workspace_resolver.maybe_import_map().map(|i| { @@ -757,6 +768,21 @@ impl<'a> DenoCompileBinaryWriter<'a> { } } +/// This function returns the environment variables specified +/// in the passed environment file. +fn get_file_env_vars( + filename: String, +) -> Result, dotenvy::Error> { + let mut file_env_vars = HashMap::new(); + for item in dotenvy::from_filename_iter(filename)? { + let Ok((key, val)) = item else { + continue; // this failure will be warned about on load + }; + file_env_vars.insert(key, val); + } + Ok(file_env_vars) +} + /// This function sets the subsystem field in the PE header to 2 (GUI subsystem) /// For more information about the PE header: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format fn set_windows_binary_to_gui(bin: &mut [u8]) -> Result<(), AnyError> { -- cgit v1.2.3