Skip to content

Conversation

euanh
Copy link
Collaborator

@euanh euanh commented Apr 28, 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.

@euanh euanh changed the title containertool: Don't set environment-variable defaults in parser (pre… containertool: Don't set environment-variable defaults in parser Apr 28, 2025
@euanh euanh marked this pull request as ready for review April 28, 2025 11:19
@euanh euanh added the semver/patch No public API change. label Apr 28, 2025
@euanh euanh merged commit 83866f8 into apple:main Apr 28, 2025
23 checks passed
@euanh euanh deleted the refactor/containertool-option-processing branch April 28, 2025 11:23
@euanh euanh added semver/none No version bump required. and removed semver/patch No public API change. labels Apr 30, 2025
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 euanh mentioned this pull request May 1, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver/none No version bump required.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant