From b3a119f221d0c1c22588ef6c7ec5502188bce509 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 8 Feb 2023 11:00:17 +0100 Subject: [PATCH] chore: ran v fmt for v 0.3.3 changes --- src/agent/cli.v | 2 +- src/agent/daemon.v | 20 ++++++++++---------- src/agent/images.v | 2 +- src/build/build.v | 8 ++++---- src/build/queue.v | 6 +++--- src/build/shell.v | 16 ++++++++-------- src/client/client.v | 20 ++++++++++---------- src/client/jobs.v | 4 ++-- src/client/logs.v | 12 ++++++------ src/client/repos.v | 6 +++--- src/client/targets.v | 12 ++++++------ src/console/aur/aur.v | 8 ++++---- src/console/logs/logs.v | 8 ++++---- src/console/repos/repos.v | 2 +- src/console/targets/targets.v | 16 ++++++++-------- src/cron/parse_test.v | 2 +- src/db/db.v | 6 +++--- src/db/logs.v | 20 ++++++++++---------- src/db/targets.v | 4 ++-- src/models/builds.v | 2 +- src/models/logs.v | 12 ++++++------ src/models/models.v | 8 ++++---- src/models/targets.v | 14 +++++++------- src/package/format.v | 14 +++++++------- src/package/package.v | 2 +- src/repo/add.v | 12 ++++++++---- src/server/api_logs.v | 8 ++++---- src/server/api_targets.v | 4 ++-- src/server/cli.v | 2 +- src/server/log_removal.v | 6 +++--- src/server/repo.v | 14 +++++++------- src/server/repo_remove.v | 18 +++++++++--------- src/server/server.v | 16 ++++++++-------- src/util/stream.v | 2 +- src/web/parse.v | 4 ++-- src/web/response/response.v | 14 +++++++------- src/web/web.v | 28 ++++++++++++++-------------- 37 files changed, 179 insertions(+), 175 deletions(-) diff --git a/src/agent/cli.v b/src/agent/cli.v index 1535e17..41e3421 100644 --- a/src/agent/cli.v +++ b/src/agent/cli.v @@ -23,7 +23,7 @@ pub fn cmd() cli.Command { description: 'Start an agent daemon.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! agent(conf)! } diff --git a/src/agent/daemon.v b/src/agent/daemon.v index 62f36c2..4364a92 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -51,7 +51,7 @@ pub fn (mut d AgentDaemon) run() { for { if sleep_time > 0 { - d.ldebug('Sleeping for $sleep_time') + d.ldebug('Sleeping for ${sleep_time}') time.sleep(sleep_time) } @@ -80,14 +80,14 @@ pub fn (mut d AgentDaemon) run() { d.ldebug('Polling for new jobs') new_configs := d.client.poll_jobs(d.conf.arch, finished + empty) or { - d.lerror('Failed to poll jobs: $err.msg()') + d.lerror('Failed to poll jobs: ${err.msg()}') // TODO pick a better delay here sleep_time = 5 * time.second continue } - d.ldebug('Received $new_configs.len jobs') + d.ldebug('Received ${new_configs.len} jobs') last_poll_time = time.now() @@ -95,7 +95,7 @@ pub fn (mut d AgentDaemon) run() { // Make sure a recent build base image is available for // building the config if !d.images.up_to_date(config.base_image) { - d.linfo('Building builder image from base image $config.base_image') + d.linfo('Building builder image from base image ${config.base_image}') // TODO handle this better than to just skip the config d.images.refresh_image(config.base_image) or { @@ -154,7 +154,7 @@ fn (mut d AgentDaemon) start_build(config BuildConfig) bool { stdatomic.store_u64(&d.atomics[i], agent.build_running) d.builds[i] = config - go d.run_build(i, config) + spawn d.run_build(i, config) return true } @@ -165,7 +165,7 @@ fn (mut d AgentDaemon) start_build(config BuildConfig) bool { // run_build actually starts the build process for a given target. fn (mut d AgentDaemon) run_build(build_index int, config BuildConfig) { - d.linfo('started build: $config') + d.linfo('started build: ${config}') // 0 means success, 1 means failure mut status := 0 @@ -176,21 +176,21 @@ fn (mut d AgentDaemon) run_build(build_index int, config BuildConfig) { } res := build.build_config(d.client.address, d.client.api_key, new_config) or { - d.ldebug('build_config error: $err.msg()') + d.ldebug('build_config error: ${err.msg()}') status = 1 build.BuildResult{} } if status == 0 { - d.linfo('Uploading build logs for $config') + d.linfo('Uploading build logs for ${config}') // TODO use the arch value here build_arch := os.uname().machine d.client.add_build_log(config.target_id, res.start_time, res.end_time, build_arch, - res.exit_code, res.logs) or { d.lerror('Failed to upload logs for $config') } + res.exit_code, res.logs) or { d.lerror('Failed to upload logs for ${config}') } } else { - d.lwarn('an error occurred during build: $config') + d.lwarn('an error occurred during build: ${config}') } stdatomic.store_u64(&d.atomics[build_index], agent.build_done) diff --git a/src/agent/images.v b/src/agent/images.v index 5fba0f7..9befc0c 100644 --- a/src/agent/images.v +++ b/src/agent/images.v @@ -71,7 +71,7 @@ pub fn (mut m ImageManager) up_to_date(base_image string) bool { fn (mut m ImageManager) refresh_image(base_image string) ! { // TODO use better image tags for built images new_image := build.create_build_image(base_image) or { - return error('Failed to build builder image from base image $base_image') + return error('Failed to build builder image from base image ${base_image}') } m.images[base_image] << new_image diff --git a/src/build/build.v b/src/build/build.v index dfea8c0..c69a613 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -45,7 +45,7 @@ pub fn create_build_image(base_image string) !string { c := docker.NewContainer{ image: base_image - env: ['BUILD_SCRIPT=$cmds_str'] + env: ['BUILD_SCRIPT=${cmds_str}'] entrypoint: ['/bin/sh', '-c'] cmd: ['echo \$BUILD_SCRIPT | base64 -d | /bin/sh -e'] } @@ -118,10 +118,10 @@ pub fn build_config(address string, api_key string, config BuildConfig) !BuildRe base64_script := base64.encode_str(build_script) c := docker.NewContainer{ - image: '$config.base_image' + image: '${config.base_image}' env: [ - 'BUILD_SCRIPT=$base64_script', - 'API_KEY=$api_key', + 'BUILD_SCRIPT=${base64_script}', + 'API_KEY=${api_key}', // `archlinux:base-devel` does not correctly set the path variable, // causing certain builds to fail. This fixes it. 'PATH=${build.path_dirs.join(':')}', diff --git a/src/build/queue.v b/src/build/queue.v index abd4ec6..2aa6e7a 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -36,7 +36,7 @@ pub struct BuildJobQueue { mut: mutex shared util.Dummy // For each architecture, a priority queue is tracked - queues map[string]MinHeap + queues map[string]MinHeap[BuildJob] // When a target is removed from the server or edited, its previous build // configs will be invalid. This map allows for those to be simply skipped // by ignoring any build configs created before this timestamp. @@ -74,7 +74,7 @@ pub struct InsertConfig { pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { lock q.mutex { if input.arch !in q.queues { - q.queues[input.arch] = MinHeap{} + q.queues[input.arch] = MinHeap[BuildJob]{} } mut job := BuildJob{ @@ -86,7 +86,7 @@ pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { if !input.now { ce := if input.target.schedule != '' { cron.parse_expression(input.target.schedule) or { - return error("Error while parsing cron expression '$input.target.schedule' (id $input.target.id): $err.msg()") + return error("Error while parsing cron expression '${input.target.schedule}' (id ${input.target.id}): ${err.msg()}") } } else { q.default_schedule diff --git a/src/build/shell.v b/src/build/shell.v index 16f93b5..f32cd08 100644 --- a/src/build/shell.v +++ b/src/build/shell.v @@ -24,12 +24,12 @@ pub fn echo_commands(cmds []string) []string { // create_build_script generates a shell script that builds a given Target. fn create_build_script(address string, config BuildConfig, build_arch string) string { - repo_url := '$address/$config.repo' + repo_url := '${address}/${config.repo}' mut commands := [ // This will later be replaced by a proper setting for changing the // mirrorlist - "echo -e '[$config.repo]\\nServer = $address/\$repo/\$arch\\nSigLevel = Optional' >> /etc/pacman.conf" + "echo -e '[${config.repo}]\\nServer = ${address}/\$repo/\$arch\\nSigLevel = Optional' >> /etc/pacman.conf" // We need to update the package list of the repo we just added above. // This should however not pull in a lot of packages as long as the // builder image is rebuilt frequently. @@ -42,18 +42,18 @@ fn create_build_script(address string, config BuildConfig, build_arch string) st 'git' { if config.branch == '' { [ - "git clone --single-branch --depth 1 '$config.url' repo", + "git clone --single-branch --depth 1 '${config.url}' repo", ] } else { [ - "git clone --single-branch --depth 1 --branch $config.branch '$config.url' repo", + "git clone --single-branch --depth 1 --branch ${config.branch} '${config.url}' repo", ] } } 'url' { [ 'mkdir repo', - "curl -o repo/PKGBUILD -L '$config.url'", + "curl -o repo/PKGBUILD -L '${config.url}'", ] } else { @@ -62,7 +62,7 @@ fn create_build_script(address string, config BuildConfig, build_arch string) st } commands << if config.path != '' { - "cd 'repo/$config.path'" + "cd 'repo/${config.path}'" } else { 'cd repo' } @@ -76,7 +76,7 @@ fn create_build_script(address string, config BuildConfig, build_arch string) st // The build container checks whether the package is already present on // the server. commands << [ - 'curl -s --head --fail $repo_url/$build_arch/\$pkgname-\$pkgver-\$pkgrel && exit 0', + 'curl -s --head --fail ${repo_url}/${build_arch}/\$pkgname-\$pkgver-\$pkgrel && exit 0', // If the above curl command succeeds, we don't need to rebuild the // package. However, because we're in a su shell, the exit command will // drop us back into the root shell. Therefore, we must check whether @@ -86,7 +86,7 @@ fn create_build_script(address string, config BuildConfig, build_arch string) st } commands << [ - 'MAKEFLAGS="-j\$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\$pkg" -H "X-API-KEY: \$API_KEY" $repo_url/publish; done', + 'MAKEFLAGS="-j\$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\$pkg" -H "X-API-KEY: \$API_KEY" ${repo_url}/publish; done', ] return echo_commands(commands).join('\n') diff --git a/src/client/client.v b/src/client/client.v index cce4e70..7d57e92 100644 --- a/src/client/client.v +++ b/src/client/client.v @@ -22,7 +22,7 @@ pub fn new(address string, api_key string) Client { // 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. 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 { mut params_escaped := map[string]string{} @@ -33,9 +33,9 @@ fn (c &Client) send_request_raw(method Method, url string, params map[string]str params_escaped[k] = urllib.query_escape(v) } - params_str := params_escaped.keys().map('$it=${params_escaped[it]}').join('&') + params_str := params_escaped.keys().map('${it}=${params_escaped[it]}').join('&') - full_url = '$full_url?$params_str' + full_url = '${full_url}?${params_str}' } // Looking at the source code, this function doesn't actually fail, so I'm @@ -49,13 +49,13 @@ fn (c &Client) send_request_raw(method Method, url string, params map[string]str } // send_request just calls send_request_with_body with an empty body. -fn (c &Client) send_request(method Method, url string, params map[string]string) !Response { - return c.send_request_with_body(method, url, params, '') +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, '') } // send_request_with_body calls send_request_raw_response & parses its // output as a Response object. -fn (c &Client) send_request_with_body(method Method, url string, params map[string]string, body string) !Response { +fn (c &Client) send_request_with_body[T](method Method, url string, params map[string]string, body string) !Response[T] { res := c.send_request_raw(method, url, params, body)! status := res.status() @@ -64,12 +64,12 @@ fn (c &Client) send_request_with_body(method Method, url string, params map[s if status.is_error() { // A non-successful status call will have an empty body if res.body == '' { - return error('Error $res.status_code ($status.str()): (empty response)') + return error('Error ${res.status_code} (${status.str()}): (empty response)') } - data := json.decode(Response, res.body)! + data := json.decode(Response[string], res.body)! - return error('Status $res.status_code ($status.str()): $data.message') + return error('Status ${res.status_code} (${status.str()}): ${data.message}') } // Just return an empty successful response @@ -77,7 +77,7 @@ fn (c &Client) send_request_with_body(method Method, url string, params map[s return new_data_response(T{}) } - data := json.decode(Response, res.body)! + data := json.decode(Response[T], res.body)! return data } diff --git a/src/client/jobs.v b/src/client/jobs.v index 784639e..ddb9e2d 100644 --- a/src/client/jobs.v +++ b/src/client/jobs.v @@ -4,7 +4,7 @@ import models { BuildConfig } // poll_jobs requests a list of new build jobs from the server. pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { - data := c.send_request<[]BuildConfig>(.get, '/api/v1/jobs/poll', { + data := c.send_request[[]BuildConfig](.get, '/api/v1/jobs/poll', { 'arch': arch 'max': max.str() })! @@ -15,7 +15,7 @@ pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { // queue_job adds a new one-time build job for the given target to the job // queue. pub fn (c &Client) queue_job(target_id int, arch string, force bool) ! { - c.send_request(.post, '/api/v1/jobs/queue', { + c.send_request[string](.post, '/api/v1/jobs/queue', { 'target': target_id.str() 'arch': arch 'force': force.str() diff --git a/src/client/logs.v b/src/client/logs.v index 6553837..ff6b7c5 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -7,27 +7,27 @@ import time // get_build_logs returns all build logs. pub fn (c &Client) get_build_logs(filter BuildLogFilter) ![]BuildLog { params := models.params_from(filter) - data := c.send_request<[]BuildLog>(.get, '/api/v1/logs', params)! + data := c.send_request[[]BuildLog](.get, '/api/v1/logs', params)! return data.data } // get_build_log returns a specific build log. pub fn (c &Client) get_build_log(id int) !BuildLog { - data := c.send_request(.get, '/api/v1/logs/$id', {})! + data := c.send_request[BuildLog](.get, '/api/v1/logs/${id}', {})! return data.data } // get_build_log_content returns the contents of the build log file. pub fn (c &Client) get_build_log_content(id int) !string { - data := c.send_request_raw_response(.get, '/api/v1/logs/$id/content', {}, '')! + data := c.send_request_raw_response(.get, '/api/v1/logs/${id}/content', {}, '')! return data } // 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 { +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 := { 'target': target_id.str() 'startTime': start_time.unix_time().str() @@ -36,12 +36,12 @@ pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time t 'exitCode': exit_code.str() } - data := c.send_request_with_body(.post, '/api/v1/logs', params, content)! + data := c.send_request_with_body[int](.post, '/api/v1/logs', params, content)! return data } // remove_build_log removes the build log with the given id from the server. pub fn (c &Client) remove_build_log(id int) ! { - c.send_request(.delete, '/api/v1/logs/$id', {})! + c.send_request[string](.delete, '/api/v1/logs/${id}', {})! } diff --git a/src/client/repos.v b/src/client/repos.v index 9644e9b..dff5d90 100644 --- a/src/client/repos.v +++ b/src/client/repos.v @@ -2,15 +2,15 @@ module client // remove_repo removes an entire repository. pub fn (c &Client) remove_repo(repo string) ! { - c.send_request(.delete, '/$repo', {})! + c.send_request[string](.delete, '/${repo}', {})! } // remove_arch_repo removes an entire arch-repo. pub fn (c &Client) remove_arch_repo(repo string, arch string) ! { - c.send_request(.delete, '/$repo/$arch', {})! + c.send_request[string](.delete, '/${repo}/${arch}', {})! } // remove_package removes a single package from the given arch-repo. pub fn (c &Client) remove_package(repo string, arch string, pkgname string) ! { - c.send_request(.delete, '/$repo/$arch/$pkgname', {})! + c.send_request[string](.delete, '/${repo}/${arch}/${pkgname}', {})! } diff --git a/src/client/targets.v b/src/client/targets.v index 565832e..3d43d43 100644 --- a/src/client/targets.v +++ b/src/client/targets.v @@ -5,7 +5,7 @@ import models { Target, TargetFilter } // get_targets returns a list of targets, given a filter object. pub fn (c &Client) get_targets(filter TargetFilter) ![]Target { params := models.params_from(filter) - data := c.send_request<[]Target>(.get, '/api/v1/targets', params)! + data := c.send_request[[]Target](.get, '/api/v1/targets', params)! return data.data } @@ -33,7 +33,7 @@ pub fn (c &Client) get_all_targets() ![]Target { // get_target returns the target for a specific id. pub fn (c &Client) get_target(id int) !Target { - data := c.send_request(.get, '/api/v1/targets/$id', {})! + data := c.send_request[Target](.get, '/api/v1/targets/${id}', {})! return data.data } @@ -49,15 +49,15 @@ pub struct NewTarget { // add_target adds a new target to the server. pub fn (c &Client) add_target(t NewTarget) !int { - params := models.params_from(t) - data := c.send_request(.post, '/api/v1/targets', params)! + params := models.params_from[NewTarget](t) + data := c.send_request[int](.post, '/api/v1/targets', params)! return data.data } // remove_target removes the target with the given id from the server. pub fn (c &Client) remove_target(id int) !string { - data := c.send_request(.delete, '/api/v1/targets/$id', {})! + data := c.send_request[string](.delete, '/api/v1/targets/${id}', {})! return data.data } @@ -65,7 +65,7 @@ pub fn (c &Client) remove_target(id int) !string { // patch_target sends a PATCH request to the given target with the params as // payload. pub fn (c &Client) patch_target(id int, params map[string]string) !string { - data := c.send_request(.patch, '/api/v1/targets/$id', params)! + data := c.send_request[string](.patch, '/api/v1/targets/${id}', params)! return data.data } diff --git a/src/console/aur/aur.v b/src/console/aur/aur.v index a6a3324..6fc8513 100644 --- a/src/console/aur/aur.v +++ b/src/console/aur/aur.v @@ -36,7 +36,7 @@ pub fn cmd() cli.Command { required_args: 2 execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! c := aur.new() pkgs := c.info(cmd.args[1..])! @@ -46,14 +46,14 @@ pub fn cmd() cli.Command { for pkg in pkgs { vc.add_target( kind: 'git' - url: 'https://aur.archlinux.org/$pkg.package_base' + '.git' + url: 'https://aur.archlinux.org/${pkg.package_base}' + '.git' repo: cmd.args[0] ) or { - println('Failed to add $pkg.name: $err.msg()') + println('Failed to add ${pkg.name}: ${err.msg()}') continue } - println('Added $pkg.name' + '.') + println('Added ${pkg.name}' + '.') } } }, diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 35ce4d7..518b507 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -74,7 +74,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! mut filter := BuildLogFilter{} @@ -156,7 +156,7 @@ pub fn cmd() cli.Command { description: 'Remove a build log that matches the given id.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! remove(conf, cmd.args[0])! } @@ -168,7 +168,7 @@ pub fn cmd() cli.Command { description: 'Show all info for a specific build log.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! id := cmd.args[0].int() info(conf, id)! @@ -181,7 +181,7 @@ pub fn cmd() cli.Command { description: 'Output the content of a build log to stdout.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! id := cmd.args[0].int() content(conf, id)! diff --git a/src/console/repos/repos.v b/src/console/repos/repos.v index 729208e..0021d52 100644 --- a/src/console/repos/repos.v +++ b/src/console/repos/repos.v @@ -28,7 +28,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! if cmd.args.len < 3 { if !cmd.flags.get_bool('force')! { diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 709c196..1b8d4be 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -54,7 +54,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! mut filter := TargetFilter{} @@ -113,7 +113,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! t := NewTarget{ kind: cmd.flags.get_string('kind')! @@ -135,7 +135,7 @@ pub fn cmd() cli.Command { description: 'Remove a target that matches the given id.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! remove(conf, cmd.args[0])! } @@ -147,7 +147,7 @@ pub fn cmd() cli.Command { description: 'Show detailed information for the target matching the id.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! info(conf, cmd.args[0])! } @@ -196,7 +196,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! found := cmd.flags.get_all_found() @@ -235,7 +235,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! remote := cmd.flags.get_bool('remote')! force := cmd.flags.get_bool('force')! @@ -280,7 +280,7 @@ fn add(conf Config, t &NewTarget, raw bool) ! { if raw { println(target_id) } else { - println('Target added with id $target_id') + println('Target added with id ${target_id}') } } @@ -296,7 +296,7 @@ fn patch(conf Config, id string, params map[string]string) ! { // invalid one to the server. if 'schedule' in params && params['schedule'] != '' { cron.parse_expression(params['schedule']) or { - return error('Invalid cron expression: $err.msg()') + return error('Invalid cron expression: ${err.msg()}') } } diff --git a/src/cron/parse_test.v b/src/cron/parse_test.v index 0dce7c2..19575d7 100644 --- a/src/cron/parse_test.v +++ b/src/cron/parse_test.v @@ -25,7 +25,7 @@ fn test_not_allowed() { for exp in illegal_expressions { res = false parse_expression(exp) or { res = true } - assert res, "'$exp' should produce an error" + assert res, "'${exp}' should produce an error" } } diff --git a/src/db/db.v b/src/db/db.v index 98ee000..73a5e83 100644 --- a/src/db/db.v +++ b/src/db/db.v @@ -55,7 +55,7 @@ pub fn init(db_path string) !VieterDb { version_num := i + 1 // vfmt does not like these dots - println('Applying migration $version_num' + '...') + println('Applying migration ${version_num}' + '...') // The sqlite library seems to not like it when multiple statements are // passed in a single exec. Therefore, we split them & run them all @@ -64,7 +64,7 @@ pub fn init(db_path string) !VieterDb { res := conn.exec_none(part) if res != sqlite.sqlite_done { - return error('An error occurred while applying migration $version_num: SQLite error code $res') + return error('An error occurred while applying migration ${version_num}: SQLite error code ${res}') } } @@ -82,7 +82,7 @@ pub fn init(db_path string) !VieterDb { // row_into converts an sqlite.Row into a given type T by parsing each field // from a string according to its type. -pub fn row_into(row sqlite.Row) T { +pub fn row_into[T](row sqlite.Row) T { mut i := 0 mut out := T{} diff --git a/src/db/logs.v b/src/db/logs.v index 2745467..0321183 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -8,20 +8,20 @@ pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog { mut where_parts := []string{} if filter.target != 0 { - where_parts << 'target_id == $filter.target' + where_parts << 'target_id == ${filter.target}' } if filter.before != time.Time{} { - where_parts << 'start_time < $filter.before.unix_time()' + where_parts << 'start_time < ${filter.before.unix_time()}' } if filter.after != time.Time{} { - where_parts << 'start_time > $filter.after.unix_time()' + where_parts << 'start_time > ${filter.after.unix_time()}' } // NOTE: possible SQL injection if filter.arch != '' { - where_parts << "arch == '$filter.arch'" + where_parts << "arch == '${filter.arch}'" } mut parts := []string{} @@ -30,27 +30,27 @@ pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog { if exp[0] == `!` { code := exp[1..].int() - parts << 'exit_code != $code' + parts << 'exit_code != ${code}' } else { code := exp.int() - parts << 'exit_code == $code' + parts << 'exit_code == ${code}' } } if parts.len > 0 { - where_parts << parts.map('($it)').join(' or ') + where_parts << parts.map('(${it})').join(' or ') } mut where_str := '' if where_parts.len > 0 { - where_str = 'where ' + where_parts.map('($it)').join(' and ') + where_str = 'where ' + where_parts.map('(${it})').join(' and ') } - query := 'select * from BuildLog $where_str limit $filter.limit offset $filter.offset' + query := 'select * from BuildLog ${where_str} limit ${filter.limit} offset ${filter.offset}' rows, _ := db.conn.exec(query) - res := rows.map(row_into(it)) + res := rows.map(row_into[BuildLog](it)) return res } diff --git a/src/db/targets.v b/src/db/targets.v index 2644f49..35ee270 100644 --- a/src/db/targets.v +++ b/src/db/targets.v @@ -49,13 +49,13 @@ pub fn (db &VieterDb) update_target(target_id int, params map[string]string) { if field.name in params { // Any fields that are array types require their own update method $if field.typ is string { - values << "$field.name = '${params[field.name]}'" + values << "${field.name} = '${params[field.name]}'" } } } values_str := values.join(', ') // I think this is actual SQL & not the ORM language - query := 'update Target set $values_str where id == $target_id' + query := 'update Target set ${values_str} where id == ${target_id}' db.conn.exec_none(query) } diff --git a/src/models/builds.v b/src/models/builds.v index 926a53c..be2910c 100644 --- a/src/models/builds.v +++ b/src/models/builds.v @@ -14,5 +14,5 @@ pub: // str return a single-line string representation of a build log pub fn (c BuildConfig) str() string { - return '{ target: $c.target_id, kind: $c.kind, url: $c.url, branch: $c.branch, path: $c.path, repo: $c.repo, base_image: $c.base_image, force: $c.force }' + return '{ target: ${c.target_id}, kind: ${c.kind}, url: ${c.url}, branch: ${c.branch}, path: ${c.path}, repo: ${c.repo}, base_image: ${c.base_image}, force: ${c.force} }' } diff --git a/src/models/logs.v b/src/models/logs.v index 66a3a0a..cb01d08 100644 --- a/src/models/logs.v +++ b/src/models/logs.v @@ -16,13 +16,13 @@ pub mut: // str returns a string representation. pub fn (bl &BuildLog) str() string { mut parts := [ - 'id: $bl.id', - 'target id: $bl.target_id', - 'start time: $bl.start_time.local()', - 'end time: $bl.end_time.local()', + 'id: ${bl.id}', + 'target id: ${bl.target_id}', + 'start time: ${bl.start_time.local()}', + 'end time: ${bl.end_time.local()}', 'duration: ${bl.end_time - bl.start_time}', - 'arch: $bl.arch', - 'exit code: $bl.exit_code', + 'arch: ${bl.arch}', + 'exit code: ${bl.exit_code}', ] str := parts.join('\n') diff --git a/src/models/models.v b/src/models/models.v index b6103d3..9111286 100644 --- a/src/models/models.v +++ b/src/models/models.v @@ -4,17 +4,17 @@ import time // from_params creates a new instance of T from the given map by parsing all // of its fields from the map. -pub fn from_params(params map[string]string) ?T { +pub fn from_params[T](params map[string]string) ?T { mut o := T{} - patch_from_params(mut o, params)? + patch_from_params[T](mut o, params)? return o } // patch_from_params updates the given T object with the params defined in // the map. -pub fn patch_from_params(mut o T, params map[string]string) ? { +pub fn patch_from_params[T](mut o T, params map[string]string) ? { $for field in T.fields { if field.name in params && params[field.name] != '' { $if field.typ is string { @@ -37,7 +37,7 @@ pub fn patch_from_params(mut o T, params map[string]string) ? { } // params_from converts a given T struct into a map of strings. -pub fn params_from(o &T) map[string]string { +pub fn params_from[T](o &T) map[string]string { mut out := map[string]string{} $for field in T.fields { diff --git a/src/models/targets.v b/src/models/targets.v index a0c88d0..3c0c9cf 100644 --- a/src/models/targets.v +++ b/src/models/targets.v @@ -38,13 +38,13 @@ pub mut: // str returns a string representation. pub fn (t &Target) str() string { mut parts := [ - 'id: $t.id', - 'kind: $t.kind', - 'url: $t.url', - 'branch: $t.branch', - 'path: $t.path', - 'repo: $t.repo', - 'schedule: $t.schedule', + 'id: ${t.id}', + 'kind: ${t.kind}', + 'url: ${t.url}', + 'branch: ${t.branch}', + 'path: ${t.path}', + 'repo: ${t.repo}', + 'schedule: ${t.schedule}', 'arch: ${t.arch.map(it.value).join(', ')}', ] str := parts.join('\n') diff --git a/src/package/format.v b/src/package/format.v index a81d327..b126f3a 100644 --- a/src/package/format.v +++ b/src/package/format.v @@ -3,14 +3,14 @@ module package // format_entry returns a string properly formatted to be added to a desc file. [inline] fn format_entry(key string, value string) string { - return '\n%$key%\n$value\n' + return '\n%${key}%\n${value}\n' } // full_name returns the properly formatted name for the package, including // version & architecture pub fn (pkg &Pkg) full_name() string { p := pkg.info - return '$p.name-$p.version-$p.arch' + return '${p.name}-${p.version}-${p.arch}' } // filename returns the correct filename of the package file @@ -20,10 +20,10 @@ pub fn (pkg &Pkg) filename() string { 1 { '.tar.gz' } 6 { '.tar.xz' } 14 { '.tar.zst' } - else { panic("Another compression code shouldn't be possible. Faulty code: $pkg.compression") } + else { panic("Another compression code shouldn't be possible. Faulty code: ${pkg.compression}") } } - return '${pkg.full_name()}.pkg$ext' + return '${pkg.full_name()}.pkg${ext}' } // to_desc returns a desc file valid string representation @@ -31,7 +31,7 @@ pub fn (pkg &Pkg) to_desc() !string { p := pkg.info // filename - mut desc := '%FILENAME%\n$pkg.filename()\n' + mut desc := '%FILENAME%\n${pkg.filename()}\n' desc += format_entry('NAME', p.name) desc += format_entry('BASE', p.base) @@ -94,10 +94,10 @@ pub fn (pkg &Pkg) to_desc() !string { desc += format_entry('CHECKDEPENDS', p.checkdepends.join_lines()) } - return '$desc\n' + return '${desc}\n' } // to_files returns a files file valid string representation pub fn (pkg &Pkg) to_files() string { - return '%FILES%\n$pkg.files.join_lines()\n' + return '%FILES%\n${pkg.files.join_lines()}\n' } diff --git a/src/package/package.v b/src/package/package.v index 4518ffd..6cf8e3d 100644 --- a/src/package/package.v +++ b/src/package/package.v @@ -103,7 +103,7 @@ fn parse_pkg_info_string(pkg_info_str &string) !PkgInfo { // NOTE: this command only supports zstd-, xz- & gzip-compressed tarballs. pub fn read_pkg_archive(pkg_path string) !Pkg { 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.") } a := C.archive_read_new() diff --git a/src/repo/add.v b/src/repo/add.v index 8ab3ae1..47b0d7e 100644 --- a/src/repo/add.v +++ b/src/repo/add.v @@ -31,11 +31,15 @@ pub: // new creates a new RepoGroupManager & creates the directories as needed pub fn new(repos_dir string, pkg_dir string, default_arch string) !RepoGroupManager { 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()}') + } } if !os.is_dir(pkg_dir) { - os.mkdir_all(pkg_dir) or { return error('Failed to create package directory: $err.msg()') } + os.mkdir_all(pkg_dir) or { + return error('Failed to create package directory: ${err.msg()}') + } } return RepoGroupManager{ @@ -51,7 +55,7 @@ pub fn new(repos_dir string, pkg_dir string, default_arch string) !RepoGroupMana // 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 { 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)! @@ -129,7 +133,7 @@ fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ![]strin // files, and afterwards updates the db & files archives to reflect these // changes. 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 r.remove_pkg_from_arch_repo(repo, arch, pkg.info.name, false)! diff --git a/src/server/api_logs.v b/src/server/api_logs.v index 3db4204..3e210b0 100644 --- a/src/server/api_logs.v +++ b/src/server/api_logs.v @@ -13,7 +13,7 @@ import models { BuildLog, BuildLogFilter } // optionally be added to limit the list of build logs to that repository. ['/api/v1/logs'; auth; get; markused] fn (mut app App) v1_get_logs() web.Result { - filter := models.from_params(app.query) or { + filter := models.from_params[BuildLogFilter](app.query) or { return app.json(.bad_request, new_response('Invalid query parameters.')) } logs := app.db.get_build_logs(filter) @@ -101,7 +101,7 @@ fn (mut app App) v1_post_log() web.Result { // Create the logs directory of it doesn't exist if !os.exists(os.dir(log_file_path)) { os.mkdir_all(os.dir(log_file_path)) or { - app.lerror('Error while creating log file: $err.msg()') + app.lerror('Error while creating log file: ${err.msg()}') return app.status(.internal_server_error) } @@ -109,7 +109,7 @@ fn (mut app App) v1_post_log() web.Result { if length := app.req.header.get(.content_length) { util.reader_to_file(mut app.reader, length.int(), log_file_path) or { - app.lerror('An error occured while receiving logs: $err.msg()') + app.lerror('An error occured while receiving logs: ${err.msg()}') return app.status(.internal_server_error) } @@ -127,7 +127,7 @@ fn (mut app App) v1_delete_log(id int) web.Result { full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path()) os.rm(full_path) or { - app.lerror('Failed to remove log file $full_path: $err.msg()') + app.lerror('Failed to remove log file ${full_path}: ${err.msg()}') return app.status(.internal_server_error) } diff --git a/src/server/api_targets.v b/src/server/api_targets.v index f47467a..a8fdf37 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -8,7 +8,7 @@ import models { Target, TargetArch, TargetFilter } // v1_get_targets returns the current list of targets. ['/api/v1/targets'; auth; get; markused] fn (mut app App) v1_get_targets() web.Result { - filter := models.from_params(app.query) or { + filter := models.from_params[TargetFilter](app.query) or { return app.json(.bad_request, new_response('Invalid query parameters.')) } mut iter := app.db.targets(filter) @@ -35,7 +35,7 @@ fn (mut app App) v1_post_target() web.Result { params['arch'] = app.conf.default_arch } - mut new_target := models.from_params(params) or { + mut new_target := models.from_params[Target](params) or { return app.json(.bad_request, new_response(err.msg())) } diff --git a/src/server/cli.v b/src/server/cli.v index 9a8b144..c272d52 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -25,7 +25,7 @@ pub fn cmd() cli.Command { description: 'Start the Vieter server.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! server(conf)! } diff --git a/src/server/log_removal.v b/src/server/log_removal.v index 27dc0db..bc51bcf 100644 --- a/src/server/log_removal.v +++ b/src/server/log_removal.v @@ -12,7 +12,7 @@ fn (mut app App) log_removal_daemon(schedule &cron.Expression) { for { mut too_old_timestamp := time.now().add_days(-app.conf.max_log_age) - app.linfo('Cleaning logs before $too_old_timestamp') + app.linfo('Cleaning logs before ${too_old_timestamp}') mut logs := []BuildLog{} mut counter := 0 @@ -29,7 +29,7 @@ fn (mut app App) log_removal_daemon(schedule &cron.Expression) { log_file_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path()) os.rm(log_file_path) or { - app.lerror('Failed to remove log file $log_file_path: $err.msg()') + app.lerror('Failed to remove log file ${log_file_path}: ${err.msg()}') failed += 1 continue @@ -44,7 +44,7 @@ fn (mut app App) log_removal_daemon(schedule &cron.Expression) { } } - app.linfo('Cleaned $counter logs ($failed failed)') + app.linfo('Cleaned ${counter} logs (${failed} failed)') // Sleep until the next cycle next_time := schedule.next_from_now() diff --git a/src/server/repo.v b/src/server/repo.v index 38d07fe..724d9d0 100644 --- a/src/server/repo.v +++ b/src/server/repo.v @@ -26,7 +26,7 @@ fn (mut app App) get_repo_file(repo string, arch string, filename string) web.Re // There's no point in having the ability to serve db archives with wrong // filenames - if db_exts.any(filename == '$repo$it') { + if db_exts.any(filename == '${repo}${it}') { full_path = os.join_path(app.repo.repos_dir, repo, arch, filename) // repo-add does this using symlinks, but we just change the requested @@ -62,19 +62,19 @@ fn (mut app App) put_package(repo string) web.Result { // Generate a random filename for the temp file pkg_path = os.join_path_single(app.repo.pkg_dir, rand.uuid_v4()) - app.ldebug("Uploading $length bytes (${util.pretty_bytes(length.int())}) to '$pkg_path'.") + app.ldebug("Uploading ${length} bytes (${util.pretty_bytes(length.int())}) to '${pkg_path}'.") // This is used to time how long it takes to upload a file mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true }) util.reader_to_file(mut app.reader, length.int(), pkg_path) or { - app.lwarn("Failed to upload '$pkg_path'") + app.lwarn("Failed to upload '${pkg_path}'") return app.status(.internal_server_error) } sw.stop() - app.ldebug("Upload of '$pkg_path' completed in ${sw.elapsed().seconds():.3}s.") + app.ldebug("Upload of '${pkg_path}' completed in ${sw.elapsed().seconds():.3}s.") } else { app.lwarn('Tried to upload package without specifying a Content-Length.') @@ -83,14 +83,14 @@ fn (mut app App) put_package(repo string) web.Result { } res := app.repo.add_pkg_from_path(repo, pkg_path) or { - app.lerror('Error while adding package: $err.msg()') + app.lerror('Error while adding package: ${err.msg()}') - os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg()") } + os.rm(pkg_path) or { app.lerror("Failed to remove download '${pkg_path}': ${err.msg()}") } return app.status(.internal_server_error) } - app.linfo("Added '$res.name-$res.version' to '$repo (${res.archs.join(',')})'.") + app.linfo("Added '${res.name}-${res.version}' to '${repo} (${res.archs.join(',')})'.") return app.json(.ok, new_data_response(res)) } diff --git a/src/server/repo_remove.v b/src/server/repo_remove.v index 9e6d747..24baeaf 100644 --- a/src/server/repo_remove.v +++ b/src/server/repo_remove.v @@ -6,17 +6,17 @@ import web ['/:repo/:arch/:pkg'; auth; delete; markused] fn (mut app App) delete_package(repo string, arch string, pkg string) web.Result { res := app.repo.remove_pkg_from_arch_repo(repo, arch, pkg, true) or { - app.lerror('Error while deleting package: $err.msg()') + app.lerror('Error while deleting package: ${err.msg()}') return app.status(.internal_server_error) } if res { - app.linfo("Removed package '$pkg' from '$repo/$arch'") + app.linfo("Removed package '${pkg}' from '${repo}/${arch}'") return app.status(.ok) } else { - app.linfo("Tried removing package '$pkg' from '$repo/$arch', but it doesn't exist.") + app.linfo("Tried removing package '${pkg}' from '${repo}/${arch}', but it doesn't exist.") return app.status(.not_found) } @@ -26,17 +26,17 @@ fn (mut app App) delete_package(repo string, arch string, pkg string) web.Result ['/:repo/:arch'; auth; delete; markused] fn (mut app App) delete_arch_repo(repo string, arch string) web.Result { res := app.repo.remove_arch_repo(repo, arch) or { - app.lerror('Error while deleting arch-repo: $err.msg()') + app.lerror('Error while deleting arch-repo: ${err.msg()}') return app.status(.internal_server_error) } if res { - app.linfo("Removed arch-repo '$repo/$arch'") + app.linfo("Removed arch-repo '${repo}/${arch}'") return app.status(.ok) } else { - app.linfo("Tried removing '$repo/$arch', but it doesn't exist.") + app.linfo("Tried removing '${repo}/${arch}', but it doesn't exist.") return app.status(.not_found) } @@ -46,17 +46,17 @@ fn (mut app App) delete_arch_repo(repo string, arch string) web.Result { ['/:repo'; auth; delete; markused] fn (mut app App) delete_repo(repo string) web.Result { res := app.repo.remove_repo(repo) or { - app.lerror('Error while deleting repo: $err.msg()') + app.lerror('Error while deleting repo: ${err.msg()}') return app.status(.internal_server_error) } if res { - app.linfo("Removed repo '$repo'") + app.linfo("Removed repo '${repo}'") return app.status(.ok) } else { - app.linfo("Tried removing '$repo', but it doesn't exist.") + app.linfo("Tried removing '${repo}', but it doesn't exist.") return app.status(.not_found) } diff --git a/src/server/server.v b/src/server/server.v index c6bdd1a..79d93e2 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -44,11 +44,11 @@ pub fn server(conf Config) ! { } global_ce := cron.parse_expression(conf.global_schedule) or { - util.exit_with_message(1, 'Invalid global cron expression: $err.msg()') + util.exit_with_message(1, 'Invalid global cron expression: ${err.msg()}') } log_removal_ce := cron.parse_expression(conf.log_removal_schedule) or { - util.exit_with_message(1, 'Invalid log removal cron expression: $err.msg()') + util.exit_with_message(1, 'Invalid log removal cron expression: ${err.msg()}') } // Configure logger @@ -89,7 +89,7 @@ pub fn server(conf Config) ! { db_file := os.join_path_single(conf.data_dir, server.db_file_name) db := db.init(db_file) or { - util.exit_with_message(1, 'Failed to initialize database: $err.msg()') + util.exit_with_message(1, 'Failed to initialize database: ${err.msg()}') } mut collector := if conf.collect_metrics { @@ -97,9 +97,9 @@ pub fn server(conf Config) ! { } else { &metrics.MetricsCollector(metrics.new_null_collector()) } - - collector.histogram_buckets_set('http_requests_duration_seconds', [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, - 10] ) + + collector.histogram_buckets_set('http_requests_duration_seconds', [0.001, 0.005, 0.01, 0.05, + 0.1, 0.5, 1, 5, 10]) mut app := &App{ logger: logger @@ -111,11 +111,11 @@ pub fn server(conf Config) ! { job_queue: build.new_job_queue(global_ce, conf.base_image) } app.init_job_queue() or { - util.exit_with_message(1, 'Failed to inialize job queue: $err.msg()') + util.exit_with_message(1, 'Failed to inialize job queue: ${err.msg()}') } if conf.max_log_age > 0 { - go app.log_removal_daemon(log_removal_ce) + spawn app.log_removal_daemon(log_removal_ce) } web.run(app, conf.port) diff --git a/src/util/stream.v b/src/util/stream.v index 15cc618..ec4c971 100644 --- a/src/util/stream.v +++ b/src/util/stream.v @@ -51,7 +51,7 @@ pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ! { // match_array_in_array 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(a1 []T, a2 []T) int { +pub fn match_array_in_array[T](a1 []T, a2 []T) int { mut i := 0 mut match_len := 0 diff --git a/src/web/parse.v b/src/web/parse.v index 889944b..9e26f85 100644 --- a/src/web/parse.v +++ b/src/web/parse.v @@ -10,7 +10,7 @@ const attrs_to_ignore = ['auth', 'markused'] // Parsing function attributes for methods and path. fn parse_attrs(name string, attrs []string) !([]http.Method, string) { if attrs.len == 0 { - return [http.Method.get], '/$name' + return [http.Method.get], '/${name}' } mut x := attrs.clone() @@ -45,7 +45,7 @@ fn parse_attrs(name string, attrs []string) !([]http.Method, string) { methods = [http.Method.get] } if path == '' { - path = '/$name' + path = '/${name}' } // Make path lowercase for case-insensitive comparisons return methods, path.to_lower() diff --git a/src/web/response/response.v b/src/web/response/response.v index a06a589..f736f77 100644 --- a/src/web/response/response.v +++ b/src/web/response/response.v @@ -1,6 +1,6 @@ module response -pub struct Response { +pub struct Response[T] { pub: message string data T @@ -8,8 +8,8 @@ pub: // new_response constructs a new Response object with the given message // & an empty data field. -pub fn new_response(message string) Response { - return Response{ +pub fn new_response(message string) Response[string] { + return Response[string]{ message: message data: '' } @@ -17,8 +17,8 @@ pub fn new_response(message string) Response { // new_data_response constructs a new Response object with the given data // & an empty message field. -pub fn new_data_response(data T) Response { - return Response{ +pub fn new_data_response[T](data T) Response[T] { + return Response[T]{ message: '' data: data } @@ -26,8 +26,8 @@ pub fn new_data_response(data T) Response { // new_full_response constructs a new Response object with the given // message & data. -pub fn new_full_response(message string, data T) Response { - return Response{ +pub fn new_full_response[T](message string, data T) Response[T] { + return Response[T]{ message: message data: data } diff --git a/src/web/web.v b/src/web/web.v index f0f3523..300ce32 100644 --- a/src/web/web.v +++ b/src/web/web.v @@ -158,7 +158,7 @@ pub fn (mut ctx Context) body(status http.Status, content_type string, body stri } // json HTTP_OK with json_s as payload with content-type `application/json` -pub fn (mut ctx Context) json(status http.Status, j T) Result { +pub fn (mut ctx Context) json[T](status http.Status, j T) Result { ctx.status = status ctx.content_type = 'application/json' @@ -278,14 +278,14 @@ interface DbInterface { // run runs the app [manualfree] -pub fn run(global_app &T, port int) { - mut l := net.listen_tcp(.ip6, ':$port') or { panic('failed to listen $err.code() $err') } +pub fn run[T](global_app &T, port int) { + mut l := net.listen_tcp(.ip6, ':${port}') or { panic('failed to listen ${err.code()} ${err}') } // Parsing methods attributes mut routes := map[string]Route{} $for method in T.methods { http_methods, route_path := parse_attrs(method.name, method.attrs) or { - eprintln('error parsing method attributes: $err') + eprintln('error parsing method attributes: ${err}') return } @@ -294,7 +294,7 @@ pub fn run(global_app &T, port int) { path: route_path } } - println('[Vweb] Running app on http://localhost:$port') + println('[Vweb] Running app on http://localhost:${port}') for { // Create a new app object for each connection, copy global data like db connections mut request_app := &T{} @@ -311,16 +311,16 @@ pub fn run(global_app &T, port int) { request_app.Context = global_app.Context // copy the context ref that contains static files map etc mut conn := l.accept() or { // failures should not panic - eprintln('accept() failed with error: $err.msg()') + eprintln('accept() failed with error: ${err.msg()}') continue } - go handle_conn(mut conn, mut request_app, routes) + spawn handle_conn[T](mut conn, mut request_app, routes) } } // handle_conn handles a connection [manualfree] -fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { +fn handle_conn[T](mut conn net.TcpConn, mut app T, routes map[string]Route) { conn.set_read_timeout(30 * time.second) conn.set_write_timeout(30 * time.second) @@ -362,8 +362,8 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { // Request parse head := http.parse_request_head(mut reader) or { // Prevents errors from being thrown when BufferedReader is empty - if '$err' != 'none' { - eprintln('error parsing request head: $err') + if '${err}' != 'none' { + eprintln('error parsing request head: ${err}') } return } @@ -371,7 +371,7 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { // The healthcheck spams the logs, which isn't very useful if head.url != '/health' { lock app.logger { - app.logger.debug('$head.method $head.url $head.version') + app.logger.debug('${head.method} ${head.url} ${head.version}') } } @@ -385,7 +385,7 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { // URL Parse url := urllib.parse(head.url) or { - eprintln('error parsing path: $err') + eprintln('error parsing path: ${err}') return } @@ -423,7 +423,7 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { $for method in T.methods { $if method.return_type is Result { route := routes[method.name] or { - eprintln('parsed attributes for the `$method.name` are not found, skipping...') + eprintln('parsed attributes for the `${method.name}` are not found, skipping...') Route{} } @@ -455,7 +455,7 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { method_args := params.clone() if method_args.len != method.args.len { - eprintln('warning: uneven parameters count ($method.args.len) in `$method.name`, compared to the web route `$method.attrs` ($method_args.len)') + eprintln('warning: uneven parameters count (${method.args.len}) in `${method.name}`, compared to the web route `${method.attrs}` (${method_args.len})') } app.$method(method_args) return