require "./api_client_wrapper"
require "./errors"
require "./image"
class Docker::Images
include Docker::ApiClientWrapper
def build(path : String, **props) : Image
updates = client.build path, **props
build_output = [] of String
image_id = nil
updates.each do |update|
chunk = update.stream
error = update.error
build_output << chunk if chunk
raise BuildError.new(build_output, error) if error
if chunk
if (match = chunk.match(/(^Successfully built |sha256:)([0-9a-f]+)$/))
image_id = match[2]
end
yield chunk
end
end
raise BuildError.new(build_output) unless image_id
get(image_id)
end
def get(id_or_name : String)
Image.new client, id_or_name
end
def list(all : Bool? = nil, name : String? = nil, filters : Hash? = nil) : Array(Image)
image_ids = client.images(all, name, filters).map &.id
image_ids.map &->get(String)
end
def pull(image : String, tag : String? = nil)
parts = image.split(":")
name = parts.first
tag = parts.last if tag.nil?
client.pull_image(name, tag)
end
end