diff --git a/.woodpecker/lint.yml b/.woodpecker/lint.yml index 1aeda92..e502737 100644 --- a/.woodpecker/lint.yml +++ b/.woodpecker/lint.yml @@ -1,6 +1,3 @@ -variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' - branches: exclude: [ main ] @@ -8,14 +5,14 @@ platform: 'linux/amd64' pipeline: build: - image: *vlang_image + image: 'git.rustybever.be/chewing_bever/vlang:0.3.2' commands: - make when: event: [ push ] lint: - image: *vlang_image + image: 'git.rustybever.be/chewing_bever/vlang:0.3.2' commands: - make lint when: diff --git a/ROADMAP.md b/ROADMAP.md index bb4d938..648d4c5 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -20,7 +20,7 @@ reference](https://docs.docker.com/engine/api/v1.41/). - [ ] Start a container - [ ] Stop a container - [ ] Restart a container - - [x] Kill a container + - [ ] Kill a container - [ ] Update a container - [ ] Rename a container - [ ] Pause a container diff --git a/containers.v b/containers.v index ce9c81b..6981a2c 100644 --- a/containers.v +++ b/containers.v @@ -16,7 +16,7 @@ pub fn (mut d DockerConn) container_list(c ContainerListConfig) ![]ContainerList d.params(c) d.send()! - return d.read_json_response[[]ContainerListItem]() + return d.read_json_response<[]ContainerListItem>() } pub struct NewContainer { @@ -39,12 +39,12 @@ pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer { d.body_json(c) d.send()! - return d.read_json_response[CreatedContainer]() + return d.read_json_response() } // 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,10 +68,10 @@ 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]()! + mut data := d.read_json_response()! // The Docker engine API *should* always return UTC time. data.state.start_time = time.parse_rfc3339(data.state.start_time_str)! @@ -84,19 +84,13 @@ 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.send()! - d.read_response()! -} - -pub fn (mut d DockerConn) container_kill(id string) ! { - d.request(.post, '/containers/${id}/kill') + 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' diff --git a/docker.v b/docker.v index 2a3458b..cd30b60 100644 --- a/docker.v +++ b/docker.v @@ -81,7 +81,7 @@ fn (mut d DockerConn) read_response_body() ! { content_length := d.head.header.get(.content_length)!.int() if content_length == 0 { - d.body = '' + d.body = '' return } @@ -111,7 +111,7 @@ fn (mut d DockerConn) read_response() ! { // read_json_response is a convenience function that runs read_response // before parsing its contents, which is assumed to be JSON, into a struct. -fn (mut d DockerConn) read_json_response[T]() !T { +fn (mut d DockerConn) read_json_response() !T { d.read_response()! data := json.decode(T, d.body)! @@ -156,6 +156,6 @@ fn (mut d DockerConn) check_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) + return error_with_code('$d.head.status(): $d_err.message', d.head.status_code) } } diff --git a/images.v b/images.v index 8641430..76df20d 100644 --- a/images.v +++ b/images.v @@ -3,10 +3,10 @@ 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]()! + data := d.read_json_response()! return data } @@ -40,21 +40,20 @@ pub fn (mut d DockerConn) image_from_container(id string, repo string, tag strin 'repo': repo 'tag': tag }) - d.body('application/json', '{}') d.send()! - return d.read_json_response[Image]()! + return d.read_json_response()! } // 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') + d.request(.post, '/images/$name/tag') d.params({ 'repo': repo 'tag': tag diff --git a/request.v b/request.v index 9d3f4ed..92d7073 100644 --- a/request.v +++ b/request.v @@ -19,12 +19,12 @@ fn (mut d DockerConn) body(content_type string, body string) { d.body = body } -fn (mut d DockerConn) body_json[T](data T) { +fn (mut d DockerConn) body_json(data T) { d.content_type = 'application/json' d.body = json.encode(data) } -fn (mut d DockerConn) params[T](o T) { +fn (mut d DockerConn) params(o T) { $if T is map[string]string { for key, value in o { d.params[key] = urllib.query_escape(value.replace("'", '"')) @@ -41,17 +41,11 @@ fn (mut d DockerConn) params[T](o T) { } fn (mut d DockerConn) send() ! { - mut full_url := '/${docker.api_version}${d.url}' + mut full_url := d.url if d.params.len > 0 { - mut fields := []string{cap: d.params.len} - - for key, value in d.params { - fields << '${key}=${value}' - } - params_str := fields.join('&') - // params_str := d.params.keys().map('${it}=${d.params[it]}').join('&') - full_url += '?${params_str}' + params_str := d.params.keys().map('$it=${d.params[it]}').join('&') + full_url += '?$params_str' } // This is to make sure we actually created a valid URL @@ -59,9 +53,9 @@ fn (mut d DockerConn) send() ! { final_url := parsed_url.request_uri() req := if d.body == '' { - '${d.method} ${final_url} HTTP/1.1\nHost: localhost\n\n' + '$d.method $final_url HTTP/1.1\nHost: localhost\n\n' } else { - '${d.method} ${final_url} HTTP/1.1\nHost: localhost\nContent-Type: ${d.content_type}\nContent-Length: ${d.body.len}\n\n${d.body}\n\n' + '$d.method $final_url HTTP/1.1\nHost: localhost\nContent-Type: $d.content_type\nContent-Length: $d.body.len\n\n$d.body\n\n' } d.socket.write_string(req)! diff --git a/util/util.v b/util/util.v index e905a9f..1181480 100644 --- a/util/util.v +++ b/util/util.v @@ -21,7 +21,7 @@ pub fn reader_to_writer(mut reader io.Reader, mut writer io.Writer) ! { // match_array_in_array returns how many elements of a2 overlap with a1. For // example, if a1 = "abcd" & a2 = "cd", the result will be 2. If the match is // not at the end of a1, the result is 0. -pub fn match_array_in_array[T](a1 []T, a2 []T) int { +pub fn match_array_in_array(a1 []T, a2 []T) int { mut i := 0 mut match_len := 0 diff --git a/volumes.v b/volumes.v index 69e619b..be648c8 100644 --- a/volumes.v +++ b/volumes.v @@ -20,7 +20,7 @@ pub fn (mut d DockerConn) volume_list() !VolumeListResponse { d.request(.get, '/volumes') d.send()! - mut data := d.read_json_response[VolumeListResponse]()! + mut data := d.read_json_response()! for mut vol in data.volumes { vol.created_at = time.parse_rfc3339(vol.created_at_str)!