feat(docker): added function to retrieve container logs

main
Jef Roosens 2022-05-08 10:29:06 +02:00
parent 7e5f0c5a53
commit 27aa215eff
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 20 additions and 0 deletions

View File

@ -76,3 +76,23 @@ pub fn remove_container(id string) ?bool {
return res.status_code == 204 return res.status_code == 204
} }
pub fn get_container_logs(id string) ?string {
res := request('GET', urllib.parse('/v1.41/containers/$id/logs?stdout=true&stderr=true') ?) ?
mut res_bytes := res.text.bytes()
// Docker uses a special "stream" format for their logs, so we have to
// clean up the data.
mut index := 0
for index < res_bytes.len {
// The reverse is required because V reads in the bytes differently
t := res_bytes[index + 4..index + 8].reverse()
len_length := unsafe { *(&u32(&t[0])) }
res_bytes.delete_many(index, 8)
index += int(len_length)
}
return res_bytes.bytestr()
}