-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
src: extend --env-file
to also accept sections
#58782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
src: extend --env-file
to also accept sections
#58782
Conversation
Review requested:
|
ff88ca1
to
c1edd49
Compare
This comment was marked as resolved.
This comment was marked as resolved.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #58782 +/- ##
==========================================
- Coverage 90.08% 90.08% -0.01%
==========================================
Files 640 640
Lines 188316 188370 +54
Branches 36922 36936 +14
==========================================
+ Hits 169650 169687 +37
+ Misses 11406 11404 -2
- Partials 7260 7279 +19
🚀 New features to boost your workflow:
|
c1edd49
to
ee7e6c3
Compare
Make sure that `--env-file` can accept sections, also extend the `parseEnv` and `process.loadEnvFile` APIs to accept sections as well
ee7e6c3
to
d6e8866
Compare
Based on the issue discussion, I believe we are only short of testing variables with dots in the name: one.two.three = some value
[section]
four.five = another value At the moment we only test section names with dots. The rest looks good! 👍 |
@@ -26,6 +27,33 @@ std::vector<Dotenv::env_file_data> Dotenv::GetDataFromArgs( | |||
arg.starts_with("--env-file-if-exists="); | |||
}; | |||
|
|||
const auto get_sections = [](const std::string& path) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend always using std::string_view since you'll always know when you'll copy (by calling std::string(val))
const auto get_sections = [](const std::string& path) { | |
const auto get_sections = [](std::string_view path) { |
@@ -154,6 +215,33 @@ void Dotenv::ParseContent(const std::string_view input) { | |||
continue; | |||
} | |||
|
|||
if (content.front() == '[') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment to here explaining why you're checking for [?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
He is checking for the start of a section, I believe - [section_name]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but a year from now, a contributor might have some hard time understanding the reasoning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment added 🙂
@@ -303,7 +391,8 @@ void Dotenv::ParseContent(const std::string_view input) { | |||
} | |||
} | |||
|
|||
Dotenv::ParseResult Dotenv::ParsePath(const std::string_view path) { | |||
Dotenv::ParseResult Dotenv::ParsePath(const std::string_view path, | |||
const std::set<std::string> sections) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't add &, you'll always copy.
const std::set<std::string> sections) { | |
const std::set<std::string>& sections) { |
@@ -590,21 +590,42 @@ static void Execve(const FunctionCallbackInfo<Value>& args) { | |||
} | |||
#endif | |||
|
|||
struct IterateSectionsData { | |||
Isolate* isolate; | |||
std::set<std::string>* sections; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is considered unsafe. Why do you need to have a pointer to a set?
ToNamespacedPath(env, &path_value); | ||
auto path = path_value.ToString(); | ||
|
||
CHECK(args[1]->IsArray()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you make this optional, you don't need to allocate a std::set every time this function is called.
|
||
THROW_IF_INSUFFICIENT_PERMISSIONS( | ||
env, permission::PermissionScope::kFileSystemRead, path); | ||
|
||
Dotenv dotenv{}; | ||
|
||
switch (dotenv.ParsePath(path)) { | ||
switch (dotenv.ParsePath(path, sections)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This copies the function.
If you want to avoid copy you should make it set&, if you want to move the ownership to the function since you don't need it inside this function, you should set function parameter to set&& and pass the argument as std::move(sections)
validate sections only when present in options object
update jsdoc types for options Co-authored-by: Yagiz Nizipli <[email protected]>
Thanks 😄
As I mentioned in the discussion I believe that variable key testing is unrelated to the sections support addition (and that more testing is missing not just only around dots) so I don't see a strong need for adding it here and I would much rather do that in its own dedicated PR, do you disagree? 🙂 |
fix comment typo
add code comment explaining the `[` check
fix cpp formatting
I agree that dot based variable overriding is unrelated and probably an overkill feature to implement. |
Dots in in the variables names work currently (I tested it in v24.2.0), so it is not a feature, and extra tests would be just to cover that we do not break that. |
Fixes: #58729