diff --git a/src/build.v b/src/build.v index e635cc0..85633a4 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.containers() or { panic('yeet') }) } diff --git a/src/docker/containers.v b/src/docker/containers.v index 5bdd39e..53d2880 100644 --- a/src/docker/containers.v +++ b/src/docker/containers.v @@ -4,12 +4,12 @@ import json import net.urllib struct Container { - id string - names []string + id string + names []string } pub fn containers() ?[]Container { - res := docker.get(urllib.parse('/containers/json') ?) ? + res := get(urllib.parse('/containers/json') ?) ? - return json.decode([]Container, res.text) + return json.decode([]Container, res.text) or {} } diff --git a/src/docker/docker.v b/src/docker/docker.v index a090f02..7f9d5db 100644 --- a/src/docker/docker.v +++ b/src/docker/docker.v @@ -5,44 +5,45 @@ import net.urllib import net.http 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" + req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n' - // Open a connection to the socket - mut s := unix.connect_stream(socket) ? + // Open a connection to the socket + mut s := unix.connect_stream(docker.socket) ? - defer { - s.close() ? - } + defer { + // This or is required because otherwise, the V compiler segfaults for + // some reason + s.close() or {} + } - // Write the request to the socket - s.write_string(req) ? + // Write the request to the socket + s.write_string(req) ? - // Wait for the server to respond - s.wait_for_write() ? + // Wait for the server to respond + s.wait_for_write() ? - mut buf := []byte{len: buf_len} - mut res := []byte{} + mut buf := []byte{len: docker.buf_len} + mut res := []byte{} - mut c := 0 + mut c := 0 - for { - c = s.read(mut buf) or { - return error('Failed to read data from socket.') - } - res << buf[..c] + for { + c = s.read(mut buf) or { return error('Failed to read data from socket.') } + res << buf[..c] - if c < buf_len { - break - } - } + if c < docker.buf_len { + break + } + } - // Decode chunked response - return http.parse_response(res.bytestr()) + // Decode chunked response + return http.parse_response(res.bytestr()) } fn get(url urllib.URL) ?http.Response { - return request('GET', url) + return request('GET', url) } diff --git a/src/main.v b/src/main.v index 7b19d4b..5d8c072 100644 --- a/src/main.v +++ b/src/main.v @@ -53,13 +53,13 @@ fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? { } fn main() { - if os.args.len == 1 { - exit_with_message(1, 'No action provided.') - } + if os.args.len == 1 { + exit_with_message(1, 'No action provided.') + } - match os.args[1] { - 'server' { server() } - 'build' { build() } - else { exit_with_message(1, 'Unknown action: ${os.args[1]}') } - } + match os.args[1] { + 'server' { server() } + 'build' { build() } + else { exit_with_message(1, 'Unknown action: ${os.args[1]}') } + } }