Skip to content

Commit 50dbfb6

Browse files
committed
containertool: Don't set environment-variable defaults in parser (apple#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.
1 parent aefba9d commit 50dbfb6

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

Sources/containertool/containertool.swift

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
2929
)
3030

3131
@Option(help: "Default registry for references which do not specify a registry")
32-
private var defaultRegistry: String =
33-
ProcessInfo.processInfo.environment["CONTAINERTOOL_DEFAULT_REGISTRY"] ?? "docker.io"
32+
private var defaultRegistry: String?
3433

3534
@Option(help: "Repository path")
3635
private var repository: String
@@ -51,16 +50,16 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
5150
private var verbose: Bool = false
5251

5352
@Option(help: "Connect to the container registry using plaintext HTTP")
54-
var allowInsecureHttp: AllowHTTP?
53+
private var allowInsecureHttp: AllowHTTP?
5554

5655
@Option(help: "CPU architecture")
5756
private var architecture: String?
5857

5958
@Option(help: "Base image reference")
60-
private var from: String = ProcessInfo.processInfo.environment["CONTAINERTOOL_BASE_IMAGE"] ?? "swift:slim"
59+
private var from: String?
6160

6261
@Option(help: "Operating system")
63-
private var os: String = ProcessInfo.processInfo.environment["CONTAINERTOOL_OS"] ?? "linux"
62+
private var os: String?
6463

6564
@Option(help: "Tag for this manifest")
6665
private var tag: String?
@@ -72,8 +71,26 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
7271
private var netrcFile: String?
7372

7473
func run() async throws {
75-
let baseImage = try ImageReference(fromString: from, defaultRegistry: defaultRegistry)
76-
let destinationImage = try ImageReference(fromString: repository, defaultRegistry: defaultRegistry)
74+
// MARK: Apply defaults for unspecified configuration flags
75+
76+
let env = ProcessInfo.processInfo.environment
77+
let defaultRegistry = defaultRegistry ?? env["CONTAINERTOOL_DEFAULT_REGISTRY"] ?? "docker.io"
78+
let from = from ?? env["CONTAINERTOOL_BASE_IMAGE"] ?? "swift:slim"
79+
let os = os ?? env["CONTAINERTOOL_OS"] ?? "linux"
80+
81+
// Try to detect the architecture of the application executable so a suitable base image can be selected.
82+
// This reduces the risk of accidentally creating an image which stacks an aarch64 executable on top of an x86_64 base image.
83+
let executableURL = URL(fileURLWithPath: executable)
84+
let elfheader = try ELF.read(at: executableURL)
85+
86+
let architecture =
87+
architecture
88+
?? env["CONTAINERTOOL_ARCHITECTURE"]
89+
?? elfheader?.ISA.containerArchitecture
90+
?? "amd64"
91+
if verbose { log("Base image architecture: \(architecture)") }
92+
93+
// MARK: Load netrc
7794

7895
let authProvider: AuthorizationProvider?
7996
if !netrc {
@@ -89,6 +106,9 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
89106

90107
// MARK: Create registry clients
91108

109+
let baseImage = try ImageReference(fromString: from, defaultRegistry: defaultRegistry)
110+
let destinationImage = try ImageReference(fromString: repository, defaultRegistry: defaultRegistry)
111+
92112
// The base image may be stored on a different registry to the final destination, so two clients are needed.
93113
// `scratch` is a special case and requires no source client.
94114
let source: RegistryClient?
@@ -112,19 +132,6 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
112132
if verbose { log("Connected to destination registry: \(destinationImage.registry)") }
113133
if verbose { log("Using base image: \(baseImage)") }
114134

115-
// MARK: Detect the base image architecture
116-
117-
// Try to detect the architecture of the application executable so a suitable base image can be selected.
118-
// This reduces the risk of accidentally creating an image which stacks an aarch64 executable on top of an x86_64 base image.
119-
let executableURL = URL(fileURLWithPath: executable)
120-
let elfheader = try ELF.read(at: executableURL)
121-
let architecture =
122-
architecture
123-
?? ProcessInfo.processInfo.environment["CONTAINERTOOL_ARCHITECTURE"]
124-
?? elfheader?.ISA.containerArchitecture
125-
?? "amd64"
126-
if verbose { log("Base image architecture: \(architecture)") }
127-
128135
// MARK: Build the image
129136

130137
let finalImage = try await destination.publishContainerImage(

0 commit comments

Comments
 (0)