containertool: Don't set environment-variable defaults in parser#117
Merged
euanh merged 1 commit intoapple:mainfrom Apr 28, 2025
Merged
Conversation
…pare for configuration file)
euanh
added a commit
to euanh/swift-container-plugin
that referenced
this pull request
May 1, 2025
…le#117) Motivation ---------- Many `containertool` options have default values set by environment variables. * if the option flag is set on the command line, the command line value is used * otherwise, if the option flag is not set, the environment variable value is used * otherwise, a hard-coded default is used This pattern is quite convenient, but there are some drawbacks. For example: * the default value declarations complicate the definition of new arguments. * default values appear in the `--help` output: for some options, such as default base image, this is desirable, but for others, such as credentials, it is not. Some options do not fit this pattern well. For instance, `containertool` tries to detect the architecture of the executable it is packaging so it can try to choose a compatible base image. * if the architecture flag is set on the command line, that value is used * otherwise, if the flag is not set, the environment variable value is used * otherwise, if the executable is an ELF file, the architecture is read from the file * otherwise, a hard-coded default is used The path to the executable is an option which is parsed by the argument parser. It is not available when the parser sets up default argument values, so it must be handled after the arguments have been parsed, overwriting whatever the parser had set. This means that option handling for this flag is split into two different places, which invites bugs. Future changes to `containertool`'s configuration, such as supporting a configuration file on disk, add further complications. Modifications ------------- * All command line arguments with corresponding environment variables are handled uniformly, with defaults applied at the start of the `run()` function Result ------ No functional change, but maintenance and future enhancement become easier. Test Plan --------- All existing tests continue to pass. Handling defaults in this way allows future PRs to add unit or integration tests which exercise argument parsing.
euanh
added a commit
that referenced
this pull request
May 1, 2025
Motivation ---------- Many `containertool` options have default values set by environment variables. * if the option flag is set on the command line, the command line value is used * otherwise, if the option flag is not set, the environment variable value is used * otherwise, a hard-coded default is used This pattern is quite convenient, but there are some drawbacks. For example: * the default value declarations complicate the definition of new arguments. * default values appear in the `--help` output: for some options, such as default base image, this is desirable, but for others, such as credentials, it is not. Some options do not fit this pattern well. For instance, `containertool` tries to detect the architecture of the executable it is packaging so it can try to choose a compatible base image. * if the architecture flag is set on the command line, that value is used * otherwise, if the flag is not set, the environment variable value is used * otherwise, if the executable is an ELF file, the architecture is read from the file * otherwise, a hard-coded default is used The path to the executable is an option which is parsed by the argument parser. It is not available when the parser sets up default argument values, so it must be handled after the arguments have been parsed, overwriting whatever the parser had set. This means that option handling for this flag is split into two different places, which invites bugs. Future changes to `containertool`'s configuration, such as supporting a configuration file on disk, add further complications. Modifications ------------- * All command line arguments with corresponding environment variables are handled uniformly, with defaults applied at the start of the `run()` function Result ------ No functional change, but maintenance and future enhancement become easier. Test Plan --------- All existing tests continue to pass. Handling defaults in this way allows future PRs to add unit or integration tests which exercise argument parsing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Many
containertooloptions have default values set by environment variables.This pattern is quite convenient, but there are some drawbacks. For example:
--helpoutput: for some options, such as default base image, this is desirable, but for others, such as credentials, it is not.Some options do not fit this pattern well. For instance,
containertooltries to detect the architecture of the executable it is packaging so it can try to choose a compatible base image.The path to the executable is an option which is parsed by the argument parser. It is not available when the parser sets up default argument values, so it must be handled after the arguments have been parsed, overwriting whatever the parser had set. This means that option handling for this flag is split into two different places, which invites bugs.
Future changes to
containertool's configuration, such as supporting a configuration file on disk, add further complications.Modifications
run()functionResult
No functional change, but maintenance and future enhancement become easier.
Test Plan
All existing tests continue to pass.
Handling defaults in this way allows future PRs to add unit or integration tests which exercise argument parsing.