forked from vieter-v/vieter
Currently broken start of docker wrapper [CI SKIP]
This commit is contained in:
parent
022f8c4fbe
commit
6f86033cd9
6 changed files with 134 additions and 47 deletions
15
src/docker/containers.v
Normal file
15
src/docker/containers.v
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
module docker
|
||||
|
||||
import json
|
||||
import net.urllib
|
||||
|
||||
struct Container {
|
||||
id string
|
||||
names []string
|
||||
}
|
||||
|
||||
pub fn containers() ?[]Container {
|
||||
res := docker.get(urllib.parse('/containers/json') ?) ?
|
||||
|
||||
return json.decode([]Container, res.text)
|
||||
}
|
||||
48
src/docker/docker.v
Normal file
48
src/docker/docker.v
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
module docker
|
||||
|
||||
import net.unix
|
||||
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"
|
||||
|
||||
// Open a connection to the socket
|
||||
mut s := unix.connect_stream(socket) ?
|
||||
|
||||
defer {
|
||||
s.close() ?
|
||||
}
|
||||
|
||||
// Write the request to the socket
|
||||
s.write_string(req) ?
|
||||
|
||||
// Wait for the server to respond
|
||||
s.wait_for_write() ?
|
||||
|
||||
mut buf := []byte{len: buf_len}
|
||||
mut res := []byte{}
|
||||
|
||||
mut c := 0
|
||||
|
||||
for {
|
||||
c = s.read(mut buf) or {
|
||||
return error('Failed to read data from socket.')
|
||||
}
|
||||
res << buf[..c]
|
||||
|
||||
if c < buf_len {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Decode chunked response
|
||||
return http.parse_response(res.bytestr())
|
||||
}
|
||||
|
||||
fn get(url urllib.URL) ?http.Response {
|
||||
return request('GET', url)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue