mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
src: add --env-file-if-exists flag
Fixes: https://github.com/nodejs/node/issues/50993 Refs: https://github.com/nodejs/node/issues/51451 test: remove unnecessary comment src: conform to style guidelines src: change flag to `--env-file-optional` test: revert automatic linter changes doc: fix typos src: change flag to `--env-file-if-exists` src: refactor `env_file_data` and `GetEnvFileDataFromArgs` test: clean up tests src: print error when file not found test: remove unnecessary extras PR-URL: https://github.com/nodejs/node/pull/53060 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
1 parent
9195210399
commit
53ede878a5
7 files changed
+118
-35
No files matched your search
+12
-6
@@ -851,20 +851,26 @@ static ExitCode InitializeNodeWithArgsInternal(
|
||||
HandleEnvOptions(per_process::cli_options->per_isolate->per_env);
|
||||
|
||||
std::string node_options;
|
||||
auto file_paths = node::Dotenv::GetPathFromArgs(*argv);
|
||||
auto env_files = node::Dotenv::GetDataFromArgs(*argv);
|
||||
|
||||
if (!file_paths.empty()) {
|
||||
if (!env_files.empty()) {
|
||||
CHECK(!per_process::v8_initialized);
|
||||
|
||||
for (const auto& file_path : file_paths) {
|
||||
switch (per_process::dotenv_file.ParsePath(file_path)) {
|
||||
for (const auto& file_data : env_files) {
|
||||
switch (per_process::dotenv_file.ParsePath(file_data.path)) {
|
||||
case Dotenv::ParseResult::Valid:
|
||||
break;
|
||||
case Dotenv::ParseResult::InvalidContent:
|
||||
errors->push_back(file_path + ": invalid format");
|
||||
errors->push_back(file_data.path + ": invalid format");
|
||||
break;
|
||||
case Dotenv::ParseResult::FileError:
|
||||
errors->push_back(file_path + ": not found");
|
||||
if (file_data.is_optional) {
|
||||
fprintf(stderr,
|
||||
"%s not found. Continuing without it.\n",
|
||||
file_data.path.c_str());
|
||||
continue;
|
||||
}
|
||||
errors->push_back(file_data.path + ": not found");
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
|
||||
+32
-16
@@ -11,36 +11,52 @@ using v8::NewStringType;
|
||||
using v8::Object;
|
||||
using v8::String;
|
||||
|
||||
std::vector<std::string> Dotenv::GetPathFromArgs(
|
||||
std::vector<Dotenv::env_file_data> Dotenv::GetDataFromArgs(
|
||||
const std::vector<std::string>& args) {
|
||||
const std::string_view optional_env_file_flag = "--env-file-if-exists";
|
||||
|
||||
const auto find_match = [](const std::string& arg) {
|
||||
return arg == "--" || arg == "--env-file" || arg.starts_with("--env-file=");
|
||||
return arg == "--" || arg == "--env-file" ||
|
||||
arg.starts_with("--env-file=") || arg == "--env-file-if-exists" ||
|
||||
arg.starts_with("--env-file-if-exists=");
|
||||
};
|
||||
std::vector<std::string> paths;
|
||||
auto path = std::find_if(args.begin(), args.end(), find_match);
|
||||
|
||||
while (path != args.end()) {
|
||||
if (*path == "--") {
|
||||
return paths;
|
||||
std::vector<Dotenv::env_file_data> env_files;
|
||||
// This will be an iterator, pointing to args.end() if no matches are found
|
||||
auto matched_arg = std::find_if(args.begin(), args.end(), find_match);
|
||||
|
||||
while (matched_arg != args.end()) {
|
||||
if (*matched_arg == "--") {
|
||||
return env_files;
|
||||
}
|
||||
auto equal_char = path->find('=');
|
||||
|
||||
if (equal_char != std::string::npos) {
|
||||
paths.push_back(path->substr(equal_char + 1));
|
||||
auto equal_char_index = matched_arg->find('=');
|
||||
|
||||
if (equal_char_index != std::string::npos) {
|
||||
// `--env-file=path`
|
||||
auto flag = matched_arg->substr(0, equal_char_index);
|
||||
auto file_path = matched_arg->substr(equal_char_index + 1);
|
||||
|
||||
struct env_file_data env_file_data = {
|
||||
file_path, flag.starts_with(optional_env_file_flag)};
|
||||
env_files.push_back(env_file_data);
|
||||
} else {
|
||||
auto next_path = std::next(path);
|
||||
// `--env-file path`
|
||||
auto file_path = std::next(matched_arg);
|
||||
|
||||
if (next_path == args.end()) {
|
||||
return paths;
|
||||
if (file_path == args.end()) {
|
||||
return env_files;
|
||||
}
|
||||
|
||||
paths.push_back(*next_path);
|
||||
struct env_file_data env_file_data = {
|
||||
*file_path, matched_arg->starts_with(optional_env_file_flag)};
|
||||
env_files.push_back(env_file_data);
|
||||
}
|
||||
|
||||
path = std::find_if(++path, args.end(), find_match);
|
||||
matched_arg = std::find_if(++matched_arg, args.end(), find_match);
|
||||
}
|
||||
|
||||
return paths;
|
||||
return env_files;
|
||||
}
|
||||
|
||||
void Dotenv::SetEnvironment(node::Environment* env) {
|
||||
|
||||
+5
-1
@@ -13,6 +13,10 @@ namespace node {
|
||||
class Dotenv {
|
||||
public:
|
||||
enum ParseResult { Valid, FileError, InvalidContent };
|
||||
struct env_file_data {
|
||||
std::string path;
|
||||
bool is_optional;
|
||||
};
|
||||
|
||||
Dotenv() = default;
|
||||
Dotenv(const Dotenv& d) = delete;
|
||||
@@ -27,7 +31,7 @@ class Dotenv {
|
||||
void SetEnvironment(Environment* env);
|
||||
v8::Local<v8::Object> ToObject(Environment* env) const;
|
||||
|
||||
static std::vector<std::string> GetPathFromArgs(
|
||||
static std::vector<env_file_data> GetDataFromArgs(
|
||||
const std::vector<std::string>& args);
|
||||
|
||||
private:
|
||||
|
||||
@@ -640,6 +640,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
|
||||
"set environment variables from supplied file",
|
||||
&EnvironmentOptions::env_file);
|
||||
Implies("--env-file", "[has_env_file_string]");
|
||||
AddOption("--env-file-if-exists",
|
||||
"set environment variables from supplied file",
|
||||
&EnvironmentOptions::optional_env_file);
|
||||
Implies("--env-file-if-exists", "[has_env_file_string]");
|
||||
AddOption("--test",
|
||||
"launch test runner on startup",
|
||||
&EnvironmentOptions::test_runner);
|
||||
|
||||
@@ -177,6 +177,7 @@ class EnvironmentOptions : public Options {
|
||||
std::string redirect_warnings;
|
||||
std::string diagnostic_dir;
|
||||
std::string env_file;
|
||||
std::string optional_env_file;
|
||||
bool has_env_file_string = false;
|
||||
bool test_runner = false;
|
||||
uint64_t test_runner_concurrency = 0;
|
||||
|
||||
Reference in new issue
Block a user