refactor(util): split into two files

Jef Roosens 2022-05-16 14:22:53 +02:00
parent 1d3c7a1651
commit 055b168ff1
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
7 changed files with 114 additions and 107 deletions

View File

@ -9,9 +9,10 @@ import client
import strings import strings
import util import util
const container_build_dir = '/build' const (
container_build_dir = '/build'
const build_image_repo = 'vieter-build' build_image_repo = 'vieter-build'
)
// create_build_image creates a builder image given some base image which can // create_build_image creates a builder image given some base image which can
// then be used to build & package Arch images. It mostly just updates the // then be used to build & package Arch images. It mostly just updates the

View File

@ -1,5 +1,3 @@
# docker
This module implements part of the Docker Engine API v1.41 This module implements part of the Docker Engine API v1.41
([documentation](https://docs.docker.com/engine/api/v1.41/)) using socket-based ([documentation](https://docs.docker.com/engine/api/v1.41/)) using socket-based
HTTP communication. HTTP communication.

View File

@ -58,9 +58,9 @@ pub fn (mut r ChunkedResponseReader) read(mut buf []u8) ?int {
// This function should only be called if the previous chunk has been // This function should only be called if the previous chunk has been
// completely consumed. // completely consumed.
fn (mut r ChunkedResponseReader) read_chunk_size() ?u64 { fn (mut r ChunkedResponseReader) read_chunk_size() ?u64 {
if r.started {
mut buf := []u8{len: 2} mut buf := []u8{len: 2}
if r.started {
// Each chunk ends with a `\r\n` which we want to skip first // Each chunk ends with a `\r\n` which we want to skip first
r.reader.read(mut buf)? r.reader.read(mut buf)?
} }

13
src/env/env.v vendored
View File

@ -3,12 +3,13 @@ module env
import os import os
import toml import toml
// The prefix that every environment variable should have const (
const prefix = 'VIETER_' // The prefix that every environment variable should have
prefix = 'VIETER_'
// The suffix an environment variable in order for it to be loaded from a file // The suffix an environment variable in order for it to be loaded from a file
// instead // instead
const file_suffix = '_FILE' file_suffix = '_FILE'
)
fn get_env_var(field_name string) ?string { fn get_env_var(field_name string) ?string {
env_var_name := '$env.prefix$field_name.to_upper()' env_var_name := '$env.prefix$field_name.to_upper()'

View File

@ -0,0 +1,2 @@
This module defines a few useful functions used throughout the codebase that
don't specifically fit inside a module.

95
src/util/stream.v 100644
View File

@ -0,0 +1,95 @@
// Functions for interacting with `io.Reader` & `io.Writer` objects.
module util
import io
import os
// reader_to_writer tries to consume the entire reader & write it to the writer.
pub fn reader_to_writer(mut reader io.Reader, mut writer io.Writer) ? {
mut buf := []u8{len: 10 * 1024}
for {
bytes_read := reader.read(mut buf) or { break }
mut bytes_written := 0
for bytes_written < bytes_read {
c := writer.write(buf[bytes_written..bytes_read]) or { break }
bytes_written += c
}
}
}
// reader_to_file writes the contents of a BufferedReader to a file
pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
mut file := os.create(path)?
defer {
file.close()
}
mut buf := []u8{len: reader_buf_size}
mut bytes_left := length
// Repeat as long as the stream still has data
for bytes_left > 0 {
// TODO check if just breaking here is safe
bytes_read := reader.read(mut buf) or { break }
bytes_left -= bytes_read
mut to_write := bytes_read
for to_write > 0 {
// TODO don't just loop infinitely here
bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue }
// file.flush()
to_write = to_write - bytes_written
}
}
}
// match_array_in_array<T> returns how many elements of a2 overlap with a1. For
// example, if a1 = "abcd" & a2 = "cd", the result will be 2. If the match is
// not at the end of a1, the result is 0.
pub fn match_array_in_array<T>(a1 []T, a2 []T) int {
mut i := 0
mut match_len := 0
for i + match_len < a1.len {
if a1[i + match_len] == a2[match_len] {
match_len += 1
} else {
i += match_len + 1
match_len = 0
}
}
return match_len
}
// read_until_separator consumes an io.Reader until it encounters some
// separator array. The data read is stored inside the provided res array.
pub fn read_until_separator(mut reader io.Reader, mut res []u8, sep []u8) ? {
mut buf := []u8{len: sep.len}
for {
c := reader.read(mut buf)?
res << buf[..c]
match_len := match_array_in_array(buf[..c], sep)
if match_len == sep.len {
break
}
if match_len > 0 {
match_left := sep.len - match_len
c2 := reader.read(mut buf[..match_left])?
res << buf[..c2]
if buf[..c2] == sep[match_len..] {
break
}
}
}
}

View File

@ -1,13 +1,13 @@
module util module util
import os import os
import io
import crypto.md5 import crypto.md5
import crypto.sha256 import crypto.sha256
const reader_buf_size = 1_000_000 const (
reader_buf_size = 1_000_000
const prefixes = ['B', 'KB', 'MB', 'GB'] prefixes = ['B', 'KB', 'MB', 'GB']
)
// Dummy struct to work around the fact that you can only share structs, maps & // Dummy struct to work around the fact that you can only share structs, maps &
// arrays // arrays
@ -23,50 +23,6 @@ pub fn exit_with_message(code int, msg string) {
exit(code) exit(code)
} }
// reader_to_writer tries to consume the entire reader & write it to the writer.
pub fn reader_to_writer(mut reader io.Reader, mut writer io.Writer) ? {
mut buf := []u8{len: 10 * 1024}
for {
bytes_read := reader.read(mut buf) or { break }
mut bytes_written := 0
for bytes_written < bytes_read {
c := writer.write(buf[bytes_written..bytes_read]) or { break }
bytes_written += c
}
}
}
// reader_to_file writes the contents of a BufferedReader to a file
pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
mut file := os.create(path)?
defer {
file.close()
}
mut buf := []u8{len: util.reader_buf_size}
mut bytes_left := length
// Repeat as long as the stream still has data
for bytes_left > 0 {
// TODO check if just breaking here is safe
bytes_read := reader.read(mut buf) or { break }
bytes_left -= bytes_read
mut to_write := bytes_read
for to_write > 0 {
// TODO don't just loop infinitely here
bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue }
// file.flush()
to_write = to_write - bytes_written
}
}
}
// hash_file returns the md5 & sha256 hash of a given file // hash_file returns the md5 & sha256 hash of a given file
// TODO actually implement sha256 // TODO actually implement sha256
pub fn hash_file(path &string) ?(string, string) { pub fn hash_file(path &string) ?(string, string) {
@ -108,49 +64,3 @@ pub fn pretty_bytes(bytes int) string {
return '${n:.2}${util.prefixes[i]}' return '${n:.2}${util.prefixes[i]}'
} }
// match_array_in_array<T> returns how many elements of a2 overlap with a1. For
// example, if a1 = "abcd" & a2 = "cd", the result will be 2. If the match is
// not at the end of a1, the result is 0.
pub fn match_array_in_array<T>(a1 []T, a2 []T) int {
mut i := 0
mut match_len := 0
for i + match_len < a1.len {
if a1[i + match_len] == a2[match_len] {
match_len += 1
} else {
i += match_len + 1
match_len = 0
}
}
return match_len
}
// read_until_separator consumes an io.Reader until it encounters some
// separator array. The data read is stored inside the provided res array.
pub fn read_until_separator(mut reader io.Reader, mut res []u8, sep []u8) ? {
mut buf := []u8{len: sep.len}
for {
c := reader.read(mut buf)?
res << buf[..c]
match_len := match_array_in_array(buf[..c], sep)
if match_len == sep.len {
break
}
if match_len > 0 {
match_left := sep.len - match_len
c2 := reader.read(mut buf[..match_left])?
res << buf[..c2]
if buf[..c2] == sep[match_len..] {
break
}
}
}
}