refactor: removed custom error type; greatly increased internal api
parent
9aebb3bedc
commit
c8191a19e1
6
Makefile
6
Makefile
|
@ -1,6 +1,6 @@
|
||||||
# =====CONFIG=====
|
# =====CONFIG=====
|
||||||
V_PATH ?= v
|
V_PATH ?= v
|
||||||
V := $(V_PATH) -showcc
|
V := $(V_PATH) -showcc -d use_openssl
|
||||||
|
|
||||||
all: vdocker
|
all: vdocker
|
||||||
|
|
||||||
|
@ -10,6 +10,10 @@ all: vdocker
|
||||||
vdocker:
|
vdocker:
|
||||||
$(V) -g -shared .
|
$(V) -g -shared .
|
||||||
|
|
||||||
|
.PHONY: c
|
||||||
|
c:
|
||||||
|
$(V) -o docker.c .
|
||||||
|
|
||||||
|
|
||||||
# =====DOCS=====
|
# =====DOCS=====
|
||||||
.PHONY: api-docs
|
.PHONY: api-docs
|
||||||
|
|
48
containers.v
48
containers.v
|
@ -1,6 +1,5 @@
|
||||||
module docker
|
module docker
|
||||||
|
|
||||||
import json
|
|
||||||
import time
|
import time
|
||||||
import net.http { Method }
|
import net.http { Method }
|
||||||
import types { ContainerListItem }
|
import types { ContainerListItem }
|
||||||
|
@ -38,29 +37,14 @@ pub:
|
||||||
|
|
||||||
pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer {
|
pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer {
|
||||||
d.send_request_with_json(Method.post, '/containers/create', c)!
|
d.send_request_with_json(Method.post, '/containers/create', c)!
|
||||||
head, res := d.read_response()!
|
|
||||||
|
|
||||||
if head.status_code != 201 {
|
return d.read_json_response<CreatedContainer>()
|
||||||
data := json.decode(DockerError, res)!
|
|
||||||
|
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
data := json.decode(CreatedContainer, res)!
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// start_container starts the container with the given id.
|
// start_container starts the container with the given id.
|
||||||
pub fn (mut d DockerConn) container_start(id string) ! {
|
pub fn (mut d DockerConn) container_start(id string) ! {
|
||||||
d.send_request(Method.post, '/containers/$id/start')!
|
d.send_request(Method.post, '/containers/$id/start')!
|
||||||
head, body := d.read_response()!
|
d.read_response()!
|
||||||
|
|
||||||
if head.status_code != 204 {
|
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ContainerInspect {
|
struct ContainerInspect {
|
||||||
|
@ -83,15 +67,8 @@ pub mut:
|
||||||
|
|
||||||
pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
|
pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
|
||||||
d.send_request(Method.get, '/containers/$id/json')!
|
d.send_request(Method.get, '/containers/$id/json')!
|
||||||
head, body := d.read_response()!
|
|
||||||
|
|
||||||
if head.status_code != 200 {
|
mut data := d.read_json_response<ContainerInspect>()!
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
mut data := json.decode(ContainerInspect, body)!
|
|
||||||
|
|
||||||
// The Docker engine API *should* always return UTC time.
|
// The Docker engine API *should* always return UTC time.
|
||||||
data.state.start_time = time.parse_rfc3339(data.state.start_time_str)!
|
data.state.start_time = time.parse_rfc3339(data.state.start_time_str)!
|
||||||
|
@ -105,26 +82,13 @@ pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
|
||||||
|
|
||||||
pub fn (mut d DockerConn) container_remove(id string) ! {
|
pub fn (mut d DockerConn) container_remove(id string) ! {
|
||||||
d.send_request(Method.delete, '/containers/$id')!
|
d.send_request(Method.delete, '/containers/$id')!
|
||||||
head, body := d.read_response()!
|
d.read_response()!
|
||||||
|
|
||||||
if head.status_code != 204 {
|
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut d DockerConn) container_get_logs(id string) !&StreamFormatReader {
|
pub fn (mut d DockerConn) container_get_logs(id string) !&StreamFormatReader {
|
||||||
d.send_request(Method.get, '/containers/$id/logs?stdout=true&stderr=true')!
|
d.send_request(Method.get, '/containers/$id/logs?stdout=true&stderr=true')!
|
||||||
head := d.read_response_head()!
|
d.read_response_head()!
|
||||||
|
d.check_error()!
|
||||||
if head.status_code != 200 {
|
|
||||||
content_length := head.header.get(http.CommonHeader.content_length)!.int()
|
|
||||||
body := d.read_response_body(content_length)!
|
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
return d.get_stream_format_reader()
|
return d.get_stream_format_reader()
|
||||||
}
|
}
|
||||||
|
|
77
docker.v
77
docker.v
|
@ -27,6 +27,7 @@ mut:
|
||||||
url string
|
url string
|
||||||
params map[string]string
|
params map[string]string
|
||||||
content_type string
|
content_type string
|
||||||
|
head http.Response
|
||||||
body string
|
body string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,79 +82,61 @@ fn (mut d DockerConn) send_request_with_json<T>(method http.Method, url_str stri
|
||||||
// '\r\n\r\n', after which it parses the response as an HTTP response.
|
// '\r\n\r\n', after which it parses the response as an HTTP response.
|
||||||
// Importantly, this function never consumes the reader past the HTTP
|
// Importantly, this function never consumes the reader past the HTTP
|
||||||
// separator, so the body can be read fully later on.
|
// separator, so the body can be read fully later on.
|
||||||
fn (mut d DockerConn) read_response_head() !http.Response {
|
fn (mut d DockerConn) read_response_head() ! {
|
||||||
mut res := []u8{}
|
mut res := []u8{}
|
||||||
|
|
||||||
util.read_until_separator(mut d.reader, mut res, docker.http_separator)!
|
util.read_until_separator(mut d.reader, mut res, docker.http_separator)!
|
||||||
|
|
||||||
return http.parse_response(res.bytestr())
|
d.head = http.parse_response(res.bytestr())!
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_response_body reads `length` bytes from the stream. It can be used when
|
fn (mut d DockerConn) read_response_body() ! {
|
||||||
// the response encoding isn't chunked to fully read it.
|
content_length := d.head.header.get(.content_length)!.int()
|
||||||
fn (mut d DockerConn) read_response_body(length int) !string {
|
|
||||||
if length == 0 {
|
if content_length == 0 {
|
||||||
return ''
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mut buf := []u8{len: docker.buf_len}
|
mut buf := []u8{len: docker.buf_len}
|
||||||
mut c := 0
|
mut c := 0
|
||||||
mut builder := strings.new_builder(docker.buf_len)
|
mut builder := strings.new_builder(docker.buf_len)
|
||||||
|
|
||||||
for builder.len < length {
|
for builder.len < content_length {
|
||||||
c = d.reader.read(mut buf) or { break }
|
c = d.reader.read(mut buf) or { break }
|
||||||
|
|
||||||
builder.write(buf[..c])!
|
builder.write(buf[..c])!
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.str()
|
d.body = builder.str()
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_response is a convenience function which always consumes the entire
|
// read_response is a convenience function which always consumes the entire
|
||||||
// response & returns it. It should only be used when we're certain that the
|
// response & returns it. It should only be used when we're certain that the
|
||||||
// result isn't too large.
|
// result isn't too large.
|
||||||
fn (mut d DockerConn) read_response() !(http.Response, string) {
|
fn (mut d DockerConn) read_response() ! {
|
||||||
head := d.read_response_head()!
|
d.read_response_head()!
|
||||||
|
d.check_error()!
|
||||||
if head.status().is_error() {
|
|
||||||
content_length := head.header.get(.content_length)!.int()
|
|
||||||
body := d.read_response_body(content_length)!
|
|
||||||
mut err := json.decode(DockerError, body)!
|
|
||||||
err.status = head.status_code
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 204 means "No Content", so we can assume nothing follows after this
|
// 204 means "No Content", so we can assume nothing follows after this
|
||||||
if head.status() == .no_content {
|
if d.head.status() == .no_content {
|
||||||
return head, ''
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if head.header.get(http.CommonHeader.transfer_encoding) or { '' } == 'chunked' {
|
if d.head.header.get(http.CommonHeader.transfer_encoding) or { '' } == 'chunked' {
|
||||||
mut builder := strings.new_builder(1024)
|
mut builder := strings.new_builder(1024)
|
||||||
mut body := d.get_chunked_response_reader()
|
mut body := d.get_chunked_response_reader()
|
||||||
|
|
||||||
util.reader_to_writer(mut body, mut builder)!
|
util.reader_to_writer(mut body, mut builder)!
|
||||||
|
d.body = builder.str()
|
||||||
return head, builder.str()
|
} else {
|
||||||
|
d.read_response_body()!
|
||||||
}
|
}
|
||||||
|
|
||||||
content_length := head.header.get(http.CommonHeader.content_length)!.int()
|
|
||||||
body := d.read_response_body(content_length)!
|
|
||||||
|
|
||||||
return head, body
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut d DockerConn) read_json_response<T>() !T {
|
fn (mut d DockerConn) read_json_response<T>() !T {
|
||||||
head, body := d.read_response()!
|
d.read_response()!
|
||||||
|
|
||||||
if head.status_code < 200 || head.status_code > 300 {
|
mut data := json.decode(T, d.body)!
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
|
||||||
return docker_error(head.status_code, data.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
mut data := json.decode(T, body)!
|
|
||||||
|
|
||||||
//$for field in T.fields {
|
//$for field in T.fields {
|
||||||
//$if field.typ is time.Time {
|
//$if field.typ is time.Time {
|
||||||
|
@ -180,3 +163,21 @@ fn (mut d DockerConn) get_stream_format_reader() &StreamFormatReader {
|
||||||
|
|
||||||
return r2
|
return r2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DockerError {
|
||||||
|
pub:
|
||||||
|
message string
|
||||||
|
}
|
||||||
|
|
||||||
|
// check_error should be called after read_response_head. If the status code of
|
||||||
|
// the response is an error, the body is consumed and the Docker HTTP error is
|
||||||
|
// returned as a V error. If the status isn't the error, this function is a
|
||||||
|
// no-op.
|
||||||
|
fn (mut d DockerConn) check_error() ! {
|
||||||
|
if d.head.status().is_error() {
|
||||||
|
d.read_response_body()!
|
||||||
|
d_err := json.decode(DockerError, d.body)!
|
||||||
|
|
||||||
|
return error_with_code('$d.head.status(): $d_err.message', d.head.status_code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
22
errors.v
22
errors.v
|
@ -1,22 +0,0 @@
|
||||||
module docker
|
|
||||||
|
|
||||||
struct DockerError {
|
|
||||||
pub mut:
|
|
||||||
status int [skip]
|
|
||||||
message string
|
|
||||||
}
|
|
||||||
|
|
||||||
fn (err DockerError) code() int {
|
|
||||||
return err.status
|
|
||||||
}
|
|
||||||
|
|
||||||
fn (err DockerError) msg() string {
|
|
||||||
return err.message
|
|
||||||
}
|
|
||||||
|
|
||||||
fn docker_error(status int, message string) DockerError {
|
|
||||||
return DockerError{
|
|
||||||
status: status
|
|
||||||
message: message
|
|
||||||
}
|
|
||||||
}
|
|
20
images.v
20
images.v
|
@ -2,13 +2,11 @@ module docker
|
||||||
|
|
||||||
import net.http { Method }
|
import net.http { Method }
|
||||||
import types { Image }
|
import types { Image }
|
||||||
import json
|
|
||||||
|
|
||||||
pub fn (mut d DockerConn) image_inspect(image string) !Image {
|
pub fn (mut d DockerConn) image_inspect(image string) !Image {
|
||||||
d.send_request(.get, '/images/$image/json')!
|
d.send_request(.get, '/images/$image/json')!
|
||||||
_, body := d.read_response()!
|
|
||||||
|
|
||||||
data := json.decode(Image, body)!
|
data := d.read_json_response<Image>()!
|
||||||
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
@ -16,16 +14,8 @@ pub fn (mut d DockerConn) image_inspect(image string) !Image {
|
||||||
// pull_image pulls the given image:tag.
|
// pull_image pulls the given image:tag.
|
||||||
pub fn (mut d DockerConn) pull_image(image string, tag string) ! {
|
pub fn (mut d DockerConn) pull_image(image string, tag string) ! {
|
||||||
d.send_request(Method.post, '/images/create?fromImage=$image&tag=$tag')!
|
d.send_request(Method.post, '/images/create?fromImage=$image&tag=$tag')!
|
||||||
head := d.read_response_head()!
|
d.read_response_head()!
|
||||||
|
d.check_error()!
|
||||||
if head.status().is_error() {
|
|
||||||
content_length := head.header.get(.content_length)!.int()
|
|
||||||
body := d.read_response_body(content_length)!
|
|
||||||
mut err := json.decode(DockerError, body)!
|
|
||||||
err.status = head.status_code
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep reading the body until the pull has completed
|
// Keep reading the body until the pull has completed
|
||||||
mut body := d.get_chunked_response_reader()
|
mut body := d.get_chunked_response_reader()
|
||||||
|
@ -40,9 +30,7 @@ pub fn (mut d DockerConn) pull_image(image string, tag string) ! {
|
||||||
// create_image_from_container creates a new image from a container.
|
// create_image_from_container creates a new image from a container.
|
||||||
pub fn (mut d DockerConn) create_image_from_container(id string, repo string, tag string) !Image {
|
pub fn (mut d DockerConn) create_image_from_container(id string, repo string, tag string) !Image {
|
||||||
d.send_request(.post, '/commit?container=$id&repo=$repo&tag=$tag')!
|
d.send_request(.post, '/commit?container=$id&repo=$repo&tag=$tag')!
|
||||||
_, body := d.read_response()!
|
data := d.read_json_response<Image>()!
|
||||||
|
|
||||||
data := json.decode(Image, body)!
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue