Skip to content

Commit 3cd7eed

Browse files
committed
containertool: Don't set environment-variable defaults in parser (prepare for configuration file)
1 parent 6ed6c6c commit 3cd7eed

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)