From e6a1d32f0e9aa62e650116da33b1e3b1bda40025 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 19 Feb 2022 22:25:52 +0100 Subject: [PATCH] Some experimenting with docker api --- .editorconfig | 3 ++- src/build.v | 2 +- src/docker/containers.v | 4 ++-- src/docker/docker.v | 41 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/.editorconfig b/.editorconfig index 355c6bf4..630e4fa7 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,4 +7,5 @@ end_of_line = lf insert_final_newline = true [*.v] -indent_style = space +# vfmt wants it :( +indent_style = tab diff --git a/src/build.v b/src/build.v index 85633a49..304aaf2a 100644 --- a/src/build.v +++ b/src/build.v @@ -3,5 +3,5 @@ module main import docker fn build() { - println(docker.containers() or { panic('yeet') }) + println(docker.pull('archlinux', 'latest') or { panic('yeetus') }) } diff --git a/src/docker/containers.v b/src/docker/containers.v index 53d28807..f0a12234 100644 --- a/src/docker/containers.v +++ b/src/docker/containers.v @@ -4,8 +4,8 @@ import json import net.urllib struct Container { - id string - names []string + id string [json: Id] + names []string [json: Names] } pub fn containers() ?[]Container { diff --git a/src/docker/docker.v b/src/docker/docker.v index 7f9d5db3..7f5c8546 100644 --- a/src/docker/docker.v +++ b/src/docker/docker.v @@ -3,20 +3,20 @@ module docker import net.unix import net.urllib import net.http +import json const socket = '/var/run/docker.sock' const buf_len = 1024 -fn request(method string, url urllib.URL) ?http.Response { - req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n' - +fn send(req &string) ?http.Response { // Open a connection to the socket mut s := unix.connect_stream(docker.socket) ? defer { // This or is required because otherwise, the V compiler segfaults for // some reason + // https://github.com/vlang/v/issues/13534 s.close() or {} } @@ -42,8 +42,43 @@ fn request(method string, url urllib.URL) ?http.Response { // Decode chunked response return http.parse_response(res.bytestr()) + +} + +fn request_with_body(method string, url urllib.URL, body &string) ?http.Response { + req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Length: ${body.len}\n$body\n' + + return send(req) +} + +fn request(method string, url urllib.URL) ?http.Response { + req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n' + + return send(req) +} + +pub fn request_with_json(method string, url urllib.URL, data T) ?http.Response { + body := json.encode(data) + println(body) + + return request_with_body(method, url, body) } fn get(url urllib.URL) ?http.Response { return request('GET', url) } + +struct ImagePull { + from_image string [json: fromImage] + tag string +} + +pub fn pull(image string, tag string) ?http.Response { + // data := ImagePull{ + // from_image: image + // tag: tag + // } + + // return request_with_json("POST", urllib.parse("/images/create") ?, data) + return request("POST", urllib.parse("/images/create?fromImage=$image&tag=$tag") ?) +}