Skip to content

Commit 96aad2b

Browse files
committed
container build support inline Dockerfile from stdin
1 parent f2b3cbd commit 96aad2b

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

Sources/ContainerCommands/BuildCommand.swift

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,34 @@ extension Application {
186186
throw ValidationError("builder is not running")
187187
}
188188

189-
let dockerfile = try Data(contentsOf: URL(filePath: file))
189+
let dockerfile: Data
190+
let tempFile = FileManager.default.temporaryDirectory.appendingPathComponent("TempDockerfile")
191+
defer {
192+
try? FileManager.default.removeItem(at: tempFile)
193+
}
194+
195+
// Dockerfile should be read from stdin
196+
if file == "-" {
197+
guard FileManager.default.createFile(atPath: tempFile.path(), contents: nil) else {
198+
throw ContainerizationError(.internalError, message: "unable to create temporary file")
199+
}
200+
201+
guard let outputHandle = try? FileHandle(forWritingTo: tempFile) else {
202+
throw ContainerizationError(.internalError, message: "unable to open temporary file for writing")
203+
}
204+
205+
let bufferSize = 4096
206+
while true {
207+
let chunk = FileHandle.standardInput.readData(ofLength: bufferSize)
208+
if chunk.isEmpty { break }
209+
outputHandle.write(chunk)
210+
}
211+
try outputHandle.close()
212+
dockerfile = try Data(contentsOf: URL(filePath: tempFile.path()))
213+
} else {
214+
dockerfile = try Data(contentsOf: URL(filePath: file))
215+
}
216+
190217
let systemHealth = try await ClientHealthCheck.ping(timeout: .seconds(10))
191218
let exportPath = systemHealth.appRoot
192219
.appendingPathComponent(Application.BuilderCommand.builderResourceDir)
@@ -351,7 +378,7 @@ extension Application {
351378
}
352379

353380
public func validate() throws {
354-
guard FileManager.default.fileExists(atPath: file) else {
381+
guard FileManager.default.fileExists(atPath: file) || file == "-" else {
355382
throw ValidationError("Dockerfile does not exist at path: \(file)")
356383
}
357384
guard FileManager.default.fileExists(atPath: contextDir) else {

0 commit comments

Comments
 (0)