refactor: another change to internal api
ci/woodpecker/push/lint Pipeline was successful Details

fix-segfaults
Jef Roosens 2023-01-05 16:31:48 +01:00
parent 5bc54c37f3
commit d31681843c
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 32 additions and 23 deletions

View File

@ -12,7 +12,7 @@ pub struct ContainerListConfig {
}
pub fn (mut d DockerConn) container_list(c ContainerListConfig) ![]ContainerListItem {
d.request(.get, '/containers/json', {})
d.request(.get, '/containers/json')
d.params(c)
d.send()!
@ -35,7 +35,7 @@ pub:
}
pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer {
d.request(.post, '/containers/create', {})
d.request(.post, '/containers/create')
d.body_json(c)
d.send()!
@ -44,7 +44,7 @@ pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer {
// start_container starts the container with the given id.
pub fn (mut d DockerConn) container_start(id string) ! {
d.request(.post, '/containers/$id/start', {})
d.request(.post, '/containers/$id/start')
d.send()!
d.read_response()!
}
@ -68,7 +68,7 @@ pub mut:
}
pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
d.request(.get, '/containers/$id/json', {})
d.request(.get, '/containers/$id/json')
d.send()!
mut data := d.read_json_response<ContainerInspect>()!
@ -84,13 +84,14 @@ pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
}
pub fn (mut d DockerConn) container_remove(id string) ! {
d.request(.delete, '/containers/$id', {})
d.request(.delete, '/containers/$id')
d.send()!
d.read_response()!
}
pub fn (mut d DockerConn) container_get_logs(id string) !&StreamFormatReader {
d.request(.get, '/containers/$id/logs', {
d.request(.get, '/containers/$id/logs')
d.params({
'stdout': 'true'
'stderr': 'true'
})

View File

@ -3,7 +3,7 @@ module docker
import types { Image }
pub fn (mut d DockerConn) image_inspect(image string) !Image {
d.request(.get, '/images/$image/json', {})
d.request(.get, '/images/$image/json')
d.send()!
data := d.read_json_response<Image>()!
@ -13,7 +13,8 @@ pub fn (mut d DockerConn) image_inspect(image string) !Image {
// image_pull pulls the given image:tag.
pub fn (mut d DockerConn) image_pull(image string, tag string) ! {
d.request(.post, '/images/create', {
d.request(.post, '/images/create')
d.params({
'fromImage': image
'tag': tag
})
@ -33,7 +34,8 @@ pub fn (mut d DockerConn) image_pull(image string, tag string) ! {
// create_image_from_container creates a new image from a container.
pub fn (mut d DockerConn) image_from_container(id string, repo string, tag string) !Image {
d.request(.post, '/commit', {
d.request(.post, '/commit')
d.params({
'container': id
'repo': repo
'tag': tag
@ -45,13 +47,17 @@ pub fn (mut d DockerConn) image_from_container(id string, repo string, tag strin
// remove_image removes the image with the given id.
pub fn (mut d DockerConn) image_remove(id string) ! {
d.request(.delete, '/images/$id', {})
d.request(.delete, '/images/$id')
d.send()!
d.read_response()!
}
pub fn (mut d DockerConn) image_tag(name string, repo string, tag string) ! {
d.request(.post, '/images/$name/tag', {'repo': repo, 'tag': tag})
d.send()!
d.read_response()!
d.request(.post, '/images/$name/tag')
d.params({
'repo': repo
'tag': tag
})
d.send()!
d.read_response()!
}

View File

@ -5,17 +5,13 @@ import net.urllib
import io
import json
fn (mut d DockerConn) request(method http.Method, url string, params map[string]string) {
fn (mut d DockerConn) request(method http.Method, url string) {
d.method = method
d.url = url
d.content_type = ''
d.body = ''
d.params.clear()
for key, value in params {
d.params[key] = urllib.query_escape(value.replace("'", '"'))
}
}
fn (mut d DockerConn) body(content_type string, body string) {
@ -29,11 +25,17 @@ fn (mut d DockerConn) body_json<T>(data T) {
}
fn (mut d DockerConn) params<T>(o T) {
$for field in T.fields {
v := o.$(field.name)
$if T is map[string]string {
for key, value in o {
d.params[key] = urllib.query_escape(value.replace("'", '"'))
}
} $else {
$for field in T.fields {
v := o.$(field.name)
if !isnil(v) {
d.params[field.name] = urllib.query_escape(v.str().replace("'", '"'))
if !isnil(v) {
d.params[field.name] = urllib.query_escape(v.str().replace("'", '"'))
}
}
}
}

View File

@ -17,7 +17,7 @@ struct VolumeListResponse {
}
pub fn (mut d DockerConn) volume_list() !VolumeListResponse {
d.request(.get, '/volumes', {})
d.request(.get, '/volumes')
d.send()!
mut data := d.read_json_response<VolumeListResponse>()!