Skip to content

Commit c113aa4

Browse files
committed
implement stdout for ImageSave
1 parent 0a19967 commit c113aa4

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

Sources/ContainerCommands/Image/ImageSave.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension Application {
4646
transform: { str in
4747
URL(fileURLWithPath: str, relativeTo: .currentDirectory()).absoluteURL.path(percentEncoded: false)
4848
})
49-
var output: String
49+
var output: String?
5050

5151
@Option(
5252
help: "Platform for the saved image (format: os/arch[/variant], takes precedence over --os and --arch)"
@@ -90,7 +90,30 @@ extension Application {
9090
throw ContainerizationError(.invalidArgument, message: "failed to save image(s)")
9191

9292
}
93-
try await ClientImage.save(references: references, out: output, platform: p)
93+
94+
let tempFile = FileManager.default.temporaryDirectory.appendingPathComponent("temp-file.tar")
95+
guard FileManager.default.createFile(atPath: tempFile.path(), contents: nil) else {
96+
throw ContainerizationError(.internalError, message: "unable to create temporary file")
97+
}
98+
99+
defer {
100+
try? FileManager.default.removeItem(at: tempFile)
101+
}
102+
103+
try await ClientImage.save(references: references, out: output ?? tempFile.path(), platform: p)
104+
105+
if output == nil {
106+
guard let outputHandle = try? FileHandle(forReadingFrom: tempFile) else {
107+
throw ContainerizationError(.internalError, message: "unable to open temporary file for reading")
108+
}
109+
110+
let bufferSize = 4096
111+
while true {
112+
let chunk = outputHandle.readData(ofLength: bufferSize)
113+
if chunk.isEmpty { break }
114+
FileHandle.standardOutput.write(chunk)
115+
}
116+
}
94117

95118
progress.finish()
96119
for reference in references {

0 commit comments

Comments
 (0)