Skip to content

Commit 9a0d411

Browse files
committed
container build support inline Dockerfile from stdin
1 parent 745cc18 commit 9a0d411

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

Sources/ContainerCommands/BuildCommand.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,33 @@ extension Application {
207207
buildFilePath = resolvedPath
208208
}
209209

210-
let buildFileData = try Data(contentsOf: URL(filePath: buildFilePath))
210+
let buildFileData: Data
211+
let tempFile = FileManager.default.temporaryDirectory.appendingPathComponent("TempDockerfile")
212+
defer {
213+
try? FileManager.default.removeItem(at: tempFile)
214+
}
215+
216+
// Dockerfile should be read from stdin
217+
if file == "-" {
218+
guard FileManager.default.createFile(atPath: tempFile.path(), contents: nil) else {
219+
throw ContainerizationError(.internalError, message: "unable to create temporary file")
220+
}
221+
222+
guard let outputHandle = try? FileHandle(forWritingTo: tempFile) else {
223+
throw ContainerizationError(.internalError, message: "unable to open temporary file for writing")
224+
}
225+
226+
let bufferSize = 4096
227+
while true {
228+
let chunk = FileHandle.standardInput.readData(ofLength: bufferSize)
229+
if chunk.isEmpty { break }
230+
outputHandle.write(chunk)
231+
}
232+
try outputHandle.close()
233+
buildFileData = try Data(contentsOf: URL(filePath: tempFile.path()))
234+
} else {
235+
buildFileData = try Data(contentsOf: URL(filePath: buildFilePath))
236+
}
211237

212238
let systemHealth = try await ClientHealthCheck.ping(timeout: .seconds(10))
213239
let exportPath = systemHealth.appRoot

0 commit comments

Comments
 (0)