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 { pub fn (mut d DockerConn) container_list(c ContainerListConfig) ![]ContainerListItem {
d.request(.get, '/containers/json', {}) d.request(.get, '/containers/json')
d.params(c) d.params(c)
d.send()! d.send()!
@ -35,7 +35,7 @@ pub:
} }
pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer { 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.body_json(c)
d.send()! 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. // 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.request(.post, '/containers/$id/start', {}) d.request(.post, '/containers/$id/start')
d.send()! d.send()!
d.read_response()! d.read_response()!
} }
@ -68,7 +68,7 @@ pub mut:
} }
pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect { pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
d.request(.get, '/containers/$id/json', {}) d.request(.get, '/containers/$id/json')
d.send()! d.send()!
mut data := d.read_json_response<ContainerInspect>()! 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) ! { pub fn (mut d DockerConn) container_remove(id string) ! {
d.request(.delete, '/containers/$id', {}) d.request(.delete, '/containers/$id')
d.send()! d.send()!
d.read_response()! d.read_response()!
} }
pub fn (mut d DockerConn) container_get_logs(id string) !&StreamFormatReader { 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' 'stdout': 'true'
'stderr': 'true' 'stderr': 'true'
}) })

View File

@ -3,7 +3,7 @@ module docker
import types { Image } import types { Image }
pub fn (mut d DockerConn) image_inspect(image string) !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()! d.send()!
data := d.read_json_response<Image>()! 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. // image_pull pulls the given image:tag.
pub fn (mut d DockerConn) image_pull(image string, tag string) ! { 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 'fromImage': image
'tag': tag '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. // 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 { 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 'container': id
'repo': repo 'repo': repo
'tag': tag '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. // remove_image removes the image with the given id.
pub fn (mut d DockerConn) image_remove(id string) ! { pub fn (mut d DockerConn) image_remove(id string) ! {
d.request(.delete, '/images/$id', {}) d.request(.delete, '/images/$id')
d.send()! d.send()!
d.read_response()! d.read_response()!
} }
pub fn (mut d DockerConn) image_tag(name string, repo string, tag string) ! { pub fn (mut d DockerConn) image_tag(name string, repo string, tag string) ! {
d.request(.post, '/images/$name/tag', {'repo': repo, 'tag': tag}) d.request(.post, '/images/$name/tag')
d.params({
'repo': repo
'tag': tag
})
d.send()! d.send()!
d.read_response()! d.read_response()!
} }

View File

@ -5,17 +5,13 @@ import net.urllib
import io import io
import json 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.method = method
d.url = url d.url = url
d.content_type = '' d.content_type = ''
d.body = '' d.body = ''
d.params.clear() 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) { fn (mut d DockerConn) body(content_type string, body string) {
@ -29,6 +25,11 @@ fn (mut d DockerConn) body_json<T>(data T) {
} }
fn (mut d DockerConn) params<T>(o T) { fn (mut d DockerConn) params<T>(o T) {
$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 { $for field in T.fields {
v := o.$(field.name) v := o.$(field.name)
@ -37,6 +38,7 @@ fn (mut d DockerConn) params<T>(o T) {
} }
} }
} }
}
fn (mut d DockerConn) send() ! { fn (mut d DockerConn) send() ! {
mut full_url := d.url mut full_url := d.url

View File

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