Switched to proper default values system

main
Jef Roosens 2022-04-06 18:17:33 +02:00
parent b70be0574e
commit 2aa2aa143c
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 26 additions and 38 deletions

View File

@ -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

View File

@ -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<T>(path string) ?T {
mut res := T{}
if os.exists(path) {
res = toml.parse_file(path) ?.reflect<T>()
// 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<T>(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.")
}
}
}

8
vieter.toml 100644
View File

@ -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"