Merge pull request 'Update to V 0.3.2' (#282) from Chewing_Bever/vieter:v-0.3.2 into dev

Reviewed-on: vieter-v/vieter#282
renovate/rouge-4.x
Jef Roosens 2022-11-02 18:59:48 +01:00
commit aff6dff06a
43 changed files with 325 additions and 307 deletions

1
.gitignore vendored
View File

@ -26,6 +26,7 @@ gdb.txt
# Generated docs # Generated docs
_docs/ _docs/
docs/resources/_gen/
/man/ /man/
# VLS logs # VLS logs

View File

@ -9,7 +9,7 @@ skip_clone: true
pipeline: pipeline:
build: build:
image: 'menci/archlinuxarm:base-devel' image: 'git.rustybever.be/vieter-v/vieter-builder'
commands: commands:
# Add the vieter repository so we can use the compiler # Add the vieter repository so we can use the compiler
- echo -e '[vieter]\nServer = https://arch.r8r.be/$repo/$arch\nSigLevel = Optional' >> /etc/pacman.conf - echo -e '[vieter]\nServer = https://arch.r8r.be/$repo/$arch\nSigLevel = Optional' >> /etc/pacman.conf

View File

@ -9,7 +9,7 @@ skip_clone: true
pipeline: pipeline:
build: build:
image: 'menci/archlinuxarm:base-devel' image: 'git.rustybever.be/vieter-v/vieter-builder'
commands: commands:
# Add the vieter repository so we can use the compiler # Add the vieter repository so we can use the compiler
- echo -e '[vieter]\nServer = https://arch.r8r.be/$repo/$arch\nSigLevel = Optional' >> /etc/pacman.conf - echo -e '[vieter]\nServer = https://arch.r8r.be/$repo/$arch\nSigLevel = Optional' >> /etc/pacman.conf

View File

@ -1,5 +1,5 @@
variables: variables:
- &vlang_image 'chewingbever/vlang:0.3' - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2'
matrix: matrix:
PLATFORM: PLATFORM:

View File

@ -1,5 +1,5 @@
variables: variables:
- &vlang_image 'chewingbever/vlang:0.3' - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2'
platform: 'linux/amd64' platform: 'linux/amd64'
branches: branches:

View File

@ -1,5 +1,5 @@
variables: variables:
- &vlang_image 'chewingbever/vlang:0.3' - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2'
platform: 'linux/amd64' platform: 'linux/amd64'
branches: [ 'main' ] branches: [ 'main' ]

View File

@ -1,5 +1,5 @@
variables: variables:
- &vlang_image 'chewingbever/vlang:0.3' - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2'
# These checks already get performed on the feature branches # These checks already get performed on the feature branches
branches: branches:
@ -7,10 +7,21 @@ branches:
platform: 'linux/amd64' platform: 'linux/amd64'
pipeline: pipeline:
# vfmt seems to get confused if these aren't present
install-modules:
image: *vlang_image
pull: true
commands:
- export VMODULES=$PWD/.vmodules
- 'cd src && v install'
when:
event: [pull_request]
lint: lint:
image: *vlang_image image: *vlang_image
pull: true pull: true
commands: commands:
- export VMODULES=$PWD/.vmodules
- make lint - make lint
when: when:
event: [pull_request] event: [pull_request]

View File

@ -1,5 +1,5 @@
variables: variables:
- &vlang_image 'chewingbever/vlang:0.3' - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2'
platform: 'linux/amd64' platform: 'linux/amd64'
branches: branches:

View File

@ -1,5 +1,5 @@
variables: variables:
- &vlang_image 'chewingbever/vlang:0.3' - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2'
matrix: matrix:
PLATFORM: PLATFORM:

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://git.rustybever.be/vieter-v/vieter/src/branch/dev) ## [Unreleased](https://git.rustybever.be/vieter-v/vieter/src/branch/dev)
### Changed
* Migrated codebase to V 0.3.2
## [0.4.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.4.0) ## [0.4.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.4.0)
### Added ### Added

View File

@ -1,4 +1,4 @@
FROM chewingbever/vlang:0.3 AS builder FROM git.rustybever.be/chewing_bever/vlang:0.3.2 AS builder
ARG TARGETPLATFORM ARG TARGETPLATFORM
ARG CI_COMMIT_SHA ARG CI_COMMIT_SHA

View File

@ -3,7 +3,7 @@ SRC_DIR := src
SOURCES != find '$(SRC_DIR)' -iname '*.v' SOURCES != find '$(SRC_DIR)' -iname '*.v'
V_PATH ?= v V_PATH ?= v
V := $(V_PATH) -showcc -gc boehm V := $(V_PATH) -showcc -gc boehm -W -d use_openssl
all: vieter all: vieter
@ -92,9 +92,9 @@ clean:
.PHONY: autofree .PHONY: autofree
autofree: afvieter autofree: afvieter
afvieter: $(SOURCES) afvieter: $(SOURCES)
$(V_PATH) -showcc -autofree -o afvieter $(SRC_DIR) $(V) -showcc -autofree -o afvieter $(SRC_DIR)
.PHONY: skip-unused .PHONY: skip-unused
skip-unused: suvieter skip-unused: suvieter
suvieter: $(SOURCES) suvieter: $(SOURCES)
$(V_PATH) -showcc -skip-unused -o suvieter $(SRC_DIR) $(V) -skip-unused -o suvieter $(SRC_DIR)

View File

@ -1,6 +1,6 @@
module build module build
import vieter_v.docker import docker
import encoding.base64 import encoding.base64
import time import time
import os import os
@ -21,8 +21,8 @@ const (
// system, install some necessary packages & creates a non-root user to run // system, install some necessary packages & creates a non-root user to run
// makepkg with. The base image should be some Linux distribution that uses // makepkg with. The base image should be some Linux distribution that uses
// Pacman as its package manager. // Pacman as its package manager.
pub fn create_build_image(base_image string) ?string { pub fn create_build_image(base_image string) !string {
mut dd := docker.new_conn()? mut dd := docker.new_conn()!
defer { defer {
dd.close() or {} dd.close() or {}
@ -57,15 +57,15 @@ pub fn create_build_image(base_image string) ?string {
image_tag := if image_parts.len > 1 { image_parts[1] } else { 'latest' } image_tag := if image_parts.len > 1 { image_parts[1] } else { 'latest' }
// We pull the provided image // We pull the provided image
dd.pull_image(image_name, image_tag)? dd.pull_image(image_name, image_tag)!
id := dd.container_create(c)?.id id := dd.container_create(c)!.id
// id := docker.create_container(c)? // id := docker.create_container(c)!
dd.container_start(id)? dd.container_start(id)!
// This loop waits until the container has stopped, so we can remove it after // This loop waits until the container has stopped, so we can remove it after
for { for {
data := dd.container_inspect(id)? data := dd.container_inspect(id)!
if !data.state.running { if !data.state.running {
break break
@ -79,8 +79,8 @@ pub fn create_build_image(base_image string) ?string {
// TODO also add the base image's name into the image name to prevent // TODO also add the base image's name into the image name to prevent
// conflicts. // conflicts.
tag := time.sys_mono_now().str() tag := time.sys_mono_now().str()
image := dd.create_image_from_container(id, 'vieter-build', tag)? image := dd.create_image_from_container(id, 'vieter-build', tag)!
dd.container_remove(id)? dd.container_remove(id)!
return image.id return image.id
} }
@ -96,8 +96,8 @@ pub:
// build_target builds, packages & publishes a given Arch package based on the // build_target builds, packages & publishes a given Arch package based on the
// provided target. The base image ID should be of an image previously created // provided target. The base image ID should be of an image previously created
// by create_build_image. It returns the logs of the container. // by create_build_image. It returns the logs of the container.
pub fn build_target(address string, api_key string, base_image_id string, target &Target) ?BuildResult { pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult {
mut dd := docker.new_conn()? mut dd := docker.new_conn()!
defer { defer {
dd.close() or {} dd.close() or {}
@ -125,25 +125,25 @@ pub fn build_target(address string, api_key string, base_image_id string, target
user: '0:0' user: '0:0'
} }
id := dd.container_create(c)?.id id := dd.container_create(c)!.id
dd.container_start(id)? dd.container_start(id)!
mut data := dd.container_inspect(id)? mut data := dd.container_inspect(id)!
// This loop waits until the container has stopped, so we can remove it after // This loop waits until the container has stopped, so we can remove it after
for data.state.running { for data.state.running {
time.sleep(1 * time.second) time.sleep(1 * time.second)
data = dd.container_inspect(id)? data = dd.container_inspect(id)!
} }
mut logs_stream := dd.container_get_logs(id)? mut logs_stream := dd.container_get_logs(id)!
// Read in the entire stream // Read in the entire stream
mut logs_builder := strings.new_builder(10 * 1024) mut logs_builder := strings.new_builder(10 * 1024)
util.reader_to_writer(mut logs_stream, mut logs_builder)? util.reader_to_writer(mut logs_stream, mut logs_builder)!
dd.container_remove(id)? dd.container_remove(id)!
return BuildResult{ return BuildResult{
start_time: data.state.start_time start_time: data.state.start_time

View File

@ -21,7 +21,7 @@ pub fn new(address string, api_key string) Client {
// send_request_raw sends an HTTP request, returning the http.Response object. // send_request_raw sends an HTTP request, returning the http.Response object.
// It encodes the params so that they're safe to pass as HTTP query parameters. // It encodes the params so that they're safe to pass as HTTP query parameters.
fn (c &Client) send_request_raw(method Method, url string, params map[string]string, body string) ?http.Response { fn (c &Client) send_request_raw(method Method, url string, params map[string]string, body string) !http.Response {
mut full_url := '$c.address$url' mut full_url := '$c.address$url'
if params.len > 0 { if params.len > 0 {
@ -38,31 +38,33 @@ fn (c &Client) send_request_raw(method Method, url string, params map[string]str
full_url = '$full_url?$params_str' full_url = '$full_url?$params_str'
} }
mut req := http.new_request(method, full_url, body)? // Looking at the source code, this function doesn't actually fail, so I'm
req.add_custom_header('X-Api-Key', c.api_key)? // not sure why it returns an optional
mut req := http.new_request(method, full_url, body) or { return error('') }
req.add_custom_header('X-Api-Key', c.api_key)!
res := req.do()? res := req.do()!
return res return res
} }
// send_request<T> just calls send_request_with_body<T> with an empty body. // send_request<T> just calls send_request_with_body<T> with an empty body.
fn (c &Client) send_request<T>(method Method, url string, params map[string]string) ?Response<T> { fn (c &Client) send_request<T>(method Method, url string, params map[string]string) !Response<T> {
return c.send_request_with_body<T>(method, url, params, '') return c.send_request_with_body<T>(method, url, params, '')
} }
// send_request_with_body<T> calls send_request_raw_response & parses its // send_request_with_body<T> calls send_request_raw_response & parses its
// output as a Response<T> object. // output as a Response<T> object.
fn (c &Client) send_request_with_body<T>(method Method, url string, params map[string]string, body string) ?Response<T> { fn (c &Client) send_request_with_body<T>(method Method, url string, params map[string]string, body string) !Response<T> {
res_text := c.send_request_raw_response(method, url, params, body)? res_text := c.send_request_raw_response(method, url, params, body)!
data := json.decode(Response<T>, res_text)? data := json.decode(Response<T>, res_text)!
return data return data
} }
// send_request_raw_response returns the raw text response for an HTTP request. // send_request_raw_response returns the raw text response for an HTTP request.
fn (c &Client) send_request_raw_response(method Method, url string, params map[string]string, body string) ?string { fn (c &Client) send_request_raw_response(method Method, url string, params map[string]string, body string) !string {
res := c.send_request_raw(method, url, params, body)? res := c.send_request_raw(method, url, params, body)!
return res.body return res.body
} }

View File

@ -6,40 +6,40 @@ import web.response { Response }
import time import time
// get_build_logs returns all build logs. // get_build_logs returns all build logs.
pub fn (c &Client) get_build_logs(filter BuildLogFilter) ?Response<[]BuildLog> { pub fn (c &Client) get_build_logs(filter BuildLogFilter) !Response<[]BuildLog> {
params := models.params_from(filter) params := models.params_from(filter)
data := c.send_request<[]BuildLog>(Method.get, '/api/v1/logs', params)? data := c.send_request<[]BuildLog>(Method.get, '/api/v1/logs', params)!
return data return data
} }
// get_build_logs_for_target returns all build logs for a given target. // get_build_logs_for_target returns all build logs for a given target.
pub fn (c &Client) get_build_logs_for_target(target_id int) ?Response<[]BuildLog> { pub fn (c &Client) get_build_logs_for_target(target_id int) !Response<[]BuildLog> {
params := { params := {
'repo': target_id.str() 'repo': target_id.str()
} }
data := c.send_request<[]BuildLog>(Method.get, '/api/v1/logs', params)? data := c.send_request<[]BuildLog>(Method.get, '/api/v1/logs', params)!
return data return data
} }
// get_build_log returns a specific build log. // get_build_log returns a specific build log.
pub fn (c &Client) get_build_log(id int) ?Response<BuildLog> { pub fn (c &Client) get_build_log(id int) !Response<BuildLog> {
data := c.send_request<BuildLog>(Method.get, '/api/v1/logs/$id', {})? data := c.send_request<BuildLog>(Method.get, '/api/v1/logs/$id', {})!
return data return data
} }
// get_build_log_content returns the contents of the build log file. // get_build_log_content returns the contents of the build log file.
pub fn (c &Client) get_build_log_content(id int) ?string { pub fn (c &Client) get_build_log_content(id int) !string {
data := c.send_request_raw_response(Method.get, '/api/v1/logs/$id/content', {}, '')? data := c.send_request_raw_response(Method.get, '/api/v1/logs/$id/content', {}, '')!
return data return data
} }
// add_build_log adds a new build log to the server. // add_build_log adds a new build log to the server.
pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time time.Time, arch string, exit_code int, content string) ?Response<int> { pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time time.Time, arch string, exit_code int, content string) !Response<int> {
params := { params := {
'target': target_id.str() 'target': target_id.str()
'startTime': start_time.unix_time().str() 'startTime': start_time.unix_time().str()
@ -48,7 +48,7 @@ pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time t
'exitCode': exit_code.str() 'exitCode': exit_code.str()
} }
data := c.send_request_with_body<int>(Method.post, '/api/v1/logs', params, content)? data := c.send_request_with_body<int>(Method.post, '/api/v1/logs', params, content)!
return data return data
} }

View File

@ -5,21 +5,21 @@ import net.http { Method }
import web.response { Response } import web.response { Response }
// get_targets returns a list of targets, given a filter object. // get_targets returns a list of targets, given a filter object.
pub fn (c &Client) get_targets(filter TargetFilter) ?[]Target { pub fn (c &Client) get_targets(filter TargetFilter) ![]Target {
params := models.params_from(filter) params := models.params_from(filter)
data := c.send_request<[]Target>(Method.get, '/api/v1/targets', params)? data := c.send_request<[]Target>(Method.get, '/api/v1/targets', params)!
return data.data return data.data
} }
// get_all_targets retrieves *all* targs from the API using the default // get_all_targets retrieves *all* targs from the API using the default
// limit. // limit.
pub fn (c &Client) get_all_targets() ?[]Target { pub fn (c &Client) get_all_targets() ![]Target {
mut targets := []Target{} mut targets := []Target{}
mut offset := u64(0) mut offset := u64(0)
for { for {
sub_targets := c.get_targets(offset: offset)? sub_targets := c.get_targets(offset: offset)!
if sub_targets.len == 0 { if sub_targets.len == 0 {
break break
@ -34,8 +34,8 @@ pub fn (c &Client) get_all_targets() ?[]Target {
} }
// get_target returns the target for a specific id. // get_target returns the target for a specific id.
pub fn (c &Client) get_target(id int) ?Target { pub fn (c &Client) get_target(id int) !Target {
data := c.send_request<Target>(Method.get, '/api/v1/targets/$id', {})? data := c.send_request<Target>(Method.get, '/api/v1/targets/$id', {})!
return data.data return data.data
} }
@ -49,24 +49,24 @@ pub struct NewTarget {
} }
// add_target adds a new target to the server. // add_target adds a new target to the server.
pub fn (c &Client) add_target(t NewTarget) ?Response<int> { pub fn (c &Client) add_target(t NewTarget) !Response<int> {
params := models.params_from<NewTarget>(t) params := models.params_from<NewTarget>(t)
data := c.send_request<int>(Method.post, '/api/v1/targets', params)? data := c.send_request<int>(Method.post, '/api/v1/targets', params)!
return data return data
} }
// remove_target removes the target with the given id from the server. // remove_target removes the target with the given id from the server.
pub fn (c &Client) remove_target(id int) ?Response<string> { pub fn (c &Client) remove_target(id int) !Response<string> {
data := c.send_request<string>(Method.delete, '/api/v1/targets/$id', {})? data := c.send_request<string>(Method.delete, '/api/v1/targets/$id', {})!
return data return data
} }
// patch_target sends a PATCH request to the given target with the params as // patch_target sends a PATCH request to the given target with the params as
// payload. // payload.
pub fn (c &Client) patch_target(id int, params map[string]string) ?Response<string> { pub fn (c &Client) patch_target(id int, params map[string]string) !Response<string> {
data := c.send_request<string>(Method.patch, '/api/v1/targets/$id', params)? data := c.send_request<string>(Method.patch, '/api/v1/targets/$id', params)!
return data return data
} }

View File

@ -3,8 +3,8 @@ module aur
import cli import cli
import console import console
import client import client
import vieter_v.aur import aur
import vieter_v.conf as vconf import conf as vconf
struct Config { struct Config {
address string [required] address string [required]
@ -21,12 +21,12 @@ pub fn cmd() cli.Command {
name: 'search' name: 'search'
description: 'Search for packages.' description: 'Search for packages.'
required_args: 1 required_args: 1
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
c := aur.new() c := aur.new()
pkgs := c.search(cmd.args[0])? pkgs := c.search(cmd.args[0])!
data := pkgs.map([it.name, it.description]) data := pkgs.map([it.name, it.description])
println(console.pretty_table(['name', 'description'], data)?) println(console.pretty_table(['name', 'description'], data)!)
} }
}, },
cli.Command{ cli.Command{
@ -34,12 +34,12 @@ pub fn cmd() cli.Command {
usage: 'repo pkg-name [pkg-name...]' usage: 'repo pkg-name [pkg-name...]'
description: 'Add the given AUR package(s) to Vieter. Non-existent packages will be silently ignored.' description: 'Add the given AUR package(s) to Vieter. Non-existent packages will be silently ignored.'
required_args: 2 required_args: 2
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
c := aur.new() c := aur.new()
pkgs := c.info(cmd.args[1..])? pkgs := c.info(cmd.args[1..])!
vc := client.new(conf.address, conf.api_key) vc := client.new(conf.address, conf.api_key)

View File

@ -13,7 +13,7 @@ pub fn tabbed_table(data [][]string) string {
// pretty_table converts a list of string data into a pretty table. Many thanks // pretty_table converts a list of string data into a pretty table. Many thanks
// to @hungrybluedev in the Vlang Discord for providing this code! // to @hungrybluedev in the Vlang Discord for providing this code!
// https://ptb.discord.com/channels/592103645835821068/592106336838352923/970278787143045192 // https://ptb.discord.com/channels/592103645835821068/592106336838352923/970278787143045192
pub fn pretty_table(header []string, data [][]string) ?string { pub fn pretty_table(header []string, data [][]string) !string {
column_count := header.len column_count := header.len
mut column_widths := []int{len: column_count, init: header[it].len} mut column_widths := []int{len: column_count, init: header[it].len}
@ -26,7 +26,7 @@ pub fn pretty_table(header []string, data [][]string) ?string {
} }
} }
single_line_length := arrays.sum(column_widths)? + (column_count + 1) * 3 - 4 single_line_length := arrays.sum(column_widths)! + (column_count + 1) * 3 - 4
horizontal_line := '+' + strings.repeat(`-`, single_line_length) + '+' horizontal_line := '+' + strings.repeat(`-`, single_line_length) + '+'
mut buffer := strings.new_builder(data.len * single_line_length) mut buffer := strings.new_builder(data.len * single_line_length)
@ -64,12 +64,12 @@ pub fn pretty_table(header []string, data [][]string) ?string {
// export_man_pages recursively generates all man pages for the given // export_man_pages recursively generates all man pages for the given
// cli.Command & writes them to the given directory. // cli.Command & writes them to the given directory.
pub fn export_man_pages(cmd cli.Command, path string) ? { pub fn export_man_pages(cmd cli.Command, path string) ! {
man := cmd.manpage() man := cmd.manpage()
os.write_file(os.join_path_single(path, cmd.full_name().replace(' ', '-') + '.1'), os.write_file(os.join_path_single(path, cmd.full_name().replace(' ', '-') + '.1'),
man)? man)!
for sub_cmd in cmd.commands { for sub_cmd in cmd.commands {
export_man_pages(sub_cmd, path)? export_man_pages(sub_cmd, path)!
} }
} }

View File

@ -1,7 +1,7 @@
module logs module logs
import cli import cli
import vieter_v.conf as vconf import conf as vconf
import client import client
import console import console
import time import time
@ -63,30 +63,30 @@ pub fn cmd() cli.Command {
flag: cli.FlagType.string flag: cli.FlagType.string
}, },
] ]
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
mut filter := BuildLogFilter{} mut filter := BuildLogFilter{}
limit := cmd.flags.get_int('limit')? limit := cmd.flags.get_int('limit')!
if limit != 0 { if limit != 0 {
filter.limit = u64(limit) filter.limit = u64(limit)
} }
offset := cmd.flags.get_int('offset')? offset := cmd.flags.get_int('offset')!
if offset != 0 { if offset != 0 {
filter.offset = u64(offset) filter.offset = u64(offset)
} }
target_id := cmd.flags.get_int('target')? target_id := cmd.flags.get_int('target')!
if target_id != 0 { if target_id != 0 {
filter.target = target_id filter.target = target_id
} }
tz_offset := time.offset() tz_offset := time.offset()
if cmd.flags.get_bool('today')? { if cmd.flags.get_bool('today')! {
today := time.now() today := time.now()
filter.after = time.new_time(time.Time{ filter.after = time.new_time(time.Time{
@ -98,12 +98,12 @@ pub fn cmd() cli.Command {
} }
// The -today flag overwrites any of the other date flags. // The -today flag overwrites any of the other date flags.
else { else {
day_str := cmd.flags.get_string('day')? day_str := cmd.flags.get_string('day')!
before_str := cmd.flags.get_string('before')? before_str := cmd.flags.get_string('before')!
after_str := cmd.flags.get_string('after')? after_str := cmd.flags.get_string('after')!
if day_str != '' { if day_str != '' {
day := time.parse_rfc3339(day_str)? day := time.parse_rfc3339(day_str)!
day_utc := time.new_time(time.Time{ day_utc := time.new_time(time.Time{
year: day.year year: day.year
month: day.month month: day.month
@ -118,24 +118,24 @@ pub fn cmd() cli.Command {
filter.before = day_utc.add_days(1) filter.before = day_utc.add_days(1)
} else { } else {
if before_str != '' { if before_str != '' {
filter.before = time.parse(before_str)?.add_seconds(-tz_offset) filter.before = time.parse(before_str)!.add_seconds(-tz_offset)
} }
if after_str != '' { if after_str != '' {
filter.after = time.parse(after_str)?.add_seconds(-tz_offset) filter.after = time.parse(after_str)!.add_seconds(-tz_offset)
} }
} }
} }
if cmd.flags.get_bool('failed')? { if cmd.flags.get_bool('failed')! {
filter.exit_codes = [ filter.exit_codes = [
'!0', '!0',
] ]
} }
raw := cmd.flags.get_bool('raw')? raw := cmd.flags.get_bool('raw')!
list(conf, filter, raw)? list(conf, filter, raw)!
} }
}, },
cli.Command{ cli.Command{
@ -143,12 +143,12 @@ pub fn cmd() cli.Command {
required_args: 1 required_args: 1
usage: 'id' usage: 'id'
description: 'Show all info for a specific build log.' description: 'Show all info for a specific build log.'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
id := cmd.args[0].int() id := cmd.args[0].int()
info(conf, id)? info(conf, id)!
} }
}, },
cli.Command{ cli.Command{
@ -156,12 +156,12 @@ pub fn cmd() cli.Command {
required_args: 1 required_args: 1
usage: 'id' usage: 'id'
description: 'Output the content of a build log to stdout.' description: 'Output the content of a build log to stdout.'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
id := cmd.args[0].int() id := cmd.args[0].int()
content(conf, id)? content(conf, id)!
} }
}, },
] ]
@ -169,46 +169,46 @@ pub fn cmd() cli.Command {
} }
// print_log_list prints a list of logs. // print_log_list prints a list of logs.
fn print_log_list(logs []BuildLog, raw bool) ? { fn print_log_list(logs []BuildLog, raw bool) ! {
data := logs.map([it.id.str(), it.target_id.str(), it.start_time.local().str(), data := logs.map([it.id.str(), it.target_id.str(), it.start_time.local().str(),
it.exit_code.str()]) it.exit_code.str()])
if raw { if raw {
println(console.tabbed_table(data)) println(console.tabbed_table(data))
} else { } else {
println(console.pretty_table(['id', 'target', 'start time', 'exit code'], data)?) println(console.pretty_table(['id', 'target', 'start time', 'exit code'], data)!)
} }
} }
// list prints a list of all build logs. // list prints a list of all build logs.
fn list(conf Config, filter BuildLogFilter, raw bool) ? { fn list(conf Config, filter BuildLogFilter, raw bool) ! {
c := client.new(conf.address, conf.api_key) c := client.new(conf.address, conf.api_key)
logs := c.get_build_logs(filter)?.data logs := c.get_build_logs(filter)!.data
print_log_list(logs, raw)? print_log_list(logs, raw)!
} }
// list prints a list of all build logs for a given target. // list prints a list of all build logs for a given target.
fn list_for_target(conf Config, target_id int, raw bool) ? { fn list_for_target(conf Config, target_id int, raw bool) ! {
c := client.new(conf.address, conf.api_key) c := client.new(conf.address, conf.api_key)
logs := c.get_build_logs_for_target(target_id)?.data logs := c.get_build_logs_for_target(target_id)!.data
print_log_list(logs, raw)? print_log_list(logs, raw)!
} }
// info print the detailed info for a given build log. // info print the detailed info for a given build log.
fn info(conf Config, id int) ? { fn info(conf Config, id int) ! {
c := client.new(conf.address, conf.api_key) c := client.new(conf.address, conf.api_key)
log := c.get_build_log(id)?.data log := c.get_build_log(id)!.data
print(log) print(log)
} }
// content outputs the contents of the log file for a given build log to // content outputs the contents of the log file for a given build log to
// stdout. // stdout.
fn content(conf Config, id int) ? { fn content(conf Config, id int) ! {
c := client.new(conf.address, conf.api_key) c := client.new(conf.address, conf.api_key)
content := c.get_build_log_content(id)? content := c.get_build_log_content(id)!
println(content) println(content)
} }

View File

@ -11,11 +11,11 @@ pub fn cmd() cli.Command {
description: 'Generate all man pages & save them in the given directory.' description: 'Generate all man pages & save them in the given directory.'
usage: 'dir' usage: 'dir'
required_args: 1 required_args: 1
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
root := cmd.root() root := cmd.root()
os.mkdir_all(cmd.args[0])? os.mkdir_all(cmd.args[0])!
console.export_man_pages(root, cmd.args[0])? console.export_man_pages(root, cmd.args[0])!
} }
} }
} }

View File

@ -18,11 +18,11 @@ pub fn cmd() cli.Command {
default_value: ['5'] default_value: ['5']
}, },
] ]
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
ce := parse_expression(cmd.args.join(' '))? ce := parse_expression(cmd.args.join(' '))!
count := cmd.flags.get_int('count')? count := cmd.flags.get_int('count')!
for t in ce.next_n(time.now(), count)? { for t in ce.next_n(time.now(), count)! {
println(t) println(t)
} }
} }

View File

@ -1,34 +1,34 @@
module targets module targets
import client import client
import vieter_v.docker import docker
import os import os
import build import build
// build locally builds the target with the given id. // build locally builds the target with the given id.
fn build(conf Config, target_id int) ? { fn build(conf Config, target_id int) ! {
c := client.new(conf.address, conf.api_key) c := client.new(conf.address, conf.api_key)
target := c.get_target(target_id)? target := c.get_target(target_id)!
build_arch := os.uname().machine build_arch := os.uname().machine
println('Creating base image...') println('Creating base image...')
image_id := build.create_build_image(conf.base_image)? image_id := build.create_build_image(conf.base_image)!
println('Running build...') println('Running build...')
res := build.build_target(conf.address, conf.api_key, image_id, target)? res := build.build_target(conf.address, conf.api_key, image_id, target)!
println('Removing build image...') println('Removing build image...')
mut dd := docker.new_conn()? mut dd := docker.new_conn()!
defer { defer {
dd.close() or {} dd.close() or {}
} }
dd.remove_image(image_id)? dd.remove_image(image_id)!
println('Uploading logs to Vieter...') println('Uploading logs to Vieter...')
c.add_build_log(target.id, res.start_time, res.end_time, build_arch, res.exit_code, c.add_build_log(target.id, res.start_time, res.end_time, build_arch, res.exit_code,
res.logs)? res.logs)!
} }

View File

@ -1,7 +1,7 @@
module targets module targets
import cli import cli
import vieter_v.conf as vconf import conf as vconf
import cron.expression { parse_expression } import cron.expression { parse_expression }
import client { NewTarget } import client { NewTarget }
import console import console
@ -39,30 +39,30 @@ pub fn cmd() cli.Command {
flag: cli.FlagType.string flag: cli.FlagType.string
}, },
] ]
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
mut filter := TargetFilter{} mut filter := TargetFilter{}
limit := cmd.flags.get_int('limit')? limit := cmd.flags.get_int('limit')!
if limit != 0 { if limit != 0 {
filter.limit = u64(limit) filter.limit = u64(limit)
} }
offset := cmd.flags.get_int('offset')? offset := cmd.flags.get_int('offset')!
if offset != 0 { if offset != 0 {
filter.offset = u64(offset) filter.offset = u64(offset)
} }
repo := cmd.flags.get_string('repo')? repo := cmd.flags.get_string('repo')!
if repo != '' { if repo != '' {
filter.repo = repo filter.repo = repo
} }
raw := cmd.flags.get_bool('raw')? raw := cmd.flags.get_bool('raw')!
list(conf, filter, raw)? list(conf, filter, raw)!
} }
}, },
cli.Command{ cli.Command{
@ -83,20 +83,20 @@ pub fn cmd() cli.Command {
flag: cli.FlagType.string flag: cli.FlagType.string
}, },
] ]
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
t := NewTarget{ t := NewTarget{
kind: cmd.flags.get_string('kind')? kind: cmd.flags.get_string('kind')!
url: cmd.args[0] url: cmd.args[0]
repo: cmd.args[1] repo: cmd.args[1]
branch: cmd.flags.get_string('branch') or { '' } branch: cmd.flags.get_string('branch') or { '' }
} }
raw := cmd.flags.get_bool('raw')? raw := cmd.flags.get_bool('raw')!
add(conf, t, raw)? add(conf, t, raw)!
} }
}, },
cli.Command{ cli.Command{
@ -104,11 +104,11 @@ pub fn cmd() cli.Command {
required_args: 1 required_args: 1
usage: 'id' usage: 'id'
description: 'Remove a target that matches the given id.' description: 'Remove a target that matches the given id.'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
remove(conf, cmd.args[0])? remove(conf, cmd.args[0])!
} }
}, },
cli.Command{ cli.Command{
@ -116,11 +116,11 @@ pub fn cmd() cli.Command {
required_args: 1 required_args: 1
usage: 'id' usage: 'id'
description: 'Show detailed information for the target matching the id.' description: 'Show detailed information for the target matching the id.'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
info(conf, cmd.args[0])? info(conf, cmd.args[0])!
} }
}, },
cli.Command{ cli.Command{
@ -160,9 +160,9 @@ pub fn cmd() cli.Command {
flag: cli.FlagType.string flag: cli.FlagType.string
}, },
] ]
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
found := cmd.flags.get_all_found() found := cmd.flags.get_all_found()
@ -170,11 +170,11 @@ pub fn cmd() cli.Command {
for f in found { for f in found {
if f.name != 'config-file' { if f.name != 'config-file' {
params[f.name] = f.get_string()? params[f.name] = f.get_string()!
} }
} }
patch(conf, cmd.args[0], params)? patch(conf, cmd.args[0], params)!
} }
}, },
cli.Command{ cli.Command{
@ -182,11 +182,11 @@ pub fn cmd() cli.Command {
required_args: 1 required_args: 1
usage: 'id' usage: 'id'
description: 'Build the target with the given id & publish it.' description: 'Build the target with the given id & publish it.'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
build(conf, cmd.args[0].int())? build(conf, cmd.args[0].int())!
} }
}, },
] ]
@ -197,22 +197,22 @@ pub fn cmd() cli.Command {
// ID. If multiple or none are found, an error is raised. // ID. If multiple or none are found, an error is raised.
// list prints out a list of all repositories. // list prints out a list of all repositories.
fn list(conf Config, filter TargetFilter, raw bool) ? { fn list(conf Config, filter TargetFilter, raw bool) ! {
c := client.new(conf.address, conf.api_key) c := client.new(conf.address, conf.api_key)
repos := c.get_targets(filter)? repos := c.get_targets(filter)!
data := repos.map([it.id.str(), it.kind, it.url, it.repo]) data := repos.map([it.id.str(), it.kind, it.url, it.repo])
if raw { if raw {
println(console.tabbed_table(data)) println(console.tabbed_table(data))
} else { } else {
println(console.pretty_table(['id', 'kind', 'url', 'repo'], data)?) println(console.pretty_table(['id', 'kind', 'url', 'repo'], data)!)
} }
} }
// add adds a new repository to the server's list. // add adds a new repository to the server's list.
fn add(conf Config, t &NewTarget, raw bool) ? { fn add(conf Config, t &NewTarget, raw bool) ! {
c := client.new(conf.address, conf.api_key) c := client.new(conf.address, conf.api_key)
res := c.add_target(t)? res := c.add_target(t)!
if raw { if raw {
println(res.data) println(res.data)
@ -222,18 +222,18 @@ fn add(conf Config, t &NewTarget, raw bool) ? {
} }
// remove removes a repository from the server's list. // remove removes a repository from the server's list.
fn remove(conf Config, id string) ? { fn remove(conf Config, id string) ! {
id_int := id.int() id_int := id.int()
if id_int != 0 { if id_int != 0 {
c := client.new(conf.address, conf.api_key) c := client.new(conf.address, conf.api_key)
res := c.remove_target(id_int)? res := c.remove_target(id_int)!
println(res.message) println(res.message)
} }
} }
// patch patches a given repository with the provided params. // patch patches a given repository with the provided params.
fn patch(conf Config, id string, params map[string]string) ? { fn patch(conf Config, id string, params map[string]string) ! {
// We check the cron expression first because it's useless to send an // We check the cron expression first because it's useless to send an
// invalid one to the server. // invalid one to the server.
if 'schedule' in params && params['schedule'] != '' { if 'schedule' in params && params['schedule'] != '' {
@ -245,14 +245,14 @@ fn patch(conf Config, id string, params map[string]string) ? {
id_int := id.int() id_int := id.int()
if id_int != 0 { if id_int != 0 {
c := client.new(conf.address, conf.api_key) c := client.new(conf.address, conf.api_key)
res := c.patch_target(id_int, params)? res := c.patch_target(id_int, params)!
println(res.message) println(res.message)
} }
} }
// info shows detailed information for a given repo. // info shows detailed information for a given repo.
fn info(conf Config, id string) ? { fn info(conf Config, id string) ! {
id_int := id.int() id_int := id.int()
if id_int == 0 { if id_int == 0 {
@ -260,6 +260,6 @@ fn info(conf Config, id string) ? {
} }
c := client.new(conf.address, conf.api_key) c := client.new(conf.address, conf.api_key)
repo := c.get_target(id_int)? repo := c.get_target(id_int)!
println(repo) println(repo)
} }

View File

@ -1,7 +1,7 @@
module cron module cron
import cli import cli
import vieter_v.conf as vconf import conf as vconf
struct Config { struct Config {
pub: pub:
@ -22,11 +22,11 @@ pub fn cmd() cli.Command {
return cli.Command{ return cli.Command{
name: 'cron' name: 'cron'
description: 'Start the cron service that periodically runs builds.' description: 'Start the cron service that periodically runs builds.'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
cron(conf)? cron(conf)!
} }
} }
} }

View File

@ -8,7 +8,7 @@ import os
const log_file_name = 'vieter.cron.log' const log_file_name = 'vieter.cron.log'
// cron starts a cron daemon & starts periodically scheduling builds. // cron starts a cron daemon & starts periodically scheduling builds.
pub fn cron(conf Config) ? { pub fn cron(conf Config) ! {
// Configure logger // Configure logger
log_level := log.level_from_tag(conf.log_level) or { log_level := log.level_from_tag(conf.log_level) or {
return error('Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') return error('Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.')
@ -27,7 +27,7 @@ pub fn cron(conf Config) ? {
} }
mut d := daemon.init_daemon(logger, conf.address, conf.api_key, conf.base_image, ce, mut d := daemon.init_daemon(logger, conf.address, conf.api_key, conf.base_image, ce,
conf.max_concurrent_builds, conf.api_update_frequency, conf.image_rebuild_frequency)? conf.max_concurrent_builds, conf.api_update_frequency, conf.image_rebuild_frequency)!
d.run() d.run()
} }

View File

@ -6,7 +6,7 @@ import datatypes { MinHeap }
import cron.expression { CronExpression, parse_expression } import cron.expression { CronExpression, parse_expression }
import math import math
import build import build
import vieter_v.docker import docker
import os import os
import client import client
import models { Target } import models { Target }
@ -53,7 +53,7 @@ mut:
// init_daemon initializes a new Daemon object. It renews the targets & // init_daemon initializes a new Daemon object. It renews the targets &
// populates the build queue for the first time. // populates the build queue for the first time.
pub fn init_daemon(logger log.Log, address string, api_key string, base_image string, global_schedule CronExpression, max_concurrent_builds int, api_update_frequency int, image_rebuild_frequency int) ?Daemon { pub fn init_daemon(logger log.Log, address string, api_key string, base_image string, global_schedule CronExpression, max_concurrent_builds int, api_update_frequency int, image_rebuild_frequency int) !Daemon {
mut d := Daemon{ mut d := Daemon{
client: client.new(address, api_key) client: client.new(address, api_key)
base_image: base_image base_image: base_image
@ -207,7 +207,7 @@ fn (mut d Daemon) renew_queue() {
// For some reason, using // For some reason, using
// ```v // ```v
// for d.queue.len() > 0 && d.queue.peek() ?.timestamp < now { // for d.queue.len() > 0 && d.queue.peek() !.timestamp < now {
//``` //```
// here causes the function to prematurely just exit, without any errors or anything, very weird // here causes the function to prematurely just exit, without any errors or anything, very weird
// https://github.com/vlang/v/issues/14042 // https://github.com/vlang/v/issues/14042

View File

@ -3,33 +3,33 @@ module daemon
import log import log
// log reate a log message with the given level // log reate a log message with the given level
pub fn (mut d Daemon) log(msg &string, level log.Level) { pub fn (mut d Daemon) log(msg string, level log.Level) {
lock d.logger { lock d.logger {
d.logger.send_output(msg, level) d.logger.send_output(msg, level)
} }
} }
// lfatal create a log message with the fatal level // lfatal create a log message with the fatal level
pub fn (mut d Daemon) lfatal(msg &string) { pub fn (mut d Daemon) lfatal(msg string) {
d.log(msg, log.Level.fatal) d.log(msg, log.Level.fatal)
} }
// lerror create a log message with the error level // lerror create a log message with the error level
pub fn (mut d Daemon) lerror(msg &string) { pub fn (mut d Daemon) lerror(msg string) {
d.log(msg, log.Level.error) d.log(msg, log.Level.error)
} }
// lwarn create a log message with the warn level // lwarn create a log message with the warn level
pub fn (mut d Daemon) lwarn(msg &string) { pub fn (mut d Daemon) lwarn(msg string) {
d.log(msg, log.Level.warn) d.log(msg, log.Level.warn)
} }
// linfo create a log message with the info level // linfo create a log message with the info level
pub fn (mut d Daemon) linfo(msg &string) { pub fn (mut d Daemon) linfo(msg string) {
d.log(msg, log.Level.info) d.log(msg, log.Level.info)
} }
// ldebug create a log message with the debug level // ldebug create a log message with the debug level
pub fn (mut d Daemon) ldebug(msg &string) { pub fn (mut d Daemon) ldebug(msg string) {
d.log(msg, log.Level.debug) d.log(msg, log.Level.debug)
} }

View File

@ -12,7 +12,7 @@ pub struct CronExpression {
// next calculates the earliest time this cron expression is valid. It will // next calculates the earliest time this cron expression is valid. It will
// always pick a moment in the future, even if ref matches completely up to the // always pick a moment in the future, even if ref matches completely up to the
// minute. This function conciously does not take gap years into account. // minute. This function conciously does not take gap years into account.
pub fn (ce &CronExpression) next(ref time.Time) ?time.Time { pub fn (ce &CronExpression) next(ref time.Time) !time.Time {
// If the given ref matches the next cron occurence up to the minute, it // If the given ref matches the next cron occurence up to the minute, it
// will return that value. Because we always want to return a value in the // will return that value. Because we always want to return a value in the
// future, we artifically shift the ref 60 seconds to make sure we always // future, we artifically shift the ref 60 seconds to make sure we always
@ -117,19 +117,19 @@ pub fn (ce &CronExpression) next(ref time.Time) ?time.Time {
// next_from_now returns the result of ce.next(ref) where ref is the result of // next_from_now returns the result of ce.next(ref) where ref is the result of
// time.now(). // time.now().
pub fn (ce &CronExpression) next_from_now() ?time.Time { pub fn (ce &CronExpression) next_from_now() !time.Time {
return ce.next(time.now()) return ce.next(time.now())
} }
// next_n returns the n next occurences of the expression, given a starting // next_n returns the n next occurences of the expression, given a starting
// time. // time.
pub fn (ce &CronExpression) next_n(ref time.Time, n int) ?[]time.Time { pub fn (ce &CronExpression) next_n(ref time.Time, n int) ![]time.Time {
mut times := []time.Time{cap: n} mut times := []time.Time{cap: n}
times << ce.next(ref)? times << ce.next(ref)!
for i in 1 .. n { for i in 1 .. n {
times << ce.next(times[i - 1])? times << ce.next(times[i - 1])!
} }
return times return times
@ -137,7 +137,7 @@ pub fn (ce &CronExpression) next_n(ref time.Time, n int) ?[]time.Time {
// parse_range parses a given string into a range of sorted integers, if // parse_range parses a given string into a range of sorted integers, if
// possible. // possible.
fn parse_range(s string, min int, max int, mut bitv []bool) ? { fn parse_range(s string, min int, max int, mut bitv []bool) ! {
mut start := min mut start := min
mut end := max mut end := max
mut interval := 1 mut interval := 1
@ -228,11 +228,11 @@ fn bitv_to_ints(bitv []bool, min int) []int {
// parse_part parses a given part of a cron expression & returns the // parse_part parses a given part of a cron expression & returns the
// corresponding array of ints. // corresponding array of ints.
fn parse_part(s string, min int, max int) ?[]int { fn parse_part(s string, min int, max int) ![]int {
mut bitv := []bool{len: max - min + 1, init: false} mut bitv := []bool{len: max - min + 1, init: false}
for range in s.split(',') { for range in s.split(',') {
parse_range(range, min, max, mut bitv)? parse_range(range, min, max, mut bitv)!
} }
return bitv_to_ints(bitv, min) return bitv_to_ints(bitv, min)
@ -240,7 +240,7 @@ fn parse_part(s string, min int, max int) ?[]int {
// parse_expression parses an entire cron expression string into a // parse_expression parses an entire cron expression string into a
// CronExpression object, if possible. // CronExpression object, if possible.
pub fn parse_expression(exp string) ?CronExpression { pub fn parse_expression(exp string) !CronExpression {
// The filter allows for multiple spaces between parts // The filter allows for multiple spaces between parts
mut parts := exp.split(' ').filter(it != '') mut parts := exp.split(' ').filter(it != '')

View File

@ -11,88 +11,88 @@ fn parse_range_error(s string, min int, max int) string {
} }
// =====parse_range===== // =====parse_range=====
fn test_range_star_range() ? { fn test_range_star_range() ! {
mut bitv := []bool{len: 6, init: false} mut bitv := []bool{len: 6, init: false}
parse_range('*', 0, 5, mut bitv)? parse_range('*', 0, 5, mut bitv)!
assert bitv == [true, true, true, true, true, true] assert bitv == [true, true, true, true, true, true]
} }
fn test_range_number() ? { fn test_range_number() ! {
mut bitv := []bool{len: 6, init: false} mut bitv := []bool{len: 6, init: false}
parse_range('4', 0, 5, mut bitv)? parse_range('4', 0, 5, mut bitv)!
assert bitv_to_ints(bitv, 0) == [4] assert bitv_to_ints(bitv, 0) == [4]
} }
fn test_range_number_too_large() ? { fn test_range_number_too_large() ! {
assert parse_range_error('10', 0, 6) == 'Out of range.' assert parse_range_error('10', 0, 6) == 'Out of range.'
} }
fn test_range_number_too_small() ? { fn test_range_number_too_small() ! {
assert parse_range_error('0', 2, 6) == 'Out of range.' assert parse_range_error('0', 2, 6) == 'Out of range.'
} }
fn test_range_number_invalid() ? { fn test_range_number_invalid() ! {
assert parse_range_error('x', 0, 6) == 'Invalid number.' assert parse_range_error('x', 0, 6) == 'Invalid number.'
} }
fn test_range_step_star_1() ? { fn test_range_step_star_1() ! {
mut bitv := []bool{len: 21, init: false} mut bitv := []bool{len: 21, init: false}
parse_range('*/4', 0, 20, mut bitv)? parse_range('*/4', 0, 20, mut bitv)!
assert bitv_to_ints(bitv, 0) == [0, 4, 8, 12, 16, 20] assert bitv_to_ints(bitv, 0) == [0, 4, 8, 12, 16, 20]
} }
fn test_range_step_star_2() ? { fn test_range_step_star_2() ! {
mut bitv := []bool{len: 8, init: false} mut bitv := []bool{len: 8, init: false}
parse_range('*/3', 1, 8, mut bitv)? parse_range('*/3', 1, 8, mut bitv)!
assert bitv_to_ints(bitv, 1) == [1, 4, 7] assert bitv_to_ints(bitv, 1) == [1, 4, 7]
} }
fn test_range_step_star_too_large() ? { fn test_range_step_star_too_large() ! {
assert parse_range_error('*/21', 0, 20) == 'Step size too large.' assert parse_range_error('*/21', 0, 20) == 'Step size too large.'
} }
fn test_range_step_zero() ? { fn test_range_step_zero() ! {
assert parse_range_error('*/0', 0, 20) == 'Step size zero not allowed.' assert parse_range_error('*/0', 0, 20) == 'Step size zero not allowed.'
} }
fn test_range_step_number() ? { fn test_range_step_number() ! {
mut bitv := []bool{len: 21, init: false} mut bitv := []bool{len: 21, init: false}
parse_range('5/4', 2, 22, mut bitv)? parse_range('5/4', 2, 22, mut bitv)!
assert bitv_to_ints(bitv, 2) == [5, 9, 13, 17, 21] assert bitv_to_ints(bitv, 2) == [5, 9, 13, 17, 21]
} }
fn test_range_step_number_too_large() ? { fn test_range_step_number_too_large() ! {
assert parse_range_error('10/4', 0, 5) == 'Out of range.' assert parse_range_error('10/4', 0, 5) == 'Out of range.'
} }
fn test_range_step_number_too_small() ? { fn test_range_step_number_too_small() ! {
assert parse_range_error('2/4', 5, 10) == 'Out of range.' assert parse_range_error('2/4', 5, 10) == 'Out of range.'
} }
fn test_range_dash() ? { fn test_range_dash() ! {
mut bitv := []bool{len: 10, init: false} mut bitv := []bool{len: 10, init: false}
parse_range('4-8', 0, 9, mut bitv)? parse_range('4-8', 0, 9, mut bitv)!
assert bitv_to_ints(bitv, 0) == [4, 5, 6, 7, 8] assert bitv_to_ints(bitv, 0) == [4, 5, 6, 7, 8]
} }
fn test_range_dash_step() ? { fn test_range_dash_step() ! {
mut bitv := []bool{len: 10, init: false} mut bitv := []bool{len: 10, init: false}
parse_range('4-8/2', 0, 9, mut bitv)? parse_range('4-8/2', 0, 9, mut bitv)!
assert bitv_to_ints(bitv, 0) == [4, 6, 8] assert bitv_to_ints(bitv, 0) == [4, 6, 8]
} }
// =====parse_part===== // =====parse_part=====
fn test_part_single() ? { fn test_part_single() ! {
assert parse_part('*', 0, 5)? == [0, 1, 2, 3, 4, 5] assert parse_part('*', 0, 5)! == [0, 1, 2, 3, 4, 5]
} }
fn test_part_multiple() ? { fn test_part_multiple() ! {
assert parse_part('*/2,2/3', 1, 8)? == [1, 2, 3, 5, 7, 8] assert parse_part('*/2,2/3', 1, 8)! == [1, 2, 3, 5, 7, 8]
} }

View File

@ -2,12 +2,12 @@ module expression
import time { parse } import time { parse }
fn util_test_time(exp string, t1_str string, t2_str string) ? { fn util_test_time(exp string, t1_str string, t2_str string) ! {
ce := parse_expression(exp)? ce := parse_expression(exp)!
t1 := parse(t1_str)? t1 := parse(t1_str)!
t2 := parse(t2_str)? t2 := parse(t2_str)!
t3 := ce.next(t1)? t3 := ce.next(t1)!
assert t2.year == t3.year assert t2.year == t3.year
assert t2.month == t3.month assert t2.month == t3.month
@ -16,19 +16,19 @@ fn util_test_time(exp string, t1_str string, t2_str string) ? {
assert t2.minute == t3.minute assert t2.minute == t3.minute
} }
fn test_next_simple() ? { fn test_next_simple() ! {
// Very simple // Very simple
util_test_time('0 3', '2002-01-01 00:00:00', '2002-01-01 03:00:00')? util_test_time('0 3', '2002-01-01 00:00:00', '2002-01-01 03:00:00')!
// Overlap to next day // Overlap to next day
util_test_time('0 3', '2002-01-01 03:00:00', '2002-01-02 03:00:00')? util_test_time('0 3', '2002-01-01 03:00:00', '2002-01-02 03:00:00')!
util_test_time('0 3', '2002-01-01 04:00:00', '2002-01-02 03:00:00')? util_test_time('0 3', '2002-01-01 04:00:00', '2002-01-02 03:00:00')!
util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00')? util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00')!
// Overlap to next month // Overlap to next month
util_test_time('0 3', '2002-11-31 04:00:00', '2002-12-01 03:00:00')? util_test_time('0 3', '2002-11-31 04:00:00', '2002-12-01 03:00:00')!
// Overlap to next year // Overlap to next year
util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')? util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')!
} }

View File

@ -26,8 +26,8 @@ const (
) )
// init initializes a database & adds the correct tables. // init initializes a database & adds the correct tables.
pub fn init(db_path string) ?VieterDb { pub fn init(db_path string) !VieterDb {
conn := sqlite.connect(db_path)? conn := sqlite.connect(db_path)!
sql conn { sql conn {
create table MigrationVersion create table MigrationVersion

View File

@ -43,12 +43,12 @@ pub mut:
} }
// checksum calculates the sha256 hash of the package // checksum calculates the sha256 hash of the package
pub fn (p &Pkg) checksum() ?string { pub fn (p &Pkg) checksum() !string {
return util.hash_file(p.path) return util.hash_file(p.path)
} }
// parse_pkg_info_string parses a PkgInfo object from a string // parse_pkg_info_string parses a PkgInfo object from a string
fn parse_pkg_info_string(pkg_info_str &string) ?PkgInfo { fn parse_pkg_info_string(pkg_info_str &string) !PkgInfo {
mut pkg_info := PkgInfo{} mut pkg_info := PkgInfo{}
// Iterate over the entire string // Iterate over the entire string
@ -101,7 +101,7 @@ fn parse_pkg_info_string(pkg_info_str &string) ?PkgInfo {
// read_pkg_archive extracts the file list & .PKGINFO contents from an archive // read_pkg_archive extracts the file list & .PKGINFO contents from an archive
// NOTE: this command only supports zstd-, xz- & gzip-compressed tarballs. // NOTE: this command only supports zstd-, xz- & gzip-compressed tarballs.
pub fn read_pkg_archive(pkg_path string) ?Pkg { pub fn read_pkg_archive(pkg_path string) !Pkg {
if !os.is_file(pkg_path) { if !os.is_file(pkg_path) {
return error("'$pkg_path' doesn't exist or isn't a file.") return error("'$pkg_path' doesn't exist or isn't a file.")
} }
@ -159,7 +159,7 @@ pub fn read_pkg_archive(pkg_path string) ?Pkg {
pkg_text := unsafe { buf.vstring_with_len(size).clone() } pkg_text := unsafe { buf.vstring_with_len(size).clone() }
pkg_info = parse_pkg_info_string(pkg_text)? pkg_info = parse_pkg_info_string(pkg_text)!
} else { } else {
C.archive_read_data_skip(a) C.archive_read_data_skip(a)
} }
@ -201,7 +201,7 @@ pub fn (pkg &Pkg) filename() string {
} }
// to_desc returns a desc file valid string representation // to_desc returns a desc file valid string representation
pub fn (pkg &Pkg) to_desc() ?string { pub fn (pkg &Pkg) to_desc() !string {
p := pkg.info p := pkg.info
// filename // filename
@ -222,7 +222,7 @@ pub fn (pkg &Pkg) to_desc() ?string {
desc += format_entry('CSIZE', p.csize.str()) desc += format_entry('CSIZE', p.csize.str())
desc += format_entry('ISIZE', p.size.str()) desc += format_entry('ISIZE', p.size.str())
sha256sum := pkg.checksum()? sha256sum := pkg.checksum()!
desc += format_entry('SHA256SUM', sha256sum) desc += format_entry('SHA256SUM', sha256sum)

View File

@ -29,7 +29,7 @@ pub:
} }
// new creates a new RepoGroupManager & creates the directories as needed // new creates a new RepoGroupManager & creates the directories as needed
pub fn new(repos_dir string, pkg_dir string, default_arch string) ?RepoGroupManager { pub fn new(repos_dir string, pkg_dir string, default_arch string) !RepoGroupManager {
if !os.is_dir(repos_dir) { if !os.is_dir(repos_dir) {
os.mkdir_all(repos_dir) or { return error('Failed to create repos directory: $err.msg()') } os.mkdir_all(repos_dir) or { return error('Failed to create repos directory: $err.msg()') }
} }
@ -49,27 +49,27 @@ pub fn new(repos_dir string, pkg_dir string, default_arch string) ?RepoGroupMana
// pkg archive. It's a wrapper around add_pkg_in_repo that parses the archive // pkg archive. It's a wrapper around add_pkg_in_repo that parses the archive
// file, passes the result to add_pkg_in_repo, and hard links the archive to // file, passes the result to add_pkg_in_repo, and hard links the archive to
// the right subdirectories in r.pkg_dir if it was successfully added. // the right subdirectories in r.pkg_dir if it was successfully added.
pub fn (r &RepoGroupManager) add_pkg_from_path(repo string, pkg_path string) ?RepoAddResult { pub fn (r &RepoGroupManager) add_pkg_from_path(repo string, pkg_path string) !RepoAddResult {
pkg := package.read_pkg_archive(pkg_path) or { pkg := package.read_pkg_archive(pkg_path) or {
return error('Failed to read package file: $err.msg()') return error('Failed to read package file: $err.msg()')
} }
archs := r.add_pkg_in_repo(repo, pkg)? archs := r.add_pkg_in_repo(repo, pkg)!
// If the add was successful, we move the file to the packages directory // If the add was successful, we move the file to the packages directory
for arch in archs { for arch in archs {
repo_pkg_path := os.real_path(os.join_path(r.pkg_dir, repo, arch)) repo_pkg_path := os.real_path(os.join_path(r.pkg_dir, repo, arch))
dest_path := os.join_path_single(repo_pkg_path, pkg.filename()) dest_path := os.join_path_single(repo_pkg_path, pkg.filename())
os.mkdir_all(repo_pkg_path)? os.mkdir_all(repo_pkg_path)!
// We create hard links so that "any" arch packages aren't stored // We create hard links so that "any" arch packages aren't stored
// multiple times // multiple times
os.link(pkg_path, dest_path)? os.link(pkg_path, dest_path)!
} }
// After linking, we can remove the original file // After linking, we can remove the original file
os.rm(pkg_path)? os.rm(pkg_path)!
return RepoAddResult{ return RepoAddResult{
name: pkg.info.name name: pkg.info.name
@ -85,11 +85,11 @@ pub fn (r &RepoGroupManager) add_pkg_from_path(repo string, pkg_path string) ?Re
// r.default_arch. If this arch-repo doesn't exist yet, it is created. If the // r.default_arch. If this arch-repo doesn't exist yet, it is created. If the
// architecture isn't 'any', the package is only added to the specific // architecture isn't 'any', the package is only added to the specific
// architecture. // architecture.
fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ?[]string { fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ![]string {
// A package not of arch 'any' can be handled easily by adding it to the // A package not of arch 'any' can be handled easily by adding it to the
// respective repo // respective repo
if pkg.info.arch != 'any' { if pkg.info.arch != 'any' {
r.add_pkg_in_arch_repo(repo, pkg.info.arch, pkg)? r.add_pkg_in_arch_repo(repo, pkg.info.arch, pkg)!
return [pkg.info.arch] return [pkg.info.arch]
} }
@ -104,7 +104,7 @@ fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ?[]strin
// If this is the first package that's added to the repo, the directory // If this is the first package that's added to the repo, the directory
// won't exist yet // won't exist yet
if os.exists(repo_dir) { if os.exists(repo_dir) {
arch_repos = os.ls(repo_dir)? arch_repos = os.ls(repo_dir)!
} }
// The default_arch should always be updated when a package with arch 'any' // The default_arch should always be updated when a package with arch 'any'
@ -118,7 +118,7 @@ fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ?[]strin
// not know which arch-repositories did succeed in adding the package, if // not know which arch-repositories did succeed in adding the package, if
// any. // any.
for arch in arch_repos { for arch in arch_repos {
r.add_pkg_in_arch_repo(repo, arch, pkg)? r.add_pkg_in_arch_repo(repo, arch, pkg)!
} }
return arch_repos return arch_repos
@ -128,24 +128,24 @@ fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ?[]strin
// arch-repo. It records the package's data in the arch-repo's desc & files // arch-repo. It records the package's data in the arch-repo's desc & files
// files, and afterwards updates the db & files archives to reflect these // files, and afterwards updates the db & files archives to reflect these
// changes. // changes.
fn (r &RepoGroupManager) add_pkg_in_arch_repo(repo string, arch string, pkg &package.Pkg) ? { fn (r &RepoGroupManager) add_pkg_in_arch_repo(repo string, arch string, pkg &package.Pkg) ! {
pkg_dir := os.join_path(r.repos_dir, repo, arch, '$pkg.info.name-$pkg.info.version') pkg_dir := os.join_path(r.repos_dir, repo, arch, '$pkg.info.name-$pkg.info.version')
// Remove the previous version of the package, if present // Remove the previous version of the package, if present
r.remove_pkg_from_arch_repo(repo, arch, pkg.info.name, false)? r.remove_pkg_from_arch_repo(repo, arch, pkg.info.name, false)!
os.mkdir_all(pkg_dir) or { return error('Failed to create package directory.') } os.mkdir_all(pkg_dir) or { return error('Failed to create package directory.') }
os.write_file(os.join_path_single(pkg_dir, 'desc'), pkg.to_desc()?) or { os.write_file(os.join_path_single(pkg_dir, 'desc'), pkg.to_desc()!) or {
os.rmdir_all(pkg_dir)? os.rmdir_all(pkg_dir)!
return error('Failed to write desc file.') return error('Failed to write desc file.')
} }
os.write_file(os.join_path_single(pkg_dir, 'files'), pkg.to_files()) or { os.write_file(os.join_path_single(pkg_dir, 'files'), pkg.to_files()) or {
os.rmdir_all(pkg_dir)? os.rmdir_all(pkg_dir)!
return error('Failed to write files file.') return error('Failed to write files file.')
} }
r.sync(repo, arch)? r.sync(repo, arch)!
} }

View File

@ -5,7 +5,7 @@ import os
// remove_pkg_from_arch_repo removes a package from an arch-repo's database. It // remove_pkg_from_arch_repo removes a package from an arch-repo's database. It
// returns false if the package wasn't present in the database. It also // returns false if the package wasn't present in the database. It also
// optionally re-syncs the repo archives. // optionally re-syncs the repo archives.
pub fn (r &RepoGroupManager) remove_pkg_from_arch_repo(repo string, arch string, pkg_name string, sync bool) ?bool { pub fn (r &RepoGroupManager) remove_pkg_from_arch_repo(repo string, arch string, pkg_name string, sync bool) !bool {
repo_dir := os.join_path(r.repos_dir, repo, arch) repo_dir := os.join_path(r.repos_dir, repo, arch)
// If the repository doesn't exist yet, the result is automatically false // If the repository doesn't exist yet, the result is automatically false
@ -15,7 +15,7 @@ pub fn (r &RepoGroupManager) remove_pkg_from_arch_repo(repo string, arch string,
// We iterate over every directory in the repo dir // We iterate over every directory in the repo dir
// TODO filter so we only check directories // TODO filter so we only check directories
for d in os.ls(repo_dir)? { for d in os.ls(repo_dir)! {
// Because a repository only allows a single version of each package, // Because a repository only allows a single version of each package,
// we need only compare whether the name of the package is the same, // we need only compare whether the name of the package is the same,
// not the version. // not the version.
@ -25,22 +25,22 @@ pub fn (r &RepoGroupManager) remove_pkg_from_arch_repo(repo string, arch string,
// We lock the mutex here to prevent other routines from creating a // We lock the mutex here to prevent other routines from creating a
// new archive while we remove an entry // new archive while we remove an entry
lock r.mutex { lock r.mutex {
os.rmdir_all(os.join_path_single(repo_dir, d))? os.rmdir_all(os.join_path_single(repo_dir, d))!
} }
// Also remove the package archive // Also remove the package archive
repo_pkg_dir := os.join_path(r.pkg_dir, repo, arch) repo_pkg_dir := os.join_path(r.pkg_dir, repo, arch)
archives := os.ls(repo_pkg_dir)?.filter(it.split('-')#[..-3].join('-') == name) archives := os.ls(repo_pkg_dir)!.filter(it.split('-')#[..-3].join('-') == name)
for archive_name in archives { for archive_name in archives {
full_path := os.join_path_single(repo_pkg_dir, archive_name) full_path := os.join_path_single(repo_pkg_dir, archive_name)
os.rm(full_path)? os.rm(full_path)!
} }
// Sync the db archives if requested // Sync the db archives if requested
if sync { if sync {
r.sync(repo, arch)? r.sync(repo, arch)!
} }
return true return true
@ -51,7 +51,7 @@ pub fn (r &RepoGroupManager) remove_pkg_from_arch_repo(repo string, arch string,
} }
// remove_arch_repo removes an arch-repo & its packages. // remove_arch_repo removes an arch-repo & its packages.
pub fn (r &RepoGroupManager) remove_arch_repo(repo string, arch string) ?bool { pub fn (r &RepoGroupManager) remove_arch_repo(repo string, arch string) !bool {
repo_dir := os.join_path(r.repos_dir, repo, arch) repo_dir := os.join_path(r.repos_dir, repo, arch)
// If the repository doesn't exist yet, the result is automatically false // If the repository doesn't exist yet, the result is automatically false
@ -59,16 +59,16 @@ pub fn (r &RepoGroupManager) remove_arch_repo(repo string, arch string) ?bool {
return false return false
} }
os.rmdir_all(repo_dir)? os.rmdir_all(repo_dir)!
pkg_dir := os.join_path(r.pkg_dir, repo, arch) pkg_dir := os.join_path(r.pkg_dir, repo, arch)
os.rmdir_all(pkg_dir)? os.rmdir_all(pkg_dir)!
return true return true
} }
// remove_repo removes a repo & its packages. // remove_repo removes a repo & its packages.
pub fn (r &RepoGroupManager) remove_repo(repo string) ?bool { pub fn (r &RepoGroupManager) remove_repo(repo string) !bool {
repo_dir := os.join_path_single(r.repos_dir, repo) repo_dir := os.join_path_single(r.repos_dir, repo)
// If the repository doesn't exist yet, the result is automatically false // If the repository doesn't exist yet, the result is automatically false
@ -76,10 +76,10 @@ pub fn (r &RepoGroupManager) remove_repo(repo string) ?bool {
return false return false
} }
os.rmdir_all(repo_dir)? os.rmdir_all(repo_dir)!
pkg_dir := os.join_path_single(r.pkg_dir, repo) pkg_dir := os.join_path_single(r.pkg_dir, repo)
os.rmdir_all(pkg_dir)? os.rmdir_all(pkg_dir)!
return true return true
} }

View File

@ -32,7 +32,7 @@ fn archive_add_entry(archive &C.archive, entry &C.archive_entry, file_path &stri
} }
// sync regenerates the repository archive files. // sync regenerates the repository archive files.
fn (r &RepoGroupManager) sync(repo string, arch string) ? { fn (r &RepoGroupManager) sync(repo string, arch string) ! {
subrepo_path := os.join_path(r.repos_dir, repo, arch) subrepo_path := os.join_path(r.repos_dir, repo, arch)
lock r.mutex { lock r.mutex {
@ -54,7 +54,7 @@ fn (r &RepoGroupManager) sync(repo string, arch string) ? {
C.archive_write_open_filename(a_files, &char(files_path.str)) C.archive_write_open_filename(a_files, &char(files_path.str))
// Iterate over each directory // Iterate over each directory
for d in os.ls(subrepo_path)?.filter(os.is_dir(os.join_path_single(subrepo_path, for d in os.ls(subrepo_path)!.filter(os.is_dir(os.join_path_single(subrepo_path,
it))) { it))) {
// desc // desc
mut inner_path := os.join_path_single(d, 'desc') mut inner_path := os.join_path_single(d, 'desc')

View File

@ -43,9 +43,9 @@ fn (mut app App) v1_get_log_content(id int) web.Result {
// parse_query_time unescapes an HTTP query parameter & tries to parse it as a // parse_query_time unescapes an HTTP query parameter & tries to parse it as a
// time.Time struct. // time.Time struct.
fn parse_query_time(query string) ?time.Time { fn parse_query_time(query string) !time.Time {
unescaped := urllib.query_unescape(query)? unescaped := urllib.query_unescape(query)!
t := time.parse(unescaped)? t := time.parse(unescaped)!
return t return t
} }

View File

@ -1,7 +1,7 @@
module server module server
import cli import cli
import vieter_v.conf as vconf import conf as vconf
struct Config { struct Config {
pub: pub:
@ -18,11 +18,11 @@ pub fn cmd() cli.Command {
return cli.Command{ return cli.Command{
name: 'server' name: 'server'
description: 'Start the Vieter server.' description: 'Start the Vieter server.'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
config_file := cmd.flags.get_string('config-file')? config_file := cmd.flags.get_string('config-file')!
conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)? conf := vconf.load<Config>(prefix: 'VIETER_', default_path: config_file)!
server(conf)? server(conf)!
} }
} }
} }

View File

@ -24,7 +24,7 @@ pub mut:
} }
// server starts the web server & starts listening for requests // server starts the web server & starts listening for requests
pub fn server(conf Config) ? { pub fn server(conf Config) ! {
// Prevent using 'any' as the default arch // Prevent using 'any' as the default arch
if conf.default_arch == 'any' { if conf.default_arch == 'any' {
util.exit_with_message(1, "'any' is not allowed as the value for default_arch.") util.exit_with_message(1, "'any' is not allowed as the value for default_arch.")

View File

@ -5,7 +5,7 @@ import io
import os import os
// reader_to_writer tries to consume the entire reader & write it to the writer. // 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) ? { pub fn reader_to_writer(mut reader io.Reader, mut writer io.Writer) ! {
mut buf := []u8{len: 10 * 1024} mut buf := []u8{len: 10 * 1024}
for { for {
@ -21,8 +21,8 @@ pub fn reader_to_writer(mut reader io.Reader, mut writer io.Writer) ? {
} }
// reader_to_file writes the contents of a BufferedReader to a file // 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) ? { pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ! {
mut file := os.create(path)? mut file := os.create(path)!
defer { defer {
file.close() file.close()
} }
@ -69,11 +69,11 @@ pub fn match_array_in_array<T>(a1 []T, a2 []T) int {
// read_until_separator consumes an io.Reader until it encounters some // read_until_separator consumes an io.Reader until it encounters some
// separator array. The data read is stored inside the provided res array. // 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) ? { pub fn read_until_separator(mut reader io.Reader, mut res []u8, sep []u8) ! {
mut buf := []u8{len: sep.len} mut buf := []u8{len: sep.len}
for { for {
c := reader.read(mut buf)? c := reader.read(mut buf)!
res << buf[..c] res << buf[..c]
match_len := match_array_in_array(buf[..c], sep) match_len := match_array_in_array(buf[..c], sep)
@ -84,7 +84,7 @@ pub fn read_until_separator(mut reader io.Reader, mut res []u8, sep []u8) ? {
if match_len > 0 { if match_len > 0 {
match_left := sep.len - match_len match_left := sep.len - match_len
c2 := reader.read(mut buf[..match_left])? c2 := reader.read(mut buf[..match_left])!
res << buf[..c2] res << buf[..c2]
if buf[..c2] == sep[match_len..] { if buf[..c2] == sep[match_len..] {

View File

@ -23,7 +23,7 @@ pub fn exit_with_message(code int, msg string) {
} }
// hash_file returns the sha256 hash of a given file // hash_file returns the sha256 hash of a given file
pub fn hash_file(path &string) ?string { pub fn hash_file(path &string) !string {
file := os.open(path) or { return error('Failed to open file.') } file := os.open(path) or { return error('Failed to open file.') }
mut sha256sum := sha256.new() mut sha256sum := sha256.new()
@ -39,7 +39,7 @@ pub fn hash_file(path &string) ?string {
// This function never actually fails, but returns an option to follow // This function never actually fails, but returns an option to follow
// the Writer interface. // the Writer interface.
sha256sum.write(buf[..bytes_read])? sha256sum.write(buf[..bytes_read])!
} }
return sha256sum.checksum().hex() return sha256sum.checksum().hex()

View File

@ -3,33 +3,33 @@ module web
import log import log
// log reate a log message with the given level // log reate a log message with the given level
pub fn (mut ctx Context) log(msg &string, level log.Level) { pub fn (mut ctx Context) log(msg string, level log.Level) {
lock ctx.logger { lock ctx.logger {
ctx.logger.send_output(msg, level) ctx.logger.send_output(msg, level)
} }
} }
// lfatal create a log message with the fatal level // lfatal create a log message with the fatal level
pub fn (mut ctx Context) lfatal(msg &string) { pub fn (mut ctx Context) lfatal(msg string) {
ctx.log(msg, log.Level.fatal) ctx.log(msg, log.Level.fatal)
} }
// lerror create a log message with the error level // lerror create a log message with the error level
pub fn (mut ctx Context) lerror(msg &string) { pub fn (mut ctx Context) lerror(msg string) {
ctx.log(msg, log.Level.error) ctx.log(msg, log.Level.error)
} }
// lwarn create a log message with the warn level // lwarn create a log message with the warn level
pub fn (mut ctx Context) lwarn(msg &string) { pub fn (mut ctx Context) lwarn(msg string) {
ctx.log(msg, log.Level.warn) ctx.log(msg, log.Level.warn)
} }
// linfo create a log message with the info level // linfo create a log message with the info level
pub fn (mut ctx Context) linfo(msg &string) { pub fn (mut ctx Context) linfo(msg string) {
ctx.log(msg, log.Level.info) ctx.log(msg, log.Level.info)
} }
// ldebug create a log message with the debug level // ldebug create a log message with the debug level
pub fn (mut ctx Context) ldebug(msg &string) { pub fn (mut ctx Context) ldebug(msg string) {
ctx.log(msg, log.Level.debug) ctx.log(msg, log.Level.debug)
} }

View File

@ -8,7 +8,7 @@ import net.http
const attrs_to_ignore = ['auth'] const attrs_to_ignore = ['auth']
// Parsing function attributes for methods and path. // Parsing function attributes for methods and path.
fn parse_attrs(name string, attrs []string) ?([]http.Method, string) { fn parse_attrs(name string, attrs []string) !([]http.Method, string) {
if attrs.len == 0 { if attrs.len == 0 {
return [http.Method.get], '/$name' return [http.Method.get], '/$name'
} }
@ -61,7 +61,7 @@ fn parse_query_from_url(url urllib.URL) map[string]string {
} }
// Extract form data from an HTTP request. // Extract form data from an HTTP request.
fn parse_form_from_request(request http.Request) ?(map[string]string, map[string][]http.FileData) { fn parse_form_from_request(request http.Request) !(map[string]string, map[string][]http.FileData) {
mut form := map[string]string{} mut form := map[string]string{}
mut files := map[string][]http.FileData{} mut files := map[string][]http.FileData{}
if request.method in methods_with_form { if request.method in methods_with_form {

View File

@ -24,7 +24,7 @@ pub:
pub mut: pub mut:
// TCP connection to client. // TCP connection to client.
// But beware, do not store it for further use, after request processing web will close connection. // But beware, do not store it for further use, after request processing web will close connection.
conn &net.TcpConn conn &net.TcpConn = unsafe { nil }
// Gives access to a shared logger object // Gives access to a shared logger object
logger shared log.Log logger shared log.Log
// time.ticks() from start of web connection handle. // time.ticks() from start of web connection handle.
@ -67,20 +67,20 @@ struct Route {
pub fn (ctx Context) before_request() {} pub fn (ctx Context) before_request() {}
// send_string writes the given string to the TCP connection socket. // send_string writes the given string to the TCP connection socket.
fn (mut ctx Context) send_string(s string) ? { fn (mut ctx Context) send_string(s string) ! {
ctx.conn.write(s.bytes())? ctx.conn.write(s.bytes())!
} }
// send_reader reads at most `size` bytes from the given reader & writes them // send_reader reads at most `size` bytes from the given reader & writes them
// to the TCP connection socket. Internally, a 10KB buffer is used, to avoid // to the TCP connection socket. Internally, a 10KB buffer is used, to avoid
// having to store all bytes in memory at once. // having to store all bytes in memory at once.
fn (mut ctx Context) send_reader(mut reader io.Reader, size u64) ? { fn (mut ctx Context) send_reader(mut reader io.Reader, size u64) ! {
mut buf := []u8{len: 10_000} mut buf := []u8{len: 10_000}
mut bytes_left := size mut bytes_left := size
// Repeat as long as the stream still has data // Repeat as long as the stream still has data
for bytes_left > 0 { for bytes_left > 0 {
bytes_read := reader.read(mut buf)? bytes_read := reader.read(mut buf)!
bytes_left -= u64(bytes_read) bytes_left -= u64(bytes_read)
mut to_write := bytes_read mut to_write := bytes_read
@ -96,20 +96,20 @@ fn (mut ctx Context) send_reader(mut reader io.Reader, size u64) ? {
// send_custom_response sends the given http.Response to the client. It can be // send_custom_response sends the given http.Response to the client. It can be
// used to overwrite the Context object & send a completely custom // used to overwrite the Context object & send a completely custom
// http.Response instead. // http.Response instead.
fn (mut ctx Context) send_custom_response(resp &http.Response) ? { fn (mut ctx Context) send_custom_response(resp &http.Response) ! {
ctx.send_string(resp.bytestr())? ctx.send_string(resp.bytestr())!
} }
// send_response_header constructs a valid HTTP response with an empty body & // send_response_header constructs a valid HTTP response with an empty body &
// sends it to the client. // sends it to the client.
pub fn (mut ctx Context) send_response_header() ? { pub fn (mut ctx Context) send_response_header() ! {
mut resp := http.new_response( mut resp := http.new_response(
header: ctx.header.join(headers_close) header: ctx.header.join(headers_close)
) )
resp.header.add(.content_type, ctx.content_type) resp.header.add(.content_type, ctx.content_type)
resp.set_status(ctx.status) resp.set_status(ctx.status)
ctx.send_custom_response(resp)? ctx.send_custom_response(resp)!
} }
// send is a convenience function for sending the HTTP response with an empty // send is a convenience function for sending the HTTP response with an empty