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

[27.x backport] move parsing key-value files to a separate package #5503

Conversation

austinvazquez
Copy link
Contributor

@austinvazquez austinvazquez commented Oct 4, 2024

- What I did

Move the code for parsing key-value files, such as used for env-files and label-files to a separate package. This allows other projects (such as compose) to use the same parsing logic, but provide custom lookup functions for their situation (which is slightly different).

The new package provides utilities for parsing key-value files for either a file or an io.Reader. Most tests for EnvFile were now testing functionality that's already tested in the new package, so were (re)moved.

(cherry picked from commit 9ecfe4f)

- How I did it

git cherry-pick -xsS 95e221ef4d64cd0e1036b3c91dabb6a505fe408c
git cherry-pick -xsS 76196dbb01040d31ea96fabadde09ab7d7d9a85a
git cherry-pick -xsS 9ecfe4f5a7634d9c375ffce1a273c421f2df716c

- How to verify it

- Description for the changelog

opts: remove ErrBadKey as it's not used as a sentinel error
move parsing key-value files to a separate package (pkg/kvfile)

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

@codecov-commenter
Copy link

codecov-commenter commented Oct 4, 2024

Codecov Report

Attention: Patch coverage is 89.47368% with 4 lines in your changes missing coverage. Please review.

Project coverage is 59.77%. Comparing base (132ecbc) to head (9ab3d3a).
Report is 5 commits behind head on 27.x.

Additional details and impacted files
@@            Coverage Diff             @@
##             27.x    #5503      +/-   ##
==========================================
- Coverage   59.78%   59.77%   -0.01%     
==========================================
  Files         345      345              
  Lines       23442    23443       +1     
==========================================
- Hits        14015    14014       -1     
- Misses       8453     8454       +1     
- Partials      974      975       +1     

@thaJeztah
Copy link
Member

Can you also cherry-pick the other related changes, so that it's a clean cherry-pick? I probably should've done them together instead of as 3 separate PRs;

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.

see above 😅

@thaJeztah thaJeztah added kind/enhancement area/go-sdk Changes affecting the Go SDK impact/go-sdk Noteworthy (compatibility changes) in the Go SDK labels Oct 8, 2024
@thaJeztah thaJeztah added this to the 27.4.0 milestone Oct 8, 2024
@thaJeztah thaJeztah added the kind/refactor PR's that refactor, or clean-up code label Oct 8, 2024
thaJeztah and others added 3 commits October 8, 2024 14:18
This error was originally introduced `ErrBadEnvVariable` in [moby/moby@500c8ba],
but merely for convenience, and not used as a sentinel error. After the code
was moved from the daemon to the cli repository, it was renamed to be more
generic `ErrBadKey` in commit 2b17f4c.

A search on GitHub shows that there's no consumers using this error as
sentinel error, and it's not used in our own code as such, so it should
be safe to remove this error.

This patch removes the `ErrBadKey` error-type; it also removes the prefix
(`poorly formatted environment:`) to make the error more generic, because
the same function was used both for env-files and label-files.

[moby/moby@500c8ba]: vdemeester/moby@500c8ba

Signed-off-by: Sebastiaan van Stijn <[email protected]>
(cherry picked from commit 95e221e)
Signed-off-by: Austin Vazquez <[email protected]>
- the function already trimmed leading whitespace from each line before
  parsing. keys with trailing whitespace would be invalidated, and values
  have whitespace preserved, so there's no need to trim whitespace for the
  key.
- if a line is validated (key is valid), we don't need to reconstruct the
  key=value by concatenating, and we can add the line as-is.
- check if the key is empty before checking if it contains whitespace
- touch-up comments
- rename some variables for readability
- slight cleanup to use early returns / early continues to reduce nesting

Signed-off-by: Sebastiaan van Stijn <[email protected]>
(cherry picked from commit 76196db)
Signed-off-by: Austin Vazquez <[email protected]>
Move the code for parsing key-value files, such as used for
env-files and label-files to a separate package. This allows
other projects (such as compose) to use the same parsing
logic, but provide custom lookup functions for their situation
(which is slightly different).

The new package provides utilities for parsing key-value files
for either a file or an io.Reader. Most tests for EnvFile were
now testing functionality that's already tested in the new package,
so were (re)moved.

Co-authored-by: Nicolas De Loof <[email protected]>
Signed-off-by: Nicolas De Loof <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
(cherry picked from commit 9ecfe4f)
Signed-off-by: Austin Vazquez <[email protected]>
@austinvazquez austinvazquez force-pushed the cherry-pick-9ecfe4f5a7634d9c375ffce1a273c421f2df716c-to-27.x branch from d075f97 to 9ab3d3a Compare October 8, 2024 14:20
@austinvazquez
Copy link
Contributor Author

Can you also cherry-pick the other related changes, so that it's a clean cherry-pick? I probably should've done them together instead of as 3 separate PRs;

Oof good catch, I missed that. 😅

@thaJeztah
Copy link
Member

Oof good catch, I missed that. 😅

Yeah, the straight backport would probably still have worked (because the changes were made before the file was (re)moved), but it makes for a slightly accurate history.

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.

LGTM, thanks!

@thaJeztah thaJeztah merged commit 5c1cb99 into docker:27.x Oct 8, 2024
93 checks passed
@austinvazquez austinvazquez deleted the cherry-pick-9ecfe4f5a7634d9c375ffce1a273c421f2df716c-to-27.x branch October 8, 2024 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/go-sdk Changes affecting the Go SDK impact/go-sdk Noteworthy (compatibility changes) in the Go SDK kind/enhancement kind/refactor PR's that refactor, or clean-up code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants