forked from vieter-v/vieter
Fix for segfault
parent
6c435c2a1e
commit
57c4af0aaf
|
@ -3,5 +3,5 @@ module main
|
||||||
import docker
|
import docker
|
||||||
|
|
||||||
fn build() {
|
fn build() {
|
||||||
println(docker.containers() or { panic("yeet") })
|
println(docker.containers() or { panic('yeet') })
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,12 @@ import json
|
||||||
import net.urllib
|
import net.urllib
|
||||||
|
|
||||||
struct Container {
|
struct Container {
|
||||||
id string
|
id string
|
||||||
names []string
|
names []string
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn containers() ?[]Container {
|
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 {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,44 +5,45 @@ import net.urllib
|
||||||
import net.http
|
import net.http
|
||||||
|
|
||||||
const socket = '/var/run/docker.sock'
|
const socket = '/var/run/docker.sock'
|
||||||
|
|
||||||
const buf_len = 1024
|
const buf_len = 1024
|
||||||
|
|
||||||
fn request(method string, url urllib.URL) ?http.Response {
|
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
|
// Open a connection to the socket
|
||||||
mut s := unix.connect_stream(socket) ?
|
mut s := unix.connect_stream(docker.socket) ?
|
||||||
|
|
||||||
defer {
|
defer {
|
||||||
s.close() ?
|
// This or is required because otherwise, the V compiler segfaults for
|
||||||
}
|
// some reason
|
||||||
|
s.close() or {}
|
||||||
|
}
|
||||||
|
|
||||||
// Write the request to the socket
|
// Write the request to the socket
|
||||||
s.write_string(req) ?
|
s.write_string(req) ?
|
||||||
|
|
||||||
// Wait for the server to respond
|
// Wait for the server to respond
|
||||||
s.wait_for_write() ?
|
s.wait_for_write() ?
|
||||||
|
|
||||||
mut buf := []byte{len: buf_len}
|
mut buf := []byte{len: docker.buf_len}
|
||||||
mut res := []byte{}
|
mut res := []byte{}
|
||||||
|
|
||||||
mut c := 0
|
mut c := 0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
c = s.read(mut buf) or {
|
c = s.read(mut buf) or { return error('Failed to read data from socket.') }
|
||||||
return error('Failed to read data from socket.')
|
res << buf[..c]
|
||||||
}
|
|
||||||
res << buf[..c]
|
|
||||||
|
|
||||||
if c < buf_len {
|
if c < docker.buf_len {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode chunked response
|
// Decode chunked response
|
||||||
return http.parse_response(res.bytestr())
|
return http.parse_response(res.bytestr())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(url urllib.URL) ?http.Response {
|
fn get(url urllib.URL) ?http.Response {
|
||||||
return request('GET', url)
|
return request('GET', url)
|
||||||
}
|
}
|
||||||
|
|
16
src/main.v
16
src/main.v
|
@ -53,13 +53,13 @@ fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
if os.args.len == 1 {
|
if os.args.len == 1 {
|
||||||
exit_with_message(1, 'No action provided.')
|
exit_with_message(1, 'No action provided.')
|
||||||
}
|
}
|
||||||
|
|
||||||
match os.args[1] {
|
match os.args[1] {
|
||||||
'server' { server() }
|
'server' { server() }
|
||||||
'build' { build() }
|
'build' { build() }
|
||||||
else { exit_with_message(1, 'Unknown action: ${os.args[1]}') }
|
else { exit_with_message(1, 'Unknown action: ${os.args[1]}') }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue