From c656e672e27ae789efec5ec7d34111a0fccf88ad Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 6 Apr 2022 18:20:14 +0200 Subject: [PATCH] Moved config structs to more logical location --- src/build/build.v | 3 +-- src/build/cli.v | 8 +++++++- src/env.v | 19 +------------------ src/server/cli.v | 13 ++++++++++++- src/server/server.v | 5 ++--- 5 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/build/build.v b/src/build/build.v index 6f2b0a84..934627f3 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -3,7 +3,6 @@ module build import docker import encoding.base64 import time -import env import net.http import git import json @@ -62,7 +61,7 @@ fn create_build_image() ?string { return image.id } -fn build(conf env.BuildConfig) ? { +fn build(conf Config) ? { // We get the repos list from the Vieter instance mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ? req.add_custom_header('X-Api-Key', conf.api_key) ? diff --git a/src/build/cli.v b/src/build/cli.v index 463d2ca3..c95d1caa 100644 --- a/src/build/cli.v +++ b/src/build/cli.v @@ -3,6 +3,12 @@ module build import cli import env +pub struct Config { +pub: + api_key string + address string +} + // cmd returns the cli submodule that handles the build process pub fn cmd() cli.Command { return cli.Command{ @@ -10,7 +16,7 @@ pub fn cmd() cli.Command { description: 'Run the build process.' execute: fn (cmd cli.Command) ? { config_file := cmd.flags.get_string('config-file') ? - conf := env.load(config_file) ? + conf := env.load(config_file) ? build(conf) ? } diff --git a/src/env.v b/src/env.v index b7337919..7b207654 100644 --- a/src/env.v +++ b/src/env.v @@ -10,23 +10,6 @@ const prefix = 'VIETER_' // instead const file_suffix = '_FILE' -pub struct ServerConfig { -pub: - log_level string = "WARN" - log_file string = "vieter.log" - pkg_dir string - download_dir string - api_key string - repo_dir string - repos_file string -} - -pub struct BuildConfig { -pub: - api_key string - address string -} - fn get_env_var(field_name string) ?string { env_var_name := '$env.prefix$field_name.to_upper()' env_file_name := '$env.prefix$field_name.to_upper()$env.file_suffix' @@ -73,7 +56,7 @@ pub fn load(path string) ?T { s := doc.value(field.name) // We currently only support strings - if s.type_name() == "string" { + if s.type_name() == 'string' { res.$(field.name) = s.string() } } diff --git a/src/server/cli.v b/src/server/cli.v index 6e676867..2383d9f6 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -3,6 +3,17 @@ module server import cli import env +struct Config { +pub: + log_level string = 'WARN' + log_file string = 'vieter.log' + pkg_dir string + download_dir string + api_key string + repo_dir string + repos_file string +} + // cmd returns the cli submodule that handles starting the server pub fn cmd() cli.Command { return cli.Command{ @@ -10,7 +21,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 := env.load(config_file) ? + conf := env.load(config_file) ? server(conf) ? } diff --git a/src/server/server.v b/src/server/server.v index 0738bc4f..ab2e46bc 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -4,7 +4,6 @@ import web import os import log import repo -import env import util const port = 8000 @@ -12,7 +11,7 @@ const port = 8000 struct App { web.Context pub: - conf env.ServerConfig [required; web_global] + conf Config [required; web_global] pub mut: repo repo.Repo [required; web_global] // This is used to claim the file lock on the repos file @@ -20,7 +19,7 @@ pub mut: } // server starts the web server & starts listening for requests -pub fn server(conf env.ServerConfig) ? { +pub fn server(conf Config) ? { // Configure logger log_level := log.level_from_tag(conf.log_level) or { util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.')