feat(docker): added StreamFormatReader
parent
92cbea69d6
commit
f22ed29631
|
@ -168,6 +168,21 @@ pub fn remove_container(id string) ?bool {
|
|||
return res.status_code == 204
|
||||
}
|
||||
|
||||
pub fn (mut d DockerDaemon) get_container_logs(id string) ?&StreamFormatReader {
|
||||
d.send_request('GET', urllib.parse('/v1.41/containers/$id/logs?stdout=true&stderr=true')?)?
|
||||
head := d.read_response_head()?
|
||||
|
||||
if head.status_code != 200 {
|
||||
content_length := head.header.get(http.CommonHeader.content_length)?.int()
|
||||
body := d.read_response_body(content_length)?
|
||||
data := json.decode(DockerError, body)?
|
||||
|
||||
return error(data.message)
|
||||
}
|
||||
|
||||
return d.get_stream_format_reader()
|
||||
}
|
||||
|
||||
// get_container_logs retrieves the logs for a Docker container, both stdout &
|
||||
// stderr.
|
||||
pub fn get_container_logs(id string) ?string {
|
||||
|
|
|
@ -123,3 +123,10 @@ pub fn (mut d DockerDaemon) get_chunked_response_reader() &ChunkedResponseReader
|
|||
|
||||
return r
|
||||
}
|
||||
|
||||
pub fn (mut d DockerDaemon) get_stream_format_reader() &StreamFormatReader {
|
||||
r := new_chunked_response_reader(d.reader)
|
||||
r2 := new_stream_format_reader(r)
|
||||
|
||||
return r2
|
||||
}
|
||||
|
|
|
@ -8,9 +8,6 @@ import encoding.hex
|
|||
struct ChunkedResponseReader {
|
||||
mut:
|
||||
reader io.Reader
|
||||
// buf []u8
|
||||
// offset int
|
||||
// len int
|
||||
bytes_left_in_chunk u64
|
||||
end_of_stream bool
|
||||
started bool
|
||||
|
@ -97,3 +94,57 @@ fn (mut r ChunkedResponseReader) read_chunk_size() ?u64 {
|
|||
|
||||
return num
|
||||
}
|
||||
|
||||
struct StreamFormatReader {
|
||||
stdout bool
|
||||
stderr bool
|
||||
stdin bool
|
||||
mut:
|
||||
reader io.Reader
|
||||
bytes_left_in_chunk u32
|
||||
end_of_stream bool
|
||||
}
|
||||
|
||||
pub fn new_stream_format_reader(reader io.Reader) &StreamFormatReader {
|
||||
r := &StreamFormatReader{
|
||||
reader: reader
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
pub fn (mut r StreamFormatReader) read(mut buf []u8) ?int {
|
||||
if r.end_of_stream {
|
||||
return none
|
||||
}
|
||||
|
||||
if r.bytes_left_in_chunk == 0 {
|
||||
r.bytes_left_in_chunk = r.read_chunk_size()?
|
||||
|
||||
if r.end_of_stream {
|
||||
return none
|
||||
}
|
||||
}
|
||||
|
||||
mut c := 0
|
||||
|
||||
if buf.len > r.bytes_left_in_chunk {
|
||||
c = r.reader.read(mut buf[..r.bytes_left_in_chunk])?
|
||||
} else {
|
||||
c = r.reader.read(mut buf)?
|
||||
}
|
||||
|
||||
r.bytes_left_in_chunk -= u32(c)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
fn (mut r StreamFormatReader) read_chunk_size() ?u32 {
|
||||
mut buf := []u8{len: 8}
|
||||
|
||||
r.reader.read(mut buf)?
|
||||
|
||||
num := binary.big_endian_u32(buf[4..])
|
||||
|
||||
return num
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue