2022-01-09 09:29:11 +01:00
|
|
|
module main
|
|
|
|
|
|
|
|
import os
|
2022-01-09 21:45:48 +01:00
|
|
|
import io
|
2022-01-09 09:29:11 +01:00
|
|
|
|
|
|
|
[noreturn]
|
|
|
|
fn exit_with_message(code int, msg string) {
|
|
|
|
eprintln(msg)
|
|
|
|
exit(code)
|
|
|
|
}
|
|
|
|
|
2022-01-09 22:18:04 +01:00
|
|
|
fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
|
2022-01-09 21:45:48 +01:00
|
|
|
mut file := os.create(path) ?
|
|
|
|
defer {
|
|
|
|
file.close()
|
|
|
|
}
|
2022-01-09 17:44:47 +01:00
|
|
|
|
2022-01-09 21:45:48 +01:00
|
|
|
mut buf := []byte{len: buf_size}
|
2022-01-09 22:18:04 +01:00
|
|
|
mut bytes_left := length
|
2022-01-09 17:44:47 +01:00
|
|
|
|
2022-01-09 21:45:48 +01:00
|
|
|
// Repeat as long as the stream still has data
|
2022-01-09 22:18:04 +01:00
|
|
|
for bytes_left > 0 {
|
2022-01-09 21:45:48 +01:00
|
|
|
// TODO check if just breaking here is safe
|
2022-01-09 22:18:04 +01:00
|
|
|
bytes_read := reader.read(mut buf) or { break }
|
|
|
|
bytes_left -= bytes_read
|
2022-01-09 17:44:47 +01:00
|
|
|
|
2022-01-09 21:45:48 +01:00
|
|
|
mut to_write := bytes_read
|
2022-01-09 17:44:47 +01:00
|
|
|
|
2022-01-09 21:45:48 +01:00
|
|
|
for to_write > 0 {
|
|
|
|
// TODO don't just loop infinitely here
|
2022-01-09 22:18:04 +01:00
|
|
|
bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue }
|
2022-01-09 21:45:48 +01:00
|
|
|
|
|
|
|
to_write = to_write - bytes_written
|
2022-01-09 17:44:47 +01:00
|
|
|
}
|
2022-01-09 10:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 19:20:14 +01:00
|
|
|
fn main() {
|
2022-02-19 21:41:26 +01:00
|
|
|
if os.args.len == 1 {
|
|
|
|
exit_with_message(1, 'No action provided.')
|
|
|
|
}
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-19 21:41:26 +01:00
|
|
|
match os.args[1] {
|
2022-02-21 20:40:13 +01:00
|
|
|
'server' { server() ? }
|
|
|
|
'build' { build() ? }
|
2022-02-19 21:41:26 +01:00
|
|
|
else { exit_with_message(1, 'Unknown action: ${os.args[1]}') }
|
|
|
|
}
|
2022-01-13 19:20:14 +01:00
|
|
|
}
|