From 2aa2aa143ca92a1f93e29a9c7bed369d62516a48 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 6 Apr 2022 18:17:33 +0200 Subject: [PATCH] Switched to proper default values system --- Makefile | 23 +++-------------------- src/env.v | 33 +++++++++++++++------------------ vieter.toml | 8 ++++++++ 3 files changed, 26 insertions(+), 38 deletions(-) create mode 100644 vieter.toml diff --git a/Makefile b/Makefile index 2238585d..76ab7b58 100644 --- a/Makefile +++ b/Makefile @@ -23,13 +23,7 @@ dvieter: $(SOURCES) # Run the debug build inside gdb .PHONY: gdb gdb: dvieter - VIETER_API_KEY=test \ - VIETER_DOWNLOAD_DIR=data/downloads \ - VIETER_REPO_DIR=data/repo \ - VIETER_PKG_DIR=data/pkgs \ - VIETER_LOG_LEVEL=DEBUG \ - VIETER_REPOS_FILE=data/repos.json \ - gdb --args ./dvieter + gdb --args './dvieter -f vieter.toml server' # Optimised production build .PHONY: prod @@ -46,22 +40,11 @@ c: # Run the server in the default 'data' directory .PHONY: run run: vieter - VIETER_API_KEY=test \ - VIETER_DOWNLOAD_DIR=data/downloads \ - VIETER_REPO_DIR=data/repo \ - VIETER_PKG_DIR=data/pkgs \ - VIETER_LOG_LEVEL=DEBUG \ - VIETER_REPOS_FILE=data/repos.json \ - ./vieter server + ./vieter -f vieter.toml server .PHONY: run-prod run-prod: prod - VIETER_API_KEY=test \ - VIETER_DOWNLOAD_DIR=data/downloads \ - VIETER_REPO_DIR=data/repo \ - VIETER_PKG_DIR=data/pkgs \ - VIETER_LOG_LEVEL=DEBUG \ - ./pvieter server + ./pvieter -f vieter.toml server # =====OTHER===== .PHONY: lint diff --git a/src/env.v b/src/env.v index 6abd6749..b7337919 100644 --- a/src/env.v +++ b/src/env.v @@ -12,8 +12,8 @@ const file_suffix = '_FILE' pub struct ServerConfig { pub: - log_level string [default: WARN] - log_file string [default: 'vieter.log'] + log_level string = "WARN" + log_file string = "vieter.log" pkg_dir string download_dir string api_key string @@ -65,7 +65,18 @@ pub fn load(path string) ?T { mut res := T{} if os.exists(path) { - res = toml.parse_file(path) ?.reflect() + // We don't use reflect here because reflect also sets any fields not + // in the toml back to "empty", which we don't want + doc := toml.parse_file(path) ? + + $for field in T.fields { + s := doc.value(field.name) + + // We currently only support strings + if s.type_name() == "string" { + res.$(field.name) = s.string() + } + } } $for field in T.fields { @@ -80,21 +91,7 @@ pub fn load(path string) ?T { // If there's no value from the toml file either, we try to find a // default value else if res.$(field.name) == '' { - // We use the default instead, if it's present - mut default := '' - - for attr in field.attrs { - if attr.starts_with('default: ') { - default = attr[9..] - break - } - } - - if default == '' { - return error("Missing config variable '$field.name' with no provided default. Either add it to the config file or provide it using an environment variable.") - } - - res.$(field.name) = default + return error("Missing config variable '$field.name' with no provided default. Either add it to the config file or provide it using an environment variable.") } } } diff --git a/vieter.toml b/vieter.toml new file mode 100644 index 00000000..3f953987 --- /dev/null +++ b/vieter.toml @@ -0,0 +1,8 @@ +# This file contains settings used during development +api_key = "test" +download_dir = "data/downloads" +repo_dir = "data/repo" +pkg_dir = "data/pkgs" +# log_level = "DEBUG" +repos_file = "data/repos.json" +