forked from vieter-v/vieter
Compare commits
No commits in common. "889d5a08849a6ab08b41e6ff94b78f7a9624c460" and "850cba6ab9b81b8bf2c28a9348c5ba7ed2f9181f" have entirely different histories.
889d5a0884
...
850cba6ab9
2 changed files with 44 additions and 11 deletions
|
|
@ -9,6 +9,27 @@ struct DockerError {
|
||||||
message string
|
message string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Container {
|
||||||
|
id string [json: Id]
|
||||||
|
names []string [json: Names]
|
||||||
|
}
|
||||||
|
|
||||||
|
// containers returns a list of all containers.
|
||||||
|
pub fn (mut d DockerConn) containers() ?[]Container {
|
||||||
|
d.send_request(Method.get, urllib.parse('/v1.41/containers/json')?)?
|
||||||
|
head, res := d.read_response()?
|
||||||
|
|
||||||
|
if head.status_code != 200 {
|
||||||
|
data := json.decode(DockerError, res)?
|
||||||
|
|
||||||
|
return error(data.message)
|
||||||
|
}
|
||||||
|
|
||||||
|
data := json.decode([]Container, res)?
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
pub struct NewContainer {
|
pub struct NewContainer {
|
||||||
image string [json: Image]
|
image string [json: Image]
|
||||||
entrypoint []string [json: Entrypoint]
|
entrypoint []string [json: Entrypoint]
|
||||||
|
|
|
||||||
|
|
@ -9,14 +9,15 @@ import encoding.hex
|
||||||
// it as if it was a continuous stream of data.
|
// it as if it was a continuous stream of data.
|
||||||
struct ChunkedResponseReader {
|
struct ChunkedResponseReader {
|
||||||
mut:
|
mut:
|
||||||
reader io.BufferedReader
|
reader io.Reader
|
||||||
bytes_left_in_chunk u64
|
bytes_left_in_chunk u64
|
||||||
|
end_of_stream bool
|
||||||
started bool
|
started bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// new_chunked_response_reader creates a new ChunkedResponseReader on the heap
|
// new_chunked_response_reader creates a new ChunkedResponseReader on the heap
|
||||||
// with the provided reader.
|
// with the provided reader.
|
||||||
pub fn new_chunked_response_reader(reader io.BufferedReader) &ChunkedResponseReader {
|
pub fn new_chunked_response_reader(reader io.Reader) &ChunkedResponseReader {
|
||||||
r := &ChunkedResponseReader{
|
r := &ChunkedResponseReader{
|
||||||
reader: reader
|
reader: reader
|
||||||
}
|
}
|
||||||
|
|
@ -26,10 +27,16 @@ pub fn new_chunked_response_reader(reader io.BufferedReader) &ChunkedResponseRea
|
||||||
|
|
||||||
// read satisfies the io.Reader interface.
|
// read satisfies the io.Reader interface.
|
||||||
pub fn (mut r ChunkedResponseReader) read(mut buf []u8) ?int {
|
pub fn (mut r ChunkedResponseReader) read(mut buf []u8) ?int {
|
||||||
|
if r.end_of_stream {
|
||||||
|
return none
|
||||||
|
}
|
||||||
|
|
||||||
if r.bytes_left_in_chunk == 0 {
|
if r.bytes_left_in_chunk == 0 {
|
||||||
// An io.BufferedReader always returns none if its stream has
|
|
||||||
// ended.
|
|
||||||
r.bytes_left_in_chunk = r.read_chunk_size()?
|
r.bytes_left_in_chunk = r.read_chunk_size()?
|
||||||
|
|
||||||
|
if r.end_of_stream {
|
||||||
|
return none
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mut c := 0
|
mut c := 0
|
||||||
|
|
@ -75,7 +82,7 @@ fn (mut r ChunkedResponseReader) read_chunk_size() ?u64 {
|
||||||
// This only occurs for the very last chunk, which always reports a size of
|
// This only occurs for the very last chunk, which always reports a size of
|
||||||
// 0.
|
// 0.
|
||||||
if num == 0 {
|
if num == 0 {
|
||||||
return none
|
r.end_of_stream = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return num
|
return num
|
||||||
|
|
@ -85,13 +92,14 @@ fn (mut r ChunkedResponseReader) read_chunk_size() ?u64 {
|
||||||
// header bytes.
|
// header bytes.
|
||||||
struct StreamFormatReader {
|
struct StreamFormatReader {
|
||||||
mut:
|
mut:
|
||||||
reader ChunkedResponseReader
|
reader io.Reader
|
||||||
bytes_left_in_chunk u32
|
bytes_left_in_chunk u32
|
||||||
|
end_of_stream bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// new_stream_format_reader creates a new StreamFormatReader using the given
|
// new_stream_format_reader creates a new StreamFormatReader using the given
|
||||||
// reader.
|
// reader.
|
||||||
pub fn new_stream_format_reader(reader ChunkedResponseReader) &StreamFormatReader {
|
pub fn new_stream_format_reader(reader io.Reader) &StreamFormatReader {
|
||||||
r := &StreamFormatReader{
|
r := &StreamFormatReader{
|
||||||
reader: reader
|
reader: reader
|
||||||
}
|
}
|
||||||
|
|
@ -101,8 +109,16 @@ pub fn new_stream_format_reader(reader ChunkedResponseReader) &StreamFormatReade
|
||||||
|
|
||||||
// read satisfies the io.Reader interface.
|
// read satisfies the io.Reader interface.
|
||||||
pub fn (mut r StreamFormatReader) read(mut buf []u8) ?int {
|
pub fn (mut r StreamFormatReader) read(mut buf []u8) ?int {
|
||||||
|
if r.end_of_stream {
|
||||||
|
return none
|
||||||
|
}
|
||||||
|
|
||||||
if r.bytes_left_in_chunk == 0 {
|
if r.bytes_left_in_chunk == 0 {
|
||||||
r.bytes_left_in_chunk = r.read_chunk_size()?
|
r.bytes_left_in_chunk = r.read_chunk_size()?
|
||||||
|
|
||||||
|
if r.end_of_stream {
|
||||||
|
return none
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mut c := 0
|
mut c := 0
|
||||||
|
|
@ -127,9 +143,5 @@ fn (mut r StreamFormatReader) read_chunk_size() ?u32 {
|
||||||
|
|
||||||
num := binary.big_endian_u32(buf[4..])
|
num := binary.big_endian_u32(buf[4..])
|
||||||
|
|
||||||
if num == 0 {
|
|
||||||
return none
|
|
||||||
}
|
|
||||||
|
|
||||||
return num
|
return num
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue