Skip to content
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

introduce ParseKeyValueFile #5491

Closed
wants to merge 1 commit into from
Closed

Conversation

ndeloof
Copy link
Contributor

@ndeloof ndeloof commented Oct 2, 2024

- What I did
introduced ParseKeyValueFile so docker/compose can use the same parser as docker/cli when env_file is requested to be loaded in raw format. This will be used so a user can get the same behavior using docker run --env-file and compose env_file while preserving the ability to get variable interpolated by default 😵‍💫

- How I did it

- How to verify it

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)
image

@codecov-commenter
Copy link

codecov-commenter commented Oct 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.10%. Comparing base (dac7319) to head (253737e).
Report is 6 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5491   +/-   ##
=======================================
  Coverage   60.09%   60.10%           
=======================================
  Files         345      345           
  Lines       23445    23447    +2     
=======================================
+ Hits        14090    14092    +2     
  Misses       8381     8381           
  Partials      974      974           

@ndeloof ndeloof changed the base branch from 27.x to master October 2, 2024 07:28
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments; do you need this change urgently? If not, I want to give my ideas a quick try to see what it looks like

opts/envfile.go Outdated
return parseKeyValueFile(filename, os.LookupEnv)
return ParseEnvFileWithLookup(filename, os.LookupEnv)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer for this one to continue using the non-exported (internal) function; it makes it clearer whether exported functions are used, and makes it clearer that the internal implementation can be changed.

opts/envfile.go Outdated
Comment on lines 24 to 22
// ParseEnvFileWithLookup reads a file with environment variables enumerated by lines
func ParseEnvFileWithLookup(filename string, env func(key string) (string, bool)) ([]string, error) {
return parseKeyValueFile(filename, env)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some thoughts here;

  • Perhaps lookupFn is a better name for this than env.
  • Can you update the GoDoc to describe the new functionality (i.e., a lookupFn can be passed to provide values for the given key if it has no value set)
  • Can you add a minimal unit-test for this (using a custom lookup function)?

Also wondering;

  • For compose's use; is it better to pass a filename or should we have a function that takes an io.Reader? (Honestly, also looking at some of the existing unit-tests, where the requirement for having a file makes the tests more complicated than if it was a reader 😂)
  • Effectively, this is now identical to parseKeyValueFile, which is a more agnostic name, and perhaps makes the overall functionality more "flexible" (if the format is to be used elsewhere).

Considering if we should move parseKeyValueFile to a be exported (accepting a reader) or even a separate package, so that it can be consumed without all of the other opts to be used (e.g. keyvaluefile.Parse())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:+1 on using io.Reader (still need filename for error message). I tried to introduce the minimal required change here, but can make this change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated with ParseKeyValueFile to accept a Reader. Didn't moved this out of the opts package as error message refers to "environment variable" so this still make it somehow dedicated to env file parsing

@ndeloof ndeloof force-pushed the env_format branch 2 times, most recently from 045f6f5 to 90deee5 Compare October 2, 2024 13:37
@ndeloof ndeloof requested a review from thaJeztah October 2, 2024 13:48
@ndeloof ndeloof changed the title introduce ParseEnvFileWithLookup introduce ParseKeyValueFile Oct 2, 2024
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants